The Flyweight Pattern
Minimizing mana consumption by sharing magical aspects.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Flyweight
ARCANUM SCHOOL: Transmutation // Efficiency
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// SWIFT PATTERN //
class AppleTexture {
let color: String
init(color: String) { self.color = color }
}
class TextureFactory {
private var cache: [String: AppleTexture] = [:]
func getTexture(color: String) -> AppleTexture {
if let t = cache[color] { return t }
let t = AppleTexture(color: color)
cache[color] = t
return t
}
}
The Flyweight: The Orchard Cache
By caching and sharing AppleTexture, the Flyweight pattern allows an enchanter to conjure thousands of apples without depleting their mana reserves.
Runic Tags:
#swift
#design-pattern
#structural