Adapter design pattern in C++ with a real-life example

in a word, The adapter design pattern is a bridge that connects two incompatible components interface

The adapter helps the classes to work together usually if the class interface differ then they won’t work together

in essence, It will change the incompatible class interface with the compatible interface

Rendered by QuickLaTeX.com

Rendered by QuickLaTeX.com

to put it differently, Suppose you have designed a component1 or application1 class which have an unrelated interface or it doesn’t match the component 2 or application2

In that case, we use an adapter class to bridge the two unrelated interface to match with each other

Real-life example

this time, Well, imagine this

in reality, Suppose you have a task to charge a mobile phone

 but the problem is?

in this situation, The mobile phone support only a square-pin charger to charge it

until now, Okay

At this point, you have a two-pin charger

So, that means you have a two-pin charger which is incompatible to charge the mobile phone

as a matter of fact, Like it or not, you cannot charge the mobile phone with your two-pin charger

but Why?

Because the  mobile phone support only a square-pin charger

at this time, Is it possible To charge the mobile

on the whole, yes, Absolutely

But how?

in reality, let’s see how the adapter design pattern solves the problem of two incompatible interface

not to mention, at this instance, we will introduce an adapter interface(charger) that allows you to change the two-pin interface to a square-pin interface

So, the functionality of the adapter(charger) is that it will allow two incompatible interfaces to work together

finally, you can charge the mobile phone by using the  adapter

all in all, pretty effective right?

let’s deconstruct the words that we use during the adapter design process

in the first place, the client is your two-pin charger

in the second place, the adapter is a charger that converts the two-pin interface to square pin interface

in the third place, the adaptee is the square-pin charger

up to the present time, clear as mud?

Structure

Rendered by QuickLaTeX.com

#include <iostream>
using namespace std;
class MobileInterface                                  // Interface of Adaptee
{
public:
	virtual void Square_pin() = 0;
};
class MobilePhone : public MobileInterface             // Concrete Adaptee
{
public:
	void Square_pin() {
		cout << "The phone  only supports a Square pin charger" << endl;
	}
};
class AdapterInterface                                 // Target, the interface of Adapter which client will use
{
public:
	virtual void Two_pin() = 0;
};
class Adapter : public AdapterInterface
{
private:
	MobileInterface *mobile_interface;
public:
	Adapter(MobileInterface *obj) {
		mobile_interface = obj;
	}
	void Two_pin() {
		mobile_interface->Square_pin();
	}
};
int main()
{
	MobilePhone *mobilephone_ptr = new MobilePhone();
	AdapterInterface *adapter = new Adapter(mobilephone_ptr);
	adapter->Two_pin();
}

Output

The phone  only supports a Square pin charger

of course, for this adapter design pattern you can assume the client as the main function

for instance, the above program has two interfaces called mobile interface and adapter interface and two classes called mobile phone and Adapter

this time, The adapter class contain a function called two-pin()

Line 31: mobileinterface is taken as an input object

Line 32: using this obj you can call the function line 36

Line 42:object called mobilephone is created

Line 43:the created object is passed to the adapter

Line 43: the pointer  *adapter is pointing to an adapter class

Line 44: by using this adapter pointer you can call the function of line 35

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

1 Comment

  • Pankaj says:

    I read your post, and I loved it. Thanks. A try from my side.
    Is this a correct adapter pattern?

    class Socket {
    public:
    virtual void electricitySource() = 0;
    };

    class EuropeanSocket : public Socket {
    public:
    void electricitySource() { cout << "Electricity from EuropeanSocket socket!" << endl; }
    };

    class IndianSocket : public Socket {
    public:
    void electricitySource() { cout << "Electricity from IndianSocket socket!" <socket = socket;
    }
    void electricitySource() {
    socket->electricitySource();
    }
    };

    // European Client.
    class ElectricKettle {
    EuropeanSocket* socket;
    public:
    void plugIn(EuropeanSocket* socket) {
    this->socket = socket;
    }
    void boil() {
    socket->electricitySource();
    cout << "Kettle is ON!" <plugIn(eSocket);
    kettle->boil();

    // You travelled to India.
    // E.g. India has 3-pin socket.
    IndianSocket *iSocket = new IndianSocket;

    // Use adapter for compatibility.
    Adapter* adapter = new Adapter(iSocket);
    kettle->plugIn(adapter);

    // Let’s boil.
    kettle->boil();

    return 0;
    }

Leave a Reply

Your email address will not be published.