The Bridge Pattern in VBA
Sever the ties between an abstraction and its implementation, letting both evolve independently in the void.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Bridge
ARCANUM SCHOOL: Illusion // Spreadsheet Necromancy
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// VBA PATTERN //
' Interface: IRenderer (Class Module)
Public Sub DrawData(data As String)
End Sub
' Class: ChartRenderer (Class Module)
Implements IRenderer
Private Sub IRenderer_DrawData(data As String)
Debug.Print "Plotting data on an ethereal Chart: " & data
End Sub
' Class: GridRenderer (Class Module)
Implements IRenderer
Private Sub IRenderer_DrawData(data As String)
Debug.Print "Writing data directly to the cell grid: " & data
End Sub
' Abstract Class: DataView (Class Module)
' VBA doesn't have abstract classes, so we simulate it
Public Renderer As IRenderer
Public Sub Initialize(r As IRenderer)
Set Renderer = r
End Sub
Public Sub Display(data As String)
' To be overridden / implemented
End Sub
' Class: FinancialView (Class Module)
' Extends DataView concept
Private pView As New DataView
Public Sub Initialize(r As IRenderer)
pView.Initialize r
End Sub
Public Sub ShowFinancials()
pView.Renderer.DrawData "Profit: -100 Souls"
End Sub
The Bridge: Severing the Astral Cord
The Bridge pattern detaches an abstraction from its implementation so that the two can vary independently. In the dark arts of VBA, this prevents the explosive proliferation of classes.
Imagine you have two ways to view data (Financial and Operational) and two ways to render it (Grid and Chart). Without the Bridge, you would need four classes (FinancialGrid, FinancialChart, etc.). By bridging the View abstraction to a Renderer implementation, you sever the astral cord, keeping your grimoire lean and infinitely extensible.
Runic Tags:
#vba
#bridge
#structural
#spreadsheet-necromancy