The State
Morph the serpent's behavior depending on its current lifecycle phase.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: State
ARCANUM SCHOOL: Transmutation // Phase Shifting
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
# pragma version ^0.3.7
enum LifecyclePhase:
EGG
HUNTING
SLUMBER
current_phase: public(LifecyclePhase)
@external
def __init__():
self.current_phase = LifecyclePhase.EGG
@external
def interact():
if self.current_phase == LifecyclePhase.EGG:
self._hatch()
elif self.current_phase == LifecyclePhase.HUNTING:
self._bite()
elif self.current_phase == LifecyclePhase.SLUMBER:
self._dream()
@internal
def _hatch():
self.current_phase = LifecyclePhase.HUNTING
@internal
def _bite():
# logic for biting
self.current_phase = LifecyclePhase.SLUMBER
@internal
def _dream():
# rest and recover
self.current_phase = LifecyclePhase.HUNTING
The State pattern implements a Finite State Machine within the contract’s core logic. The cyber-serpent presents a single interface to the world (interact), but its behavior radically shifts depending on its internal phase. It hatches if it is an egg, it bites if it is hunting, and it dreams if it is slumbering—smoothly transitioning between states without tangled conditional nightmares.
Runic Tags:
#behavioral
#state
#vyper
#finite-state-machine