πŸ•ŠοΈ SWIFT ENCHANTMENTS DIFFICULTY: Apprentice Creational

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