The Adapter Pattern of Structural Design Pattern [Design Pattern Series]

Series Article Directory

C++ skill series
Linux communication architecture series
C++ high-performance optimization programming series
Deep understanding of software architecture design series
Advanced C++ concurrent thread programming
design pattern series

Looking forward to your attention! ! !
insert image description here

现在的一切都是为将来的梦想编织翅膀,让梦想在现实中展翅高飞。
Now everything is for the future of dream weaving wings, let the dream fly in reality.

1. Introduction to Adapter Mode

⚠️ 意图:
Adapter mode is used for adaptation. It converts incompatible interfaces into compatible interfaces, so that classes that could not work together due to incompatible interfaces can work together.

⚠️ 主要解决:
It mainly solves that in software systems, it is often necessary to put some "existing objects" into a new environment, and the new environment requires an interface that cannot be satisfied by the existing objects.

⚠️ 何时使用:
(1) The system requires an existing class, and the interface of this class does not meet the needs of the system.
(2) I want to build a reusable class for some classes that are not very related to each other, including some classes that will be introduced in the future to work together. These source classes do not necessarily have a consistent interface.
(3) Insert a class into another class system through interface conversion.

⚠️ 如何解决:
Inherit or depend on (recommended).

Adapter mode is used for adaptation. It converts incompatible interfaces into compatible interfaces, so that classes that could not work together due to incompatible interfaces can work together. For this mode, we often use a vivid example of the USB adapter to explain it. The USB adapter acts as an adapter, through which two incompatible interfaces can work together.

There are two implementations of the Adapter pattern :

  • Class Adapters : Class adapters are implemented by using inheritance relationships.
  • Object adapter : Complies with the principle of composite reuse, and uses the delegation mechanism, and the object adapter is implemented using the composition relationship.

insert image description here

Figure 1_1 Adapter pattern class diagram

When the data and behavior of the system are correct, but the interface does not match, we should consider using an adapter. The purpose is to make an existing object outside the control scope match an interface. Adapter mode is mainly used in situations where you want to reuse some existing classes, but the interface is inconsistent with the requirements of the reuse environment.

If you want to use an existing class, but if its interface, that is, its methods are different from your requirements, you should consider using the adapter mode.

For example, the third-party development component purchased, the component interface is different from the interface of our own system, or the component cannot be called directly for some reason, you can consider the adapter.

2. Advantages and disadvantages of bridge mode

2.1 Advantages

  • It can improve the transparency and reuse of classes, and the existing classes can be reused but do not need to be changed;

  • Solved the problem of mismatch between existing class and target class;

  • Target class and adapter class are decoupled to improve program scalability;

  • We can use the adapter pattern to develop our own functions;

  • Comply with the principle of opening and closing;

  • The specific implementation is in the adapter, and the client only knows the adapter class. For extension, we only need to extend the adapter, and the original class does not need to be changed.

2.2 Disadvantages

  • Adapters need to be fully considered in the process of writing, which may increase the complexity of the system

  • Increase the difficulty of system code readability

  • If too many adapters are used, our system will be very messy and difficult to organize and grasp. For example, when we call the first interface, it has been adapted internally to call the second interface. In this case, it is easy to get confused when reading the code, so it increases the readability of the system code.

3. Bridge Mode Usage Scenarios

Motivated to modify the interface of a functioning system, the Adapter pattern should be considered.

  • Encapsulation of flawed interface design;

  • Unified multi-class interface design;

  • Replace dependent external systems;

  • Compatible with the old version interface;

  • Adapt to data in different formats.

Fourth, bridge mode implementation

Adapter.h

#ifndef ADAPTER_ADAPTER_H
#define ADAPTER_ADAPTER_H

#include <iostream>

//目标接口类,客户需要的接口
class Target
{
    
    
public:
    Target();
    virtual ~Target();
    virtual void Request();//定义标准接口
};

//需要适配的类
class Adaptee
{
    
    
public:
    Adaptee();
    ~Adaptee();
    static void SpecificRequest();
};

//类模式,适配器类,通过public继承获得接口继承的效果,通过private继承获得实现继承的效果
class Adapter:public Target,private Adaptee
{
    
    
public:
    Adapter();
    ~Adapter() override;
    void Request() override;//实现Target定义的Request接口
};

//对象模式,适配器类,继承Target类,采用组合的方式实现Adaptee的复用
class Adapter1:public Target {
    
    
public:
    explicit Adapter1(Adaptee *adaptee);

    Adapter1();

    ~Adapter1() override;

    void Request() override;//实现Target定义的Request接口
private:
    Adaptee *_adaptee;

};
#endif //ADAPTER_ADAPTER_H

Adapter.cpp

#include "Adapter.h"

using namespace std;

Target::Target() = default;

Target::~Target() = default;

void Target::Request()
{
    
    
    cout << "Target::Request()" << endl;
}

Adaptee::Adaptee() = default;

Adaptee::~Adaptee() = default;

void Adaptee::SpecificRequest()
{
    
    
    cout << "Adaptee::SpecificRequest()" << endl;
}

//类模式的Adapter
Adapter::Adapter() = default;

Adapter::~Adapter() = default;

void Adapter::Request()
{
    
    
    cout << "Adapter::Request()" << endl;
    this->SpecificRequest();
    cout << "----------------------------" <<endl;
}

//对象模式的Adapter
Adapter1::Adapter1():_adaptee(new Adaptee)
{
    
    
}

Adapter1::Adapter1(Adaptee* _adaptee)
{
    
    
    this->_adaptee = _adaptee;
}

Adapter1::~Adapter1() = default;

void Adapter1::Request()
{
    
    
    cout << "Adapter1::Request()" << endl;
    this->_adaptee->SpecificRequest();
    cout << "----------------------------" <<endl;
}

main.cpp

#include <iostream>
#include "Adapter.h"

int main() {
    
    

    //类模式Adapter
    Target* pTarget = new Adapter();
    pTarget->Request();

    //对象模式Adapter1
    Adaptee* ade = new Adaptee();
    Target* pTarget1= new Adapter1(ade);
    pTarget1->Request();

    //对象模式Adapter2
    Target* Target2 = new Adapter1();
    Target2->Request();

    return 0;
}

In the two modes of the Adapter mode, a very important concept is the difference and connection between interface inheritance and implementation inheritance. Interface inheritance and implementation inheritance are two important concepts in the object-oriented field. Interface inheritance means that through inheritance, the subclass obtains the interface of the parent class, while implementation inheritance refers to obtaining the implementation of the parent class by inheriting the subclass ( not all interfaces). Public inheritance in C++ is both interface inheritance and implementation inheritance, because after inheriting the parent class, the subclass can not only provide the interface operations in the parent class to the outside world, but also obtain the interface implementation of the parent class. Of course, we can simulate separate interface inheritance and implementation inheritance through certain methods and techniques. For example, we can obtain the effect of implementation inheritance through private inheritance (after private inheritance, the interfaces in the parent class become private, of course, only implementation inheritance ), the effect of interface inheritance is simulated through a pure abstract base class, but pure virtual function can also provide a default implementation in C++, so this is impure interface inheritance, but in Java we can use interface to obtain real interface inheritance up.

Guess you like

Origin blog.csdn.net/weixin_30197685/article/details/131880892