✨ ARCANUM RUNE DIFFICULTY: Apprentice Structural

The Decorator Hex

Attach additional responsibilities to an object dynamically.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Decorator
ARCANUM SCHOOL: Transmutation // Spell Layering
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
use fmt;

type Spell = struct {
	cast: *fn(s: *Spell) void,
	base_cost: uint,
};

fn cast_base(s: *Spell) void = {
	fmt::printfln("Casting base spell. Cost: {}", s.base_cost)!;
};

type EmpoweredSpell = struct {
	inner: *Spell,
	cast: *fn(es: *EmpoweredSpell) void,
};

fn cast_empowered(es: *EmpoweredSpell) void = {
	es.inner.cast(es.inner);
	fmt::println("Adding EMP shockwave!")!;
};

export fn main() void = {
	let base = Spell { cast = &cast_base, base_cost = 10 };
	let emp = EmpoweredSpell { inner = &base, cast = &cast_empowered };
	emp.cast(&emp);
};

By layering enhancements over an existing construct, the Decorator hex prevents an explosion of sub-classes. You can wrap a mundane fireball in EMP-bursts and tracking-runes dynamically at runtime.

Runic Tags: #structural #decorator #simple-systems-hexes