The Command of the Progenitor
Encapsulate incantations as first-class closures to be executed at will.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Command
ARCANUM SCHOOL: Enchantment // Delayed Invocation
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
type command = unit -> unit
fun createHeal target amount () =
print ("Healing " ^ target ^ " for " ^ Int.toString amount ^ " HP.\n")
fun createDamage target amount () =
print ("Striking " ^ target ^ " for " ^ Int.toString amount ^ " damage.\n")
structure Invoker = struct
val queue : command list ref = ref []
fun store cmd = queue := !queue @ [cmd]
fun executeAll () =
(app (fn c => c ()) (!queue);
queue := [])
end
(* Usage *)
val _ = Invoker.store (createHeal "Ally" 50)
val _ = Invoker.store (createDamage "Goblin" 100)
val _ = Invoker.executeAll ()
In the object-oriented world, the Command pattern requires complex classes to encapsulate actions. The ML Progenitor favors closures. By defining a command as a unit -> unit function, we encapsulate the receiver, the method, and the arguments within the closure’s environment. These functions can be stored in lists, passed around, and evaluated when the arcane timing is perfect.
Runic Tags:
#closures
#higher-order functions
#lazy evaluation