Bridge Design Pattern
Bridge design pattern is used to have interface and implementation independently in separate hierarchies.
The intent is to make abstraction and implementation independent.
- Decouple an abstraction from its implementation so that the two can vary independently.
When we use inheritance for abstraction class and implementation class then the abstraction binds with implementation. This
makes it complex to further modify and extend. Also as more implementation classes are required with variation, then it
becomes complex to add more and more in same hierarchy as the variation may come in different forms – like OS, system etc.
- Implementation classes derived from abstract class makes difficult to modify, extend and adding of new implementation
So the abstraction and implementation will be independent and will have their own hierarchy.
- Come up with interfaces for implementation.
- Derive the implementation classes from abstract implementation class.
- Use the interfaces of implementation in concrete class of abstract class.
Where it is applicable?
- Binding between abstraction and implementation has to be avoided.
- To reduce further subclassing by making abstraction and implementation in separate hierarchy.
How they work together?
- Abstraction class provides the interface to the client and has the instance of Implementor class.
- RefinedAbstraction class is derived from Abstraction class and implements the interfaces.
- Implementor class has the interfaces implemented by implementation classes.
- ConcreteImplementorA and ConcreteImplementorB classes implements the methods of Implementor class.
- Client uses the interfaces from Abstraction class, the concrete class of Abstraction class uses the instance of
Implementor and uses its interface which is implemented by concrete class of Implemetor, and here it is
ConcreteImplementorA and ConcreteImplementorB.
- The interface and implementation are separated and both are having their own hierarchy. This makes it flexible to
extend and maintain. It also avoids subclassing explosion problem.
To manage the system operations, we have to manage systems operations and applications operations.
- Abstract class OperationManager provides interface Start() to client. The concrete classes SystemOperation and
ApplicationOperation implements the method Start().
- Abstract class OperationManagerImp provides the interface Start() for implementation. The concrete classes
SystemOperationImp and ApplicationOperationImp implements the method Start().
- The abstract class OperationManager has instance of OperationManagerImp. The concrete class SystemOperation and
ApplicationOperation uses this instance to call the Start() method of SystemOperationImp and ApplicationOperationImp.
Suresh Kumar Srivastava is founder of online learning site coursegalaxy.com and author of popular books "C In Depth",
"Data Structures Through C In Depth". He has 18+ years experience in industry and worked on architecture and design of
multiple products. This article is from his courses on design patterns.
Design Patterns in Ruby