当前位置:网站首页>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;
}
边栏推荐
- Word document export (using fixed template)
- B - Bridging signals
- Processing function translate (mousex, mousey) learning
- Explain C language 11 in detail (C language series)
- Typescript
- Verilog中的系统任务(显示/打印类)--$display, $write,$strobe,$monitor
- [geek challenge 2019] rce me 1
- postgis 地理化函数
- Biff TwinCAT can quickly detect the physical connection and EtherCAT network through emergency scan
- 倍福NC轴状态转移图解析
猜你喜欢
Record a phpcms9.6.3 vulnerability to use the getshell to the intranet domain control
倍福PLC选型--如何看电机是多圈绝对值还是单圈绝对值编码器
To solve the difficulties of small and medium-sized enterprises, Baidu AI Cloud makes an example
IDC报告:百度智能云AI Cloud市场份额连续六次第一
Appearance mode (facade)
微信小程序测试点总结
What should the software test report include? Interview must ask
黑马笔记---常用API
Dark horse notes - Common APIs
橋接模式(Bridge)
随机推荐
QT . Establishment and use of pri
Electron official docs series: Processes in Electron
利用scrapy爬取句子迷网站优美句子存储到本地(喜欢摘抄的人有福了!)
IDC report: the AI cloud market share of Baidu AI Cloud ranks first for six consecutive times
P2393 yyy loves Maths II
I - Dollar Dayz
sql 将数据表b字段值赋值到数据表a中某一列
[BSidesCF 2019]Kookie 1
心脏滴血漏洞(CVE-2014-0160)分析与防护
Beifu PLC passes MC_ Readparameter read configuration parameters of NC axis
Electron official docs series: Examples
Group counting practice experiment 9 -- using cmstudio to design microprogram instructions based on segment model machine (2)
D - skiing
Stream learning record
OPLG: 新一代云原生可观测最佳实践
HDU 3709 Balanced Number
Explain C language 10 in detail (C language series)
G - Cow Bowling
Analysis and protection of heart blood dripping vulnerability (cve-2014-0160)
软件测试报告应该包含的内容?面试必问