⚙️ MOTOKO RUNE DIFFICULTY: Apprentice Creational

The Factory Method Hex

Delegating the instantiation of specific magical familiars to subclass logic.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Factory Method
ARCANUM SCHOOL: Conjuration // Actor Model Hexes
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// MOTOKO PATTERN //
module FactoryMethod {
  public type Familiar = {
    speak : () -> async Text;
  };

  public type Summoner = {
    summonFamiliar : () -> Familiar;
  };

  public class RavenSummoner() {
    public func summonFamiliar() : Familiar {
      object {
        public func speak() : async Text { "Nevermore." };
      }
    };
  };

  public class ImpSummoner() {
    public func summonFamiliar() : Familiar {
      object {
        public func speak() : async Text { "Your soul is mine!" };
      }
    };
  };

  public actor Grimoire {
    public func performRitual(summoner : Summoner) : async Text {
      let familiar = summoner.summonFamiliar();
      await familiar.speak();
    };
  };
}

A foundational incantation. The Factory Method defines an interface for creating an object, but leaves the choice of its type to the specific sub-classes or implementations. The Grimoire actor can perform rituals without needing to know what specific familiar it summons.

Runic Tags: #motoko #creational #factory-method #actor-model