Bridge Pattern in VB.NET


Bridge Pattern is commonly known as Handle/Body idiom in C++ community. This pattern is used for decoupling an abstraction from its implementation so that the two can vary independently. 

The Gang Of Four (GoF) defined the Bridge pattern as follows in their most famous book "Design Patterns" Gamma et al., Addison-Wesley, ISBN:0-201-63361-2" - "Decouple an abstraction from it's implementation so that the two can vary independently." 

Non-software Example

A household switch controlling lights, ceiling fans, etc. is an example of the Bridge. The purpose of the switch is to turn a device on or off. The actual switch can be implemented as a pull chain, simple two-position switch, or a variety of dimmer switches.

The structure of a Bridge Pattern is shown below. 


The Abstraction defines the interface that the client uses for interaction with this abstraction. That is, the client makes requests directly to the Abstraction object, as that is the only interface known to the client. The Abstraction object also maintains a reference to an Implementer object. The collaboration between the objects in this pattern is such that the client's requests are forwarded by the abstraction to the Implementer through this reference. A RefinedAbstraction, then, is simply any and all extensions to the Abstraction class. The Implementer defines the interface for any and all implementations of the Abstraction. 

Suppose we have to develop an Image Viewer application to view BMP files under Windows OS. At the same time, we have to extend it to view other image formats like JPEG, JPG etc under Windows OS. 

The class structure for the above scenario may be like.


Where Image is an abstract class or interface and BMPImage, JPGImage & JPEGImage are concrete classes.  Now we have to modify the class hierarchy so that we have to use the Image for other OS like LINUX or UNIX. With the above class hierarchy it is pretty difficult. But by decoupling the abstraction and implementation we can achieve this as shown below.


The client always uses an instance of Image. Depending on the OS, the client can configure Image sub classes with a concrete ImgImp object. 

The bridge design pattern lets the abstraction and its implementation evolve separately.

VB.NET Implementation

' Structural Pattern:BRIDGE
' Author: [email protected]
Imports System
'Base class for OS implemenations
Interface ImageImp
Sub DoPaint(ByVal str As String)
End Interface 'ImageImp
'Windows specific implemenation
Class WinImp
Inherits ImageImp
Public Sub DoPaint(ByVal str As String)
Console.WriteLine((str + " WIN OS"))
End Sub 'DoPaint
End Class 'WinImp
'Abstract class for all image paintings
Class Image
Public Sub SetImageImp(ByVal ip As ImageImp)
impToUse = ip
End Sub 'SetImageImp
Public Overridable Sub Method(ByVal s1 As String)
End Sub 'Method
Protected impToUse As ImageImp
End Class 'Image
'BMP specific paintings
Class BMPImage
Inherits Image
Public Overrides Sub Method(ByVal s1 As String)
Dim s2 As String = s1 + " BMP IMAGE"
End Sub 'Method
End Class 'BMPImage
Class MyPaint
Public Function SetUpMethod() As Image
Dim im = New BMPImage ' BMP IMAGE
Dim win = New WinImp ' WIN OS
Return im
End Function 'SetUpMethod
End Class 'MyPaint
Class MyMain
Public Shared Sub Main()
Dim mp As New MyPaint
Dim im As Image = mp.SetUpMethod()
End Sub 'Main
End Class 'MyMain