Command
Encapsulate a request as an object, allowing logging, queuing, and undoing of spells.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Command
ARCANUM SCHOOL: Evocation // Will-Binding
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CONCURRENT ACTOR-MODEL ALCHEMY //
defmodule Tekromancy.Command do
@callback execute(map) :: map
@callback undo(map) :: map
end
defmodule Tekromancy.HealCommand do
@behaviour Tekromancy.Command
def execute(state), do: %{state | hp: state.hp + 20}
def undo(state), do: %{state | hp: state.hp - 20}
end
defmodule Tekromancy.Invoker do
def run_commands(initial_state, commands) do
Enum.reduce(commands, initial_state, fn cmd, state -> cmd.execute(state) end)
end
end
By crystallizing an intent into a behaviour-bound module, the Command pattern decouples the invocation of a spell from its target. In a functional paradigm, commands operate on a state map, transforming it sequentially. This allows entire timelines of actions to be executed or reversed, altering the fate of the Hive.
Runic Tags:
#elixir
#behavioral
#command
#state-machines
#undo