Bridge design pattern in C++ with real life example

As we know, there are three types of design patterns such as creational, structural ad behavioral but the first thing to remember that a bridge design pattern is a structural design pattern

Intent

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

Abstraction: is an interface that provides the non specific implementation of the logic

Implementation: implementation of the abstraction, which is responsible for low-level work

Why we use a bridge design pattern?

As we know through abstraction we can have several ways to implement it and we use inheritance to make it possible but it is not flexible enough

The problem with inheritance is that it binds abstraction and implementation permanently together because of that it becomes difficult to modify abstraction and implementation independently  or reuse it or extend it

Example

Suppose you have a family class which is an abstraction or interface through which you have inherited father, mother, and toddler

All the family members have their way of movement logic such as father goes to the office, mother stay at home

Each family member have different movement logic

Abstraction                                implementation

Family                                        move logic

Father                                         drive car

                                                     + go to the office

                                                     + go to the meeting

Mother                                      stay at home

                                                   +cook food

Toddler                                      stay at the cradle

                                                    +crawl

Structure

Rendered by QuickLaTeX.com

Participants

Abstraction(Family)

Refined abstraction(father,mother,toddler)

Implementor(move logic)

Concrete implementor(Drive car, stay at cradle)

#include<iostream>
#include<string>
using namespace std;
class MoveLogic
{
public:
    virtual void move() = 0;
};
class Drivecar : public MoveLogic
{
public:
    void move()
    {
        cout << "Go to office\n";
    }
};
class Stayatcradle : public MoveLogic
{
public:
    void move()
    {
        cout << "Crawl\n";
    }
};
class Family
{
public:
    virtual void howDoIMove() = 0;
};
class Father : public Family
{
    MoveLogic* _myMoveLogic;
public:
    Father(MoveLogic *obj) :_myMoveLogic(obj){}
    void howDoIMove()
    {
        _myMoveLogic->move();
    }
};
class Toddler : public Family
{
    MoveLogic* _myMoveLogic;
public:
    Toddler(MoveLogic *obj) :_myMoveLogic(obj){}
    void howDoIMove()
    {
        _myMoveLogic->move();
    }
};
int main()
{
    MoveLogic* drivecar = new Drivecar();
    MoveLogic* stayatcradle  = new Stayatcradle();
    Family* FamilyA = new Father(drivecar);
    Family* FamilyB = new Toddler(stayatcradle);
    FamilyA->howDoIMove();
    FamilyB->howDoIMove();
    return 1;
}

Output

Go to office
Crawl

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.