The Bridge pattern decouples an abstraction from its implementation, enabling them to vary independently. The Bridge pattern is useful when a new version of software is being developed which will replace an existing version, but the older version must still run for its existing clients.
Role players for the Bridge pattern are:
Abstraction The interface that the client sees
Operation A method that is called by the client
BridgeAn interface defining those parts of the Abstraction that might vary
ImplementationA and ImplementationB Implementations of the Bridge interface
OperationImp A method in the Bridge that is called from the Operation in the Abstraction
1. The Bridge pattern provides an alternative to inheritance when there is more than one version of abstraction. In the UML diagram above, two implementations, A and B, implement an interface called the Bridge. The Abstraction includes an attribute of type Bridge but is not otherwise in a relationship with the implementations.
2. You must have noticed with the Decorator pattern, there can be several implementations for the one abstraction. Legacy implementations do not have to implement the Bridge if they are still going to be instantiated in the old way; they need to do so only if they must be used interchangeably with the new implementations.
Bridge is a very simple, but very powerful pattern. Given a single implementation, we can add a second one together with a Bridge and an Abstraction and achieve considerable generality over the original design.
A well-quoted use of the Bridge pattern is in graphics, where different displays have different capabilities and drivers. These would be the implementations of the Bridge pattern, and the Bridge would be an interface of their essential capabilities. The Client calls the Abstraction to display something, and the Abstraction can examine the properties of the one or more Bridge instances (drivers) it is holding and select the most appropriate one for the task.
Use the Bridge pattern when you want to:
â?¢ Completely hide implementations from clients.
â?¢ Avoid binding an implementation to an abstraction directly.
â?¢ Change an implementation without even recompiling an abstraction.
â?¢ Combine different parts of a system at runtime.
Use the Bridge pattern when you can:
â?¢ Identify that there are operations that do not always need to be implemented in the same way.