(2) Structural pattern: 1. Adapter Pattern (C++ implementation example)

Table of contents

1. The meaning of Adapter Pattern

2. Adapter mode application scenarios

3. UML diagram learning of adapter pattern

4. Example of implementing adapter pattern in C++


1. The meaning of Adapter Pattern

Convert an interface to the interface expected by the client so that two classes with incompatible interfaces can work together;

The Adapter pattern is often used to combine incompatible classes to work together, or to integrate old code with new code.

The adapter pattern also has another name: Wrapper. As the name suggests, it wraps the target class with a new class, which is equivalent to adding a layer directly between the client and the target class.

There is a saying in the IT world: There is no problem that cannot be solved by adding another layer.

2. Adapter mode application scenarios

(1) When an existing class needs to be used, but the interface it provides is incompatible with the interface of our system, and we cannot modify it;

(2) When multiple teams independently develop each functional module of the system and then combine them together, but for some reason the interface cannot be determined in advance.

3. UML diagram learning of adapter pattern

The adapter pattern has 3 roles:

(1) Target: It is an interface, which is the target interface used by the client;

(2) Adaptee: It is the interface we want, a class that is incompatible with Target. This can be an interface or class;

(3) Adapter: Adapter class, the core of this mode. It needs to implement the target interface Target, and must reference Adaptee, because we want to wrap the functions of Adaptee in this class;

4. Example of implementing adapter pattern in C++


#include <iostream>
#include <string>

// 目标接口
class Target 
{
public:
    virtual void request() const = 0;
};

// 源接口
class Adaptee 
{
public:
    void specificRequest() const 
    {
        std::cout << "Adaptee: specificRequest" << std::endl;
    }
};

// 类适配器
class Adapter : public Target, private Adaptee 
{
public:
    void request() const override 
    {
        specificRequest();
    }
};

int main() 
{
    // 使用适配器调用目标接口
    Target* target = new Adapter();
    target->request();

    delete target;

    return 0;
}

In the above example, we first define the target interface (Target), which contains a pure virtual function request(). Then, we define the source interface (Adaptee), which contains a specific function specificRequest().

Next, we implemented the adapter class (Adapter) through a class adapter, which inherits from the target interface and privately inherits the source interface. In the adapter class, we implement the pure virtual function of the target interface and internally call the specific function of the source interface.

In the main function, we create an adapter object and call the adapter's request()function through the target interface. Since the adapter class inherits both the target interface and the source interface, when calling a request()function, the specific function in the adapter class will actually be called.

Guess you like

Origin blog.csdn.net/bigger_belief/article/details/132230091