The State: Phases of the Moon
Altering behavior based on internal state shifts.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: State
ARCANUM SCHOOL: Divination // Phase Shifting
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// R PATTERN //
library(R6)
# State Interface
MoonPhase <- R6Class("MoonPhase",
public = list(
cast_spell = function(context) stop("Not implemented")
)
)
# Concrete States
FullMoon <- R6Class("FullMoon", inherit = MoonPhase,
public = list(
cast_spell = function(context) {
"Casting with maximum power!"
}
)
)
NewMoon <- R6Class("NewMoon", inherit = MoonPhase,
public = list(
cast_spell = function(context) {
"Casting subtly from the shadows."
}
)
)
# Context
Spellcaster <- R6Class("Spellcaster",
private = list(
state = NULL
),
public = list(
initialize = function(state) {
self$transition_to(state)
},
transition_to = function(state) {
private$state <- state
},
execute = function() {
private$state$cast_spell(self)
}
)
)
The algorithm behaves entirely differently depending on its state (the phase of the moon), cleanly encapsulating state-specific logic.
Runic Tags:
#R6
#Divination
#Behavioral
#State