当前位置:网站首页>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 .

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 :
- The goal is (Target) Interface : The interface expected by the current system business , It can be an abstract class or interface .
- Adapter (Adaptee) class : Existing component interfaces .
- 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;
}
边栏推荐
- G - Cow Bowling
- Script - crawl the customized storage path of the cartoon and download it to the local
- J - Wooden Sticks poj 1065
- HDU 5860
- HDU 3709 Balanced Number
- C structure: definition and example
- 倍福PLC通过程序获取系统时间、本地时间、当前时区以及系统时间时区转换
- E - Apple Catching
- Electron official docs series: Testing And Debugging
- processsing 函数random
猜你喜欢
![[BSidesCF 2019]Kookie 1](/img/22/585d081668e67b8389a1b90aaebe9d.png)
[BSidesCF 2019]Kookie 1

原型模式(prototype)

倍福NC轴状态转移图解析

Detailed explanation of C const: definition and use of C constant

5月产品升级观察站

How does easygbs solve the abnormal use of intercom function?

倍福PLC通过程序获取系统时间、本地时间、当前时区以及系统时间时区转换

Do you know the limitations of automated testing?

Learning Processing Zoog
![[geek challenge 2019] rce me 1](/img/66/e135f7e5a7cbdeb5b697f3939a3402.png)
[geek challenge 2019] rce me 1
随机推荐
倍福PLC基于CX5130实现数据的断电保持
sql 将数据表b字段值赋值到数据表a中某一列
享元模式(Flyweight)
Unit practice experiment 8 - using cmstudio to design microprogram instructions based on basic model machine (1)
倍福TwinCAT3实现CSV、TXT文件读写操作
B - Bridging signals
QT . Establishment and use of pri
Sinotech software outsourcing
10秒内完成火灾预警,百度智能云助力昆明官渡打造智慧城市新标杆
倍福PLC基于NT_Shutdown实现控制器自动关机重启
Processsing function random
倍福通过CTU和TON实现时间片大小和数量的控制
Script - crawl the customized storage path of the cartoon and download it to the local
POJ 3070 Fibonacci
软件测试报告应该包含的内容?面试必问
HDU1724[辛普森公式求积分]Ellipse
Use the script to crawl the beautiful sentences of the sentence fan website and store them locally (blessed are those who like to excerpt!)
Basic principle and application routine of Beifu PLC rotary cutting
To solve the difficulties of small and medium-sized enterprises, Baidu AI Cloud makes an example
What should the software test report include? Interview must ask