Skip to content

Instantly share code, notes, and snippets.

@iyengarajay
Last active July 13, 2016 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iyengarajay/0a9c08bb45fd1f614769bfab9843d2ae to your computer and use it in GitHub Desktop.
Save iyengarajay/0a9c08bb45fd1f614769bfab9843d2ae to your computer and use it in GitHub Desktop.
public class StrategyPatternTest {
public static void main(String[] args) {
Function<Double, Double> normalStrategy = BillingStrategy::getNormalStrategy;
Function<Double, Double> happyHourStrategy = BillingStrategy::getHappyHourStrategy;
Customer firstCustomer = new Customer(normalStrategy);
// Normal billing
firstCustomer.add(1.0, 1);
// Start Happy Hour
firstCustomer.setStrategy(happyHourStrategy);
firstCustomer.add(1.0, 2);
// New Customer
Customer secondCustomer = new Customer(happyHourStrategy);
secondCustomer.add(0.8, 1);
// The Customer pays
firstCustomer.printBill();
// End Happy Hour
secondCustomer.setStrategy(normalStrategy);
secondCustomer.add(1.3, 2);
secondCustomer.add(2.5, 1);
secondCustomer.printBill();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment