December 2018
Intermediate to advanced
414 pages
10h 19m
English
First, we'll need to identify the object that will be reused over and over in our flyweight pattern—Ingredient:
struct Ingredient: CustomDebugStringConvertible { let name: String var debugDescription: String { return name }}
The Ingredient object is a simple wrapper around our name, but it could be more complex as our program grows.
Next, we need an object that will manage the creation of those Ingredient objects. As we're leveraging the flyweight pattern, we want to reduce the number of instances of the Ingredient object:
struct IngredientManager {
The knownIngredients dictionary will act as our object cache:
private var knownIngredients = [String: Ingredient]() mutating func get(withName name: ...
Read now
Unlock full access