The decorator pattern is a powerful design pattern that can be leveraged when building objects at runtime, where the final object has features that are defined over time, and where only the final outcome matters:
- Identify in your program an object that can or should be abstracted with this pattern
- Define the base protocol for your object (in our case, that was Burger)
- Define the decorator interface, in our case the BurgerDecorator, and ensure it conforms to the decorator's interface (Burger)
- Leverage protocol extensions to provide the default values for the decorator
- Provide at least one constructor or method to decorate the base object (.init(Burger) or .decorate(Burger))
- Build your decorating features out of the decorator's interface
- Use your decorator objects to build (over time and in multiple steps) your decorated objects out of base implementations