Design Patterns

Bridge



Design Pattern Type: Structural

GoF Statement of Intent:


Decouple an abstraction from its implementation so that the two can vary independently.
Brief Overview:



Bridge decouples (separated) an abstraction from its implementation so the two can vary independently. A class can be thought of as the implementation while the behavior it exhibits (what it can do) is the abstraction. The bridge can also be thought of as two layers of abstraction.
UML Diagram:

Discussion and In-class Example:

Suppose you want to create an application using a GUI, but would like to be able to easily port it from one platform type to another. For the Window class you would need a subclass for each platform to handle the specifics. The problem is that this gets messy in a hurry as you have many different types of windows, and you would have to change the abstract Window class and/or subclass each time a specific platform changed.

The solution is to create an OO design that allows you to vary the implementation and the abstraction as shown in the diagram below. Now we can vary both. The relationship between the two is called the Bridge.