🕊️ SWIFT ENCHANTMENTS DIFFICULTY: Adept Structural

The Proxy Pattern

A magical surrogate controlling access to a powerful artifact.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Proxy
ARCANUM SCHOOL: Abjuration // Warding
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// SWIFT PATTERN //
protocol Grimoire {
    func readSpells()
}
class AncientGrimoire: Grimoire {
    func readSpells() { print("Reading forbidden spells") }
}
class GrimoireProxy: Grimoire {
    private lazy var realGrimoire = AncientGrimoire()
    private let isAuthorized: Bool
    init(isAuthorized: Bool) { self.isAuthorized = isAuthorized }
    func readSpells() {
        if isAuthorized { realGrimoire.readSpells() }
        else { print("Access denied by the magical ward") }
    }
}

The Proxy: The Guarded Grimoire

The Proxy pattern ensures that an AncientGrimoire is only instantiated and accessed by those with the proper arcane authorization.

Runic Tags: #swift #design-pattern #structural