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
It will change the incompatible class interface with the compatible interface
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

Suppose you have a task to charge a mobile phone but the mobile support only a square-pin charger
In case if you want to charge a mobile with a two-pin charge you can not do it
Why?
Because the mobile phone does not support a two-pin charge
So, to charge a mobile phone you need an Adapter (which is a charger) that allows you easily change the charger output from two-pin to square- pin
Structure
#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
For this adapter design pattern you can assume the client as the main function
The above program has two interfaces called mobile interface and adapter interface and two classes called mobile phone and Adapter
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
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;
}