The Command Hex
Encapsulating a request as an object, letting you parameterize clients with closures.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Command
ARCANUM SCHOOL: Enchantment // Invocation
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// GROOVY PATTERN //
class Drone {
void fly() { println "Drone takes flight" }
void land() { println "Drone lands" }
}
class Controller {
List<Closure> history = []
void executeCommand(Closure command) {
command()
history << command
}
void replay() {
println "Replaying commands..."
history.each { it() }
}
}
def drone = new Drone()
def remote = new Controller()
// Commands as simple closures
remote.executeCommand { drone.fly() }
remote.executeCommand { drone.land() }
remote.replay()
The Command Hex
In Java, the Command hex requires an interface and multiple concrete classes. In Groovy, a Command is simply a Closure. By passing closures around, a mage encapsulates entire execution trees as first-class objects, storing them in arrays for delayed execution, queuing, or historical replays of drone maneuvers.
Runic Tags:
#groovy
#behavioral
#command
#closures