The ice-cream shop example

Ice creams are yummy, and you can almost always customize them to your tastefrom 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 enumIceCreamPart, and attach their prices right into it. We'll also track the number of scoops ...

Get Hands-On Design Patterns with Swift now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.