🔴 SCALA ALCHEMY DIFFICULTY: Apprentice Behavioral

The Command Word

Encapsulate a complete magical evocation as a first-class object that can be stored or delayed.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Command
ARCANUM SCHOOL: Evocation // Invocation
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// SCALA PATTERN //
trait Command { def execute(): Unit }

class Golem {
  def walk(): Unit = println("Golem steps forward.")
  def smash(): Unit = println("Golem smashes target.")
}

case class WalkCommand(golem: Golem) extends Command {
  def execute(): Unit = golem.walk()
}

case class SmashCommand(golem: Golem) extends Command {
  def execute(): Unit = golem.smash()
}

class RitualSequence {
  private var history = List.empty[Command]

  def invoke(command: Command): Unit = {
    command.execute()
    history = history :+ command
  }
}

The Command pattern captures intents. By storing executions in an immutable list, one could even implement a Chronamancy (Undo) function effortlessly.

Runic Tags: #scala #behavioral #evocation #delayed-magic