Site icon T4Tutorials.com

Adapter design pattern program in C++

Write a C++ Program demonstrating the working of the Adapter design pattern

adapter Design pattern
adapter Design pattern
#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.

Exit mobile version