Skip to content

Instantly share code, notes, and snippets.

@iyengarajay
Last active July 13, 2016 10:58
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/57f2176365ecff317e1e3b2d470b25a1 to your computer and use it in GitHub Desktop.
Save iyengarajay/57f2176365ecff317e1e3b2d470b25a1 to your computer and use it in GitHub Desktop.
import java.util.function.Function;
public class StrategyPatternTest {
public static void main(String[] args) {
Function<Double, Double> normalStrategy = (Double d) -> d;
Function<Double, Double> happyHourStrategy = (Double d) -> d * 0.5;
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