The Chain of Responsibility Pattern
Passing an arcane request along a chain of magical handlers.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Chain of Responsibility
ARCANUM SCHOOL: Evocation // Channeling
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// SWIFT PATTERN //
protocol SpellHandler {
var next: SpellHandler? { get set }
func handle(spellLevel: Int)
}
class ApprenticeHandler: SpellHandler {
var next: SpellHandler?
func handle(spellLevel: Int) {
if spellLevel <= 1 { print("Apprentice handles spell") }
else { next?.handle(spellLevel: spellLevel) }
}
}
class ArchmageHandler: SpellHandler {
var next: SpellHandler?
func handle(spellLevel: Int) {
print("Archmage handles spell")
}
}
The Chain of Responsibility: The Circle of Magi
Spells of varying magnitudes are channeled through the hierarchy. If the Apprentice cannot handle a high-level spell, it passes to the Archmage.
Runic Tags:
#swift
#design-pattern
#behavioral