The Observer Leyline
Defining a one-to-many dependency so that when a core arcane node changes state, all connected wards are notified.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Observer
ARCANUM SCHOOL: Divination // Leylines
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// FSHARP PATTERN //
open System
// Idiomatic F# uses the built-in IObservable / IObserver or Events.
type Leyline() =
let surgeEvent = Event<int>()
member _.OnSurge = surgeEvent.Publish
member _.TriggerSurge(power) =
printfn "Leyline pulsing with %d power!" power
surgeEvent.Trigger(power)
let centralLeyline = Leyline()
// Scribe the wards (Observers)
let ward1 = centralLeyline.OnSurge.Subscribe(fun p -> printfn "Ward 1 absorbed %d mana." p)
let ward2 = centralLeyline.OnSurge.Subscribe(fun p -> printfn "Ward 2 glows brightly from %d mana." p)
centralLeyline.TriggerSurge(500)
// Unbinding wards
ward1.Dispose()
centralLeyline.TriggerSurge(200)
The CLR provides first-class support for the Observer pattern through IObservable and F#’s native Event system. This allows an archmage to wire wards and familiars to leylines using pure functional subscriptions.
Runic Tags:
#behavioral
#observer
#fsharp
#events