The ice-cream shop example
Ice creams are yummy, and you can almost always customize them to your taste—from the number of scoops, to choosing whether you want a wafer cone or a cup, and even whether or not to dip in chocolate for extra crunch.
We're in charge of designing the billing part, but there's a twist. We'll need to be able to run promotions, offer family discounts, and implement a loyal customer program.
This kind of problem is easily solved using the strategy pattern, as it allows us to reconfigure an algorithm (the billing algorithm) at runtime.
First, let's work out the model. For the sake of simplicity, we put all the items into an enum, IceCreamPart, and attach their prices right into it. We'll also track the number of scoops ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access