The Observer Incantation
A one-to-many dependency so that when a celestial body changes state, all bound artifacts are updated.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Observer
ARCANUM SCHOOL: Divination // Synchronicity
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// MATHEMATICA PATTERN //
(* The Observable Subject *)
ClearAll[CelestialBody, AddObserver, NotifyObservers, SetPhase];
CelestialBody[name_] := Module[{observers = {}, phase = "New"},
<|
"AddObserver" -> Function[obs, AppendTo[observers, obs]],
"SetPhase" -> Function[newPhase,
phase = newPhase;
Print["[", name, "] phase shifted to: ", phase];
Scan[#[phase]&, observers]
]
|>
];
(* The Observers (Functions) *)
TideWatcher[phase_] := Print["TideWatcher noted phase: ", phase, ". Adjusting tides..."];
Lycanthrope[phase_] := If[phase === "Full", Print["Lycanthrope transforms!"], Print["Lycanthrope remains calm."]];
(* Usage *)
moon = CelestialBody["Luna"];
moon["AddObserver"][TideWatcher];
moon["AddObserver"][Lycanthrope];
moon["SetPhase"]["Waxing"];
moon["SetPhase"]["Full"];
Runic Tags:
#observer
#behavioral
#callbacks
#synchronization