Bridge design pattern is used to have interface and implementation independently in separate hierarchies.
Decouple an abstraction from its implementation so that the two can vary independently.
The intent is to make abstraction and implementation independent.
Implementation classes derived from abstract class makes difficult to modify, extend and adding of new implementation
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.
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.
So the abstraction and implementation will be independent and will have their own hierarchy.
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.
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.
How they work together?
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.
CourseGalaxy is focused on transformation of Engineering graduate to Software Engineer to fill the gap
between education and industry and provide the training on specific problems of industry to enhance the skills
of Engineers in industry to do their job appropriately.