Decorator Design Pattern:
This pattern allows behavior to be added to an individual object either statically or dynamically, without affecting the behavior of other objects from the same class.The decorator pattern is used to extend or alter the functionality of objects at run- time by wrapping them in an object of a decorator class. This provides a flexible alternative to using inheritance to modify behaviour.
protocol Coffee { func getCost() -> Double func getIngredients() -> String } class SimpleCoffee: Coffee { func getCost() -> Double { return 1.0 } func getIngredients() -> String { return "Coffee" } } class CoffeeDecorator: Coffee { private let decoratedCoffee: Coffee private let ingredientSeparator: String = ", " required init(decoratedCoffee: Coffee) { self.decoratedCoffee = decoratedCoffee } func getCost() -> Double { return decoratedCoffee.getCost() } func getIngredients() -> String { return decoratedCoffee.getIngredients() } } class Milk: CoffeeDecorator { required init(decoratedCoffee: Coffee) { super.init(decoratedCoffee: decoratedCoffee) } override func getCost() -> Double { return super.getCost() + 0.5 } override func getIngredients() -> String { return super.getIngredients() + ingredientSeparator + "Milk" } } class WhipCoffee: CoffeeDecorator { required init(decoratedCoffee: Coffee) { super.init(decoratedCoffee: decoratedCoffee) } override func getCost() -> Double { return super.getCost() + 0.7 } override func getIngredients() -> String { return super.getIngredients() + ingredientSeparator + "Whip" } }
var someCoffee: Coffee = SimpleCoffee() print("Cost : \(someCoffee.getCost()); Ingredients: \(someCoffee.getIngredients())") someCoffee = Milk(decoratedCoffee: someCoffee) print("Cost : \(someCoffee.getCost()); Ingredients: \(someCoffee.getIngredients())") someCoffee = WhipCoffee(decoratedCoffee: someCoffee) print("Cost : \(someCoffee.getCost()); Ingredients: \(someCoffee.getIngredients())")
Output
Cost : 1.0; Ingredients: Coffee
Cost : 1.5; Ingredients: Coffee, Milk
Cost : 2.2; Ingredients: Coffee, Milk, Whip