✨ ARCANUM RUNE DIFFICULTY: Apprentice Behavioral

The Observer Hex

Define a one-to-many dependency between objects so that when one changes state, all its dependents are notified.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Observer
ARCANUM SCHOOL: Divination // Sympathetic Binding
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
use fmt;

type Observer = struct {
	update: *fn(o: *Observer, message: str) void,
	name: str,
};

fn observer_update(o: *Observer, message: str) void = {
	fmt::printfln("Familiar {} sensed: {}", o.name, message)!;
};

type Subject = struct {
	observers: [2]*Observer,
	notify: *fn(s: *Subject, message: str) void,
};

fn subject_notify(s: *Subject, message: str) void = {
	for (let i = 0z; i < len(s.observers); i += 1) {
		let obs = s.observers[i];
		obs.update(obs, message);
	};
};

export fn main() void = {
	let obs1 = Observer { update = &observer_update, name = "Raven" };
	let obs2 = Observer { update = &observer_update, name = "Cat" };
	
	let sub = Subject { observers = [&obs1, &obs2], notify = &subject_notify };
	
	sub.notify(&sub, "A disturbance in the ether.");
};

Through sympathetic binding, the Observer hex links a master node to its familiars. When the node pulses with new energy, the familiars automatically shiver in response.

Runic Tags: #behavioral #observer #simple-systems-hexes