The Memento Pattern in VBA
Capture the ephemeral soul of an object, storing it safely in a phylactery so that it may be resurrected later.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Memento
ARCANUM SCHOOL: Necromancy // Spreadsheet Necromancy
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// VBA PATTERN //
' Class: CellPhylactery (Class Module) - The Memento
Public StoredValue As Variant
Public StoredColor As Long
' Class: CellEntity (Class Module) - The Originator
Private pTarget As Range
Public Sub Bind(target As Range)
Set pTarget = target
End Sub
Public Sub Mutate(val As Variant, color As Long)
pTarget.Value = val
pTarget.Interior.Color = color
End Sub
Public Function SaveSoul() As CellPhylactery
Dim phylactery As New CellPhylactery
phylactery.StoredValue = pTarget.Value
phylactery.StoredColor = pTarget.Interior.Color
Set SaveSoul = phylactery
End Function
Public Sub RestoreSoul(phylactery As CellPhylactery)
pTarget.Value = phylactery.StoredValue
pTarget.Interior.Color = phylactery.StoredColor
End Sub
' Client
Public Sub NecromanticUndo()
Dim entity As New CellEntity
entity.Bind Sheet1.Range("A1")
entity.Mutate "Pure", RGB(255, 255, 255)
' Save state
Dim backup As CellPhylactery
Set backup = entity.SaveSoul()
' Corrupt
entity.Mutate "Corrupted", RGB(0, 0, 0)
' Restore
entity.RestoreSoul backup
End Sub
The Memento: The Phylactery
When a necromancer experiments with highly destructive data mutations, there must always be a path back from the abyss. The Memento pattern allows you to capture an object’s internal state without exposing its private fields to the world.
By generating a CellPhylactery (the Memento), the CellEntity (the Originator) hands off a snapshot of its soul to a Caretaker object. If the subsequent rituals go awry and the data is corrupted beyond recognition, the entity can inhale the phylactery, instantly restoring its previous form. It is the ultimate safeguard against permanent destruction.
Runic Tags:
#vba
#memento
#behavioral
#spreadsheet-necromancy