🕊️ SWIFT ENCHANTMENTS DIFFICULTY: Adept Creational

The Abstract Factory Pattern

A protocol-oriented conjuration for orchestrating families of related orchard enchantments without specifying their concrete implementations.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Abstract Factory
ARCANUM SCHOOL: Conjuration // Orchardmancy
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// SWIFT PATTERN //
protocol AppleEnchantment {
    func cast() -> String
}
protocol TreeEnchantment {
    func cast() -> String
}
protocol OrchardFactory {
    func createAppleEnchantment() -> AppleEnchantment
    func createTreeEnchantment() -> TreeEnchantment
}

class PoisonAppleEnchantment: AppleEnchantment {
    func cast() -> String { return "Casting poison on the apple..." }
}
class PoisonTreeEnchantment: TreeEnchantment {
    func cast() -> String { return "Blighting the tree..." }
}
class DarkOrchardFactory: OrchardFactory {
    func createAppleEnchantment() -> AppleEnchantment { return PoisonAppleEnchantment() }
    func createTreeEnchantment() -> TreeEnchantment { return PoisonTreeEnchantment() }
}

The Abstract Factory: Orchard Conjuration

In the mystical groves of Apple programming, the Abstract Factory is a protocol-oriented spell. It allows a magus to summon related families of enchantments—such as those affecting apples and trees—without binding their spellbooks to concrete incantations.

Using Swift’s powerful protocols, we define the essence of the enchantments. Concrete factories, such as the DarkOrchardFactory, provide the specific implementations.

Runic Tags: #swift #design-pattern #creational