> Design Patterns > Bridge Design Pattern

Bridge Design Pattern

Bridge design pattern is used to have interface and implementation independently in separate hierarchies.

Intent

  • Decouple an abstraction from its implementation so that the two can vary independently.

The intent of bridge design pattern is to make abstraction and implementation independent.

Problem

  • Implementation classes derived from abstract class makes difficult to modify, extend and adding of new implementation classes.

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.

Solution

  • 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 bridge design pattern is applicable?

  • Binding between abstraction and implementation has to be avoided.
  • To reduce further subclassing by making abstraction and implementation in separate hierarchy.

Bridge Design Pattern UML Structure

Bridge Design Pattern UML Structure

Participant classes of bridge design pattern

  • 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.

Bridge Design Pattern Example

Here is the bridge design pattern example.

To manage the system operations, we have to manage systems operations and applications operations.

Bridge Design Pattern Example

  • 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.

Implementation Code

C++ Bridge Pattern in C++ Bridge Pattern C++ Example
C# Bridge Pattern in C# Bridge Pattern C# Example
Java Bridge Pattern in Java Bridge Pattern Java Example
Python Bridge Pattern in Python Bridge Pattern Python Example
JavaScript Bridge Pattern in JavaScript Bridge Pattern JavaScript Example
PHP Bridge Pattern in PHP Bridge Pattern PHP Example
Ruby Bridge Pattern in Ruby Bridge Pattern Ruby Example
Swift Bridge Pattern in Swift Bridge Pattern Swift Example
Objective-C Bridge Pattern in Objective-C Bridge Pattern Objective-C Example


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 Bridge Design Pattern is from his Design Patterns course.