CoursesGalaxy Courses Subscription Knowledge Hub
Knowledge Hub
Design Patterns
    Creational Patterns
    Structural Patterns
        Adapter
        Bridge
        Composite
        Decorator
        Facade
        Flyweight
        Proxy
    Behavioral Patterns


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 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 it is applicable?
  • Binding between abstraction and implementation has to be avoided.
  • To reduce further subclassing by making abstraction and implementation in separate hierarchy.
Structure

Bridge Design Pattern

Participant classes
  • 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.
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#   Example in C#
Java   Bridge Pattern in Java   Example in Java
Python   Bridge Pattern in Python   Example in Python
JavaScript   Bridge Pattern in JavaScript   Example in JavaScript
PHP   Bridge Pattern in PHP   Example in PHP
Ruby   Bridge Pattern in Ruby   Example in Ruby
Swift   Bridge Pattern in Swift   Example in Swift
Objective-C   Bridge Pattern in Objective-C   Example in Objective-C



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.







Yearly Subscription


Design Patterns in Ruby


Linux Bootcamp

Explore

Courses

Subscription

Knowledge Hub

Design Patterns

Tech Articles

About Us

Overview

Contact Us

CourseGalaxy

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.

Learn From Us

2018 CourseGalaxy