✨ ARCANUM RUNE DIFFICULTY: Adept Behavioral

The Template Method Incantation in Carbon

Define the skeleton of a high-level ritual in an operation, deferring specific steps to subclasses.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Template Method
ARCANUM SCHOOL: Evocation // Ritual Structure
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
package TemplateMethod api;

// In Carbon, we use interfaces to define the customizable steps
interface RitualSteps {
  fn PrepareComponents[me: Self]();
  fn Ignite[me: Self]();
  fn Cleanup[me: Self]();
}

// The executor acts as the skeleton
class RitualExecutor {
  fn ExecuteRitual[me: Self](steps: RitualSteps*) {
    (*steps).PrepareComponents();
    (*steps).Ignite();
    (*steps).Cleanup();
  }
}

class FireballRitual {
  impl as RitualSteps {
    fn PrepareComponents[me: Self]() { /* gather sulfur */ }
    fn Ignite[me: Self]() { /* spark */ }
    fn Cleanup[me: Self]() { /* vent heat */ }
  }
}

The Template Method: The Skeleton Ritual

Certain high-tier spells follow a strict sequence: Prepare, Cast, Cleanup. If you allow junior adepts to write the entire ritual, they will forget the cleanup phase and leak memory. The Template Method pattern locks down the skeleton of the algorithm.

While C++ used abstract base classes and virtual functions for this, Carbon often prefers composition and interfaces. The RitualExecutor defines the immutable sequence. Specific implementations, like FireballRitual, simply provide the required RitualSteps. The Successor Pact enforces structure to prevent catastrophic meltdowns.

Runic Tags: #behavioral #carbon #inheritance #algorithms