Command in Elm
Encapsulating intents as Custom Types within The Elm Architecture.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Command
ARCANUM SCHOOL: Enchantment // Intent Binding
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
module Command exposing (Msg(..), update)
-- The Command Object is simply the Msg Custom Type!
type Msg
= UploadVirus String
| BypassFirewall Int
| AbortSequence
type alias Model =
{ status : String, activeConnections : Int }
-- The Invoker and Receiver are merged into the Update function
update : Msg -> Model -> Model
update msg model =
case msg of
UploadVirus payload ->
{ model | status = "Uploading " ++ payload ++ "..." }
BypassFirewall port ->
{ model | status = "Bypassing port " ++ String.fromInt port, activeConnections = model.activeConnections + 1 }
AbortSequence ->
{ model | status = "Sequence Aborted.", activeConnections = 0 }
The Command: The Sigils of Intent
In Object-Oriented paradigms, the Command pattern requires heavy boilerplate to wrap actions into objects. In Elm, the Command pattern is the very lifeblood of the architecture. Every variant in the Msg Custom Type is a perfect Command. It encapsulates the intent and required data. The update function acts as the universal invoker, unpacking the sigil and applying the pure state mutation across the UI matrix.
Runic Tags:
#elm
#behavioral
#command
#tea
#custom-types