December 2018
Intermediate to advanced
414 pages
10h 19m
English
The decorator pattern is extremely powerful when assembling a series of objects together. We'll use a burger as an example, from the point of view of the cashier who processes and bills it.
First, let's define a basic protocol, which will encapsulate the price and the different ingredients in the current burger. In the decorator pattern, this protocol will represent the object that will be decorated over and over:
public protocol Burger { var price: Double { get } var ingredients: [String] { get }}
Then we define a decorator: BurgerDecorator. In other languages, it may be implemented as an abstract class.
The decorator is implemented with the following protocol. It also conforms to the type that defines the base object ...
Read now
Unlock full access