The Template Method Hex
Defining the immutable skeleton of a grand ritual, deferring exact ingredients to subclasses.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Template Method
ARCANUM SCHOOL: Evocation // Actor Model Hexes
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// MOTOKO PATTERN //
module TemplateMethod {
public type PotionRitual = {
brew : () -> Text;
addPrimaryIngredient : () -> Text;
addCatalyst : () -> Text;
};
// In Motoko, we use object composition or higher-order functions
// to simulate Template Method inheritance.
public class AbstractRitual(
primary : () -> Text,
catalyst : () -> Text
) {
public func brew() : Text {
"Boiling water... " # primary() # " ... " # catalyst() # " ... Potion ready!";
};
};
public class HealingPotion() {
public func getRitual() : AbstractRitual {
AbstractRitual(
func() : Text { "Adding Troll Blood" },
func() : Text { "Adding Sun Drop" }
)
};
};
public actor Alchemist {
public func craftHealing() : async Text {
let potion = HealingPotion().getRitual();
potion.brew();
};
};
}
The Template Method Hex outlines the immutable steps of an algorithm—boiling water, adding ingredients, bottling—but delegates the exact implementation of the primary ingredients and catalysts to specific instances. Here, Motoko’s higher-order functions beautifully replicate inheritance.
Runic Tags:
#motoko
#behavioral
#template-method
#actor-model