The Factory Method Pattern
Delegating the instantiation of magical artifacts to subclasses.
β§ GANG OF FOUR LINEAGE & ARCANUM β§
GOF DESIGN PATTERN: Factory Method
ARCANUM SCHOOL: Conjuration // Artifactmancy
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// SWIFT PATTERN //
protocol Wand {
func cast() -> String
}
class ElderWand: Wand {
func cast() -> String { return "Casting with Elder Wand..." }
}
protocol WandMaker {
func createWand() -> Wand
}
class ElderWandMaker: WandMaker {
func createWand() -> Wand { return ElderWand() }
}
The Factory Method: The Wand Makerβs Secret
The Factory Method allows an arcane protocol to define an interface for creating wands, but lets conforming types decide which wand to instantiate.
Runic Tags:
#swift
#design-pattern
#creational