I've converted the JAVA syntax used in the article to Typescript, to see how close are the language constructs in those two. Here's the gist with the code:
This file contains 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 { | |
calculateInterest(accountBalance:Number):Number; | |
} | |
//////////////////////////////////////// | |
/// Null object implementation and 4 account type related calculation stategies | |
//////////////////////////////////////// | |
class NoInterestCalculation implements InterestCalculationStrategy { | |
calculateInterest(accountBalance:Number):Number { | |
return 0; | |
} | |
} | |
class CurrentAccountInterestCalculation implements InterestCalculationStrategy { | |
calculateInterest(accountBalance:Number):Number { | |
return +accountBalance * (0.02 / 12); | |
} | |
} | |
class SavingsAccountInterestCalculation implements InterestCalculationStrategy { | |
calculateInterest(accountBalance:Number):Number { | |
return +accountBalance * (0.04 / 12); | |
} | |
} | |
class MoneyMarketInterestCalculation implements InterestCalculationStrategy { | |
calculateInterest(accountBalance:Number):Number { | |
return +accountBalance * (0.06/12); | |
} | |
} | |
class HighRollerMoneyMarketInterestCalculation implements InterestCalculationStrategy { | |
calculateInterest(accountBalance:Number):Number { | |
return accountBalance < 100000.00 ? 0 : (+accountBalance) * (0.075/12) | |
} | |
} | |
//////////////////////////////////////// | |
/// select and apply the correct strategy | |
//////////////////////////////////////// | |
class InterestCalculationStrategyFactory { | |
//Strategies for calculating interest. | |
private currentAccountInterestCalculationStrategy:InterestCalculationStrategy = new CurrentAccountInterestCalculation(); | |
private savingsAccountInterestCalculationStrategy:InterestCalculationStrategy = new SavingsAccountInterestCalculation(); | |
private moneyMarketAccountInterestCalculationStrategy:InterestCalculationStrategy = new MoneyMarketInterestCalculation(); | |
private highRollerMoneyMarketAccountInterestCalculationStrategy:InterestCalculationStrategy = new HighRollerMoneyMarketInterestCalculation(); | |
private noInterestCalculationStrategy:InterestCalculationStrategy = new NoInterestCalculation(); | |
public getInterestCalculationStrategy(accountType:AccountTypes):InterestCalculationStrategy { | |
switch (accountType) { | |
case AccountTypes.CURRENT: return this.currentAccountInterestCalculationStrategy; | |
case AccountTypes.SAVINGS: return this.savingsAccountInterestCalculationStrategy; | |
case AccountTypes.STANDARD_MONEY_MARKET: return this.moneyMarketAccountInterestCalculationStrategy; | |
case AccountTypes.HIGH_ROLLER_MONEY_MARKET: return this.highRollerMoneyMarketAccountInterestCalculationStrategy; | |
default: return this.noInterestCalculationStrategy; | |
} | |
} | |
} | |