🕊️ SWIFT ENCHANTMENTS DIFFICULTY: Apprentice Behavioral

The Template Method Pattern

Defining the skeleton of an arcane ritual, allowing subclasses to override specific steps.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Template Method
ARCANUM SCHOOL: Enchantment // Rituals
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// SWIFT PATTERN //
protocol Ritual {
    func prepareIngredients()
    func chant()
    func execute()
}
extension Ritual {
    func performRitual() {
        prepareIngredients()
        chant()
        execute()
    }
}
class SummonFamiliar: Ritual {
    func prepareIngredients() { print("Gathering herbs") }
    func chant() { print("Chanting summoning words") }
    func execute() { print("Familiar appears!") }
}

The Template Method: The Standard Ritual

The Ritual protocol uses an extension to define the performRitual Template Method. Concrete rituals like SummonFamiliar only need to provide the specific steps.

Runic Tags: #swift #design-pattern #behavioral