Write a C++ Program demonstrating the working of the Adapter design pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <iostream> using namespace std; // Adaptee_ActualService class Adaptee_ActualService { public: void specificRequest() { cout << "Adaptee_ActualService's specific request." << endl; } }; // Target_ClientInterface class Target_ClientInterface { public: virtual void request() = 0; }; // Adapter class Adapter : public Target_ClientInterface, private Adaptee_ActualService { public: void request() { specificRequest(); } }; // Client int main() { Target_ClientInterface* Target_ClientInterface = new Adapter(); Target_ClientInterface->request(); delete Target_ClientInterface; return 0; } |
What is the wrapped class in adapter?
The wrapped class is also known as the adaptee class and the client code wants to use it but the client is unable to use it due to an incompatible interface.
The adapter adapts this wrapped class by providing a compatible interface that the client code can use to access the wrapped class.