The Memento
Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.
β§ GANG OF FOUR LINEAGE & ARCANUM β§
GOF DESIGN PATTERN: Memento
ARCANUM SCHOOL: Necromancy // Soul-anchoring
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// EXECUTABLE GO PATTERN //
package memento
import "fmt"
// Memento
type SoulAnchor struct {
health int
mana int
}
// Originator
type Lich struct {
Health int
Mana int
}
func (l *Lich) CreateAnchor() *SoulAnchor {
return &SoulAnchor{health: l.Health, mana: l.Mana}
}
func (l *Lich) Restore(anchor *SoulAnchor) {
l.Health = anchor.health
l.Mana = anchor.mana
}
// Caretaker
type Phylactery struct {
anchors []*SoulAnchor
}
func (p *Phylactery) Save(anchor *SoulAnchor) {
p.anchors = append(p.anchors, anchor)
}
func (p *Phylactery) Undo(lich *Lich) {
if len(p.anchors) == 0 { return }
last := p.anchors[len(p.anchors)-1]
p.anchors = p.anchors[:len(p.anchors)-1]
lich.Restore(last)
}
The Memento
Immortality is just a matter of state management. A Lich does not simply regenerate; they revert to a previously saved Soul Anchor stored securely in their Phylactery. The Memento pattern captures the Lichβs exact HP and Mana without exposing the delicate dark-matter variables to the outside world, allowing for a perfect rollback when their physical shell is compromised.
Runic Tags:
#Behavioral
#Necromancy
#Go