Forked from shershen08/typescript-strategy-and-factory-patterns.ts
Last active
October 19, 2022 17:12
-
-
Save DaSchTour/c5a9e768811720b246e3be23fc0af0e9 to your computer and use it in GitHub Desktop.
Strategy and factory patterns in TypeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see article with examples in JAVA here: https://dzone.com/articles/design-patterns-the-strategy-and-factory-patterns | |
// example for educational purposes shownig close and mature syntax of modern TypeScript | |
enum AccountTypes {CURRENT, SAVINGS, HIGH_ROLLER_MONEY_MARKET, STANDARD_MONEY_MARKET} | |
//////////////////////////////////////// | |
/// the interface that is used by the strategy | |
//////////////////////////////////////// | |
interface InterestCalculationStrategy { | |
canHandle(accountType: AccountTypes): boolean; | |
calculateInterest(accountBalance: number): number; | |
} | |
//////////////////////////////////////// | |
/// Null object implementation and 4 account type related calculation stategies | |
//////////////////////////////////////// | |
class NoInterestCalculation implements InterestCalculationStrategy { | |
canHandle(accountType: AccountTypes): boolean { | |
return true; | |
} | |
calculateInterest(accountBalance: number): number { | |
return 0; | |
} | |
} | |
class CurrentAccountInterestCalculation implements InterestCalculationStrategy { | |
canHandle(accountType: AccountTypes): boolean { | |
return accountType === AccountTypes.CURRENT; | |
} | |
calculateInterest(accountBalance: number): number { | |
return +accountBalance * (0.02 / 12); | |
} | |
} | |
class SavingsAccountInterestCalculation implements InterestCalculationStrategy { | |
canHandle(accountType: AccountTypes): boolean { | |
return accountType === AccountTypes.SAVINGS; | |
} | |
calculateInterest(accountBalance: number): number { | |
return +accountBalance * (0.04 / 12); | |
} | |
} | |
class MoneyMarketInterestCalculation implements InterestCalculationStrategy { | |
canHandle(accountType: AccountTypes): boolean { | |
return accountType === AccountTypes.STANDARD_MONEY_MARKET; | |
} | |
calculateInterest(accountBalance: number): number { | |
return +accountBalance * (0.06/12); | |
} | |
} | |
class HighRollerMoneyMarketInterestCalculation implements InterestCalculationStrategy { | |
canHandle(accountType: AccountTypes): boolean { | |
return accountType === AccountTypes.HIGH_ROLLER_MONEY_MARKET; | |
} | |
calculateInterest(accountBalance: number): number { | |
return accountBalance < 100000.00 ? 0 : (+accountBalance) * (0.075/12) | |
} | |
} | |
//////////////////////////////////////// | |
/// select and apply the correct strategy | |
//////////////////////////////////////// | |
class InterestCalculationStrategyFactory { | |
public getInterestCalculationStrategy(accountType: AccountTypes): InterestCalculationStrategy { | |
return [ | |
new CurrentAccountInterestCalculation(), | |
new SavingsAccountInterestCalculation(), | |
new MoneyMarketInterestCalculation(), | |
new HighRollerMoneyMarketInterestCalculation(), | |
new NoInterestCalculation() | |
].find((strategy) => strategy.canHandle(accountType)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment