Bridge Design Pattern

This is structural design pattern.

The bridge pattern is a design pattern that separates the abstract elements of a class from its technical implementation. This provides a cleaner implementation of real-world objects and allows the implementation details to be changed easily.

The bridge pattern is used to separate the abstract elements of a class from the implementation details. For example, the abstract elements may be the business logic of an application. They can be created without any knowledge of the implementation details of their data access or interoperability with the operating system. The pattern provides the means to replace the implementation details without modifying the abstraction. This permits, for example, changing operating systems, databases, etc. with no impact to the business logic.

Example: In automate message sending application there are mainly 3type of mode to send message phone call, sms & email. There is no issue if there are less number of sending mode. But the problem generate if we want to support multiple platform Windows, Linux & IOS. In each platform there are 3 different options. So total number of mode of operation are for 3 platform are 9. It will be very difficult to maintain if a new platform added or a new mode is added. To solve this bridge pattern is used.



 

#include<iostream>
using namespace std;

class SendMode
{
    public:
        virtual void Send()=0;
};

class ByPhone : public SendMode
{
    public:
        void Send()
        {
            cout<<"Inform by Phone call"<<endl;
        }
};

class ByMessage : public SendMode
{
    public:
        void Send()
        {
            cout<<"Inform by Message"<<endl;
        }
};

class ByEmail : public SendMode
{
    public:
        void Send()
        {
            cout<<"Inform by Email"<<endl;
        }
};


class OS
{
    protected:
        SendMode *mode;
    public:
        virtual void SendMessage()=0;
};


class Linux : public OS
{
    public:
        Linux(SendMode *m)
        {
            mode = m;
        }
        void SendMessage()
        {
            cout<<"Message Send through Linux : ";
            mode->Send();
        }
};

class Windows : public OS
{
    public:
        Windows(SendMode *m)
        {
            mode = m;
        }
        void SendMessage()
        {
            cout<<"Message Send through Windows : ";
            mode->Send();
        }
};

class IOS : public OS
{
    public:
        IOS(SendMode *m)
        {
            mode = m;
        }
        void SendMessage()
        {
            cout<<"Message Send through IOS : ";
            mode->Send();
        }
};


int main()
{
    SendMode *m = new ByPhone;
    Linux l(m);
    l.SendMessage();
    return 0;
}

Comments

Popular posts from this blog

STL Questions

Producer Consumer problem using mutex

Interview questions