Sunday, December 25, 2016

Strategy and Factory patterns in Typescript

I've recently read the dzone article Design Patterns: The Strategy and Factory Patterns which gives clean example of how design patterns such as Strategy and Factory patterns can improve code structure in every day programming tasks.
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:

// 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;
}
}
}

Sunday, December 4, 2016

React, Redux architecture notes



After some discovering of the React JS (and Redux of course) as a way to build modern web apps, I've collected some keynotes that could have helped the newbie to start off faster.