The Decorator Pattern in VBA
Wrap an object in dark enchantments dynamically, adding behaviors without altering the core structure.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Decorator
ARCANUM SCHOOL: Transmutation // Spreadsheet Necromancy
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// VBA PATTERN //
' Interface: ICellFormatter (Class Module)
Public Sub ApplyFormat(target As Range)
End Sub
' Class: BaseFormatter (Class Module)
Implements ICellFormatter
Private Sub ICellFormatter_ApplyFormat(target As Range)
target.Value = "Raw Soul"
End Sub
' Class: BloodRedDecorator (Class Module)
Implements ICellFormatter
Private pWrappee As ICellFormatter
Public Sub Init(wrappee As ICellFormatter)
Set pWrappee = wrappee
End Sub
Private Sub ICellFormatter_ApplyFormat(target As Range)
' Execute base
pWrappee.ApplyFormat target
' Add new behavior
target.Interior.Color = RGB(139, 0, 0)
End Sub
' Client
Public Sub DecorateCell()
Dim base As ICellFormatter
Set base = New BaseFormatter
Dim decorated As New BloodRedDecorator
decorated.Init base
decorated.ICellFormatter_ApplyFormat Sheet1.Range("A1")
End Sub
The Decorator: Layers of the Shroud
Subclassing in VBA is impossible, but the Decorator pattern provides a powerful alternative for adding functionality dynamically. Instead of creating a massive class with dozens of boolean flags for formatting options, you create discrete wrapper classes.
By passing a core ICellFormatter into a BloodRedDecorator, you wrap the object in a new layer of logic. This is the art of weaving a dark shroud: each layer adds its own curse to the target cell, stacking effects without modifying the underlying construct.
Runic Tags:
#vba
#decorator
#structural
#spreadsheet-necromancy