Bridge Design Pattern Explained

By | September 1, 2015

This tutorial is a C++ implementation of the Bridge design pattern. This is one of the most confusing design pattern. This and the other patterns, namely Adapter, Proxy and Decorator design pattern have too much in common to confuse a simple soul. As usual I will first discuss the WHY and then later on the HOW.

WHY
If you have a background in Mathematics then you must have heard of Permutations and Combinations. I would have loved to explain it here but the fact is that I am bad at Maths. So I will limit myself to saying the Permutations is all about multiplying and Combinations is all about adding. (Yes that is the level of my Maths)

6 * 6 = 36 permutations.
6 + 6 = 12 combinations.

If the count increases even by a one:

6 * 7 = 42 permutations
6 + 7 = 13 combinations.

Permutations increase explosively (what’s the maths term for it? Exponential? Won’t risk using it. Do not know much). Combinations increase at a much more manageable pace.

Suppose you are a car maker. And you have 5 different models of cars. And you make them in 5 different colours. Now go about writing classes for each type car you make. Immediately you will realize that you need 5 * 5 = 25 such classes !!! Yikes !!
Like this:

Model_A_Color_red, Model_A_Color_blue, Model_A_Color_yellow, Model_A_Color_black, Model_A_Color_pink
Model_B_Color_red, Model_B_Color_blue, Model_B_Color_yellow, Model_B_Color_black, Model_B_Color_pink
Model_C_Color_red, Model_C_Color_blue, Model_C_Color_yellow, Model_C_Color_black, Model_C_Color_pink
Model_D_Color_red, Model_D_Color_blue, Model_D_Color_yellow, Model_D_Color_black, Model_D_Color_pink
Model_E_Color_red, Model_E_Color_blue, Model_E_Color_yellow, Model_E_Color_black, Model_E_Color_pink

And suppose you introduce one more model of car, Model_F. Now you have 30 classes !!!
One more colour into the mix and you have 36 classes to code !!!

Bridge pattern breaks these Permutations into Combinations.

So instead of 5 * 5 = 25 classes, you have 5 + 5 = 10 classes.
Add another model and you have 6 + 5 = 11 classes.
Add another colour and you have 6 + 6 = 12 classes.

This is the gist of Bridge pattern. Take two independently varying items (in our case: car model and car colour) and separate them. Then “COMBINE” them to get what you want.

Book recommendation
If you want a deep dive I recommend the book C# 3.0 Design Patterns: Use the Power of C# 3.0 to Solve Real-World Problems. I have read this book and highly recommend if over others. C# might seem out of place since the tutorial is having C++ code samples. But design problems essentially are beyond the language semantics. I always have loved books which deal with real world problems rather than foo and bar.

HOW
We will continue with the same example.
For the colour we will define an Interface called IColor. It will have a method called Color which will return the color. For the car model, we will define ICarModel. It will have a method WhatIsMyType() which will return the information about the car.

Here is the code.

#include<iostream>
#include<string>

using namespace std;

class IColor
{
public:
    virtual string Color() = 0;
};

class RedColor : public IColor
{
public:
    string Color()
    {
        return "of Red Color";
    }
};

class BlueColor : public IColor
{
public:
    string Color()
    {
        return "of Blue Color";
    }
};

class ICarModel
{
public:
    virtual string WhatIsMyType() = 0;
};

class Model_A : public ICarModel
{
    IColor* _myColor;
public:
    Model_A(IColor *obj) :_myColor(obj){}
    string WhatIsMyType()
    {
        return "I am a Model_A " + _myColor->Color();
    }
};

class Model_B : public ICarModel
{
    IColor* _myColor;
public:
    Model_B(IColor *obj) :_myColor(obj){}
    string WhatIsMyType()
    {
        return "I am a Model_B " + _myColor->Color();;
    }
};

int main()
{
    IColor* red = new RedColor();
    IColor* blue = new BlueColor();

    ICarModel* modelA = new Model_B(red);
    ICarModel* modelB = new Model_A(blue);

    cout << "\n" << modelA->WhatIsMyType();
    cout << "\n" << modelB->WhatIsMyType() << "\n\n";

    delete red;
    delete blue;
    return 1;
}

Here are the important lines:

  • Line 06 : We declare the interface IColor. This is one of the things which changes.
  • Line 09 : The method which will be returning the color in the implementing classes.
  • Line 12 : The RedColor class implementing the interface.
  • Line 21 : The BlueColor class implementing the interface.
  • Line 30 : The interface for the Car models. This is the other thing which changes.
  • Line 33 : The method which will return the type of the model.
  • Line 36 : The Model_A class implementing the interface.
  • Line 38 : A pointer to IColor is present in the class. This will get initialized in the constructor.
  • Line 40 : The constructor where the initialization takes place. The object of this class will be created using an instance of type IColor.
  • Line 47 : The Model_B class implementing the interface.
  • Line 60 – 64 : The client declaring and initializing an instances of the classes.
  • Line 66-67 : The method gets called.

And this is the output:

Bridge Design Pattern

I will sign off with providing a link to the stackoverflow answer regarding difference between the Proxy, Adapter, Decorator and Bridge pattern.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.