当前位置:网站首页>Adapter mode

Adapter mode

2022-06-26 13:11:00 baboon_ chen

Reference resources :

Adapter pattern (Adapter Pattern ) Detailed explanation (biancheng.net)

design-patterns-cpp/adapter at master · JakubVojvoda/design-patterns-cpp · GitHub

adapter design pattern ( Wrapper mode ) (refactoringguru.cn)

One 、 What is adapter mode ?

Put the incompatible interface , Through adaptation and modification to achieve unity , Convenient for users .
Adapter patterns are divided into two types: class structure and object structure , The former is implemented by multiple inheritance , High coupling degree , The latter uses a combination , Low coupling .
For example, scenes in life , The power adapter , Notebook adapter , Card reader, etc .

 Insert picture description here

Two 、 Advantages and disadvantages , Applicable scenario

advantage

  • The client can call the target interface transparently through the adapter .
  • Reuse existing classes , Programmers do not need to modify the original code and reuse the existing adapter class .
  • Decouple the target class from the adapter class , It solves the problem that the interface between the target class and the adapter class is inconsistent .
  • In many business scenarios, it conforms to the opening and closing principle .

shortcoming

  • The adapter writing process needs to be fully considered in the context of business scenarios , It may increase the complexity of the system .
  • Increase the difficulty of code reading , Reduce code readability , Using too many adapters can clutter system code .

Applicable scenario

  • Previously developed systems have classes that meet the functional requirements of new systems , But its interface is not the same as that of the new system .
  • Use third party supplied components , But the component interface definition is different from the interface definition required by itself .

3、 ... and 、 Example

Adapter pattern (Adapter) Include the following main characters :

  1. The goal is (Target) Interface : The interface expected by the current system business , It can be an abstract class or interface .
  2. Adapter (Adaptee) class : Existing component interfaces .
  3. Adapter (Adapter) class : converter , By inheriting or referencing an adapter's object , Convert the adapter interface to the target interface , Let the customer access the adapter in the format of the target interface .

1、 Class structural type

/*
 * C++ Design Patterns: Adapter (Class scope)
 * Author: Jakub Vojvoda [github.com/JakubVojvoda]
 * 2016
 *
 * Source code is licensed under MIT License
 * (for more details see LICENSE)
 *
 */

#include <iostream>

/*
 * Target
 *  Define the interface type required by the user 
 */
class Target
{
public:
  virtual ~Target() {}
  
  virtual void request() = 0;
  // ...
};

/*
 * Adaptee
 *  Adapter , Provide existing interfaces 
 */
class Adaptee
{
public:
  ~Adaptee() {}
  
  void specificRequest()
  {
    std::cout << "specific request" << std::endl;
  }
  // ...
};

/*
 * Adapter
 *  Adapter , Reuse existing interfaces , Class structs are implemented through multiple continuations 
 */
class Adapter : public Target, private Adaptee
{
public:
  virtual void request()
  {
    specificRequest();
  }
  // ...
};

int main()
{
  Target *t = new Adapter();
  t->request();
  delete t;
  
  return 0;
}

2、 Object structured

/*
 * C++ Design Patterns: Adapter (Object scope)
 * Author: Jakub Vojvoda [github.com/JakubVojvoda]
 * 2016
 *
 * Source code is licensed under MIT License
 * (for more details see LICENSE)
 *
 */

#include <iostream>

/*
 * Target
 * defines specific interface that Client uses
 */
class Target
{
public:
  virtual ~Target() {}
  
  virtual void request() = 0;
  // ...
};

/*
 * Adaptee
 * defines an existing interface that needs adapting and thanks
 * to Adapter it will get calls that client makes on the Target
 *
 */
class Adaptee
{
public:
  void specificRequest()
  {
    std::cout << "specific request" << std::endl;
  }
  // ...
};

/*
 * Adapter
 *  By combination , Reuse existing interfaces 
 */
class Adapter : public Target
{
public:
  Adapter() : adaptee() {}
  
  ~Adapter()
  {
    delete adaptee;
  }
  
  void request()
  {
    adaptee->specificRequest();
  // ...
  }
  // ...

private:
  Adaptee *adaptee;
  // ...
};


int main()
{
  Target *t = new Adapter();
  t->request();
  delete t;
  
  return 0;
}
原网站

版权声明
本文为[baboon_ chen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261224552496.html