当前位置:网站首页>适配器模式(Adapter)
适配器模式(Adapter)
2022-06-26 12:40:00 【baboon_chen】
参考:
适配器模式(Adapter模式)详解 (biancheng.net)
design-patterns-cpp/adapter at master · JakubVojvoda/design-patterns-cpp · GitHub
适配器设计模式(封装器模式) (refactoringguru.cn)
一、什么是适配器模式?
把原本不兼容的接口,通过适配修改做到统一,方便用户使用。
适配器模式分为类结构型和对象结构型两种,前者是以多继承的方式实现,耦合度高,后者使用组合,耦合度低。
比如生活中出现的场景,电源适配器,笔记本转接头,读卡器等。

二、优缺点,适用场景
优点
- 客户端通过适配器可以透明地调用目标接口。
- 复用了现存的类,程序员不需要修改原有代码而重用现有的适配者类。
- 将目标类和适配者类解耦,解决了目标类和适配者类接口不一致的问题。
- 在很多业务场景中符合开闭原则。
缺点
- 适配器编写过程需要结合业务场景全面考虑,可能会增加系统的复杂性。
- 增加代码阅读难度,降低代码可读性,过多使用适配器会使系统代码变得凌乱。
适用场景
- 以前开发的系统存在满足新系统功能需求的类,但其接口同新系统的接口不一致。
- 使用第三方提供的组件,但组件接口定义和自己要求的接口定义不同。
三、示例
适配器模式(Adapter)包含以下主要角色:
- 目标(Target)接口:当前系统业务所期待的接口,它可以是抽象类或接口。
- 适配者(Adaptee)类:现有的组件接口。
- 适配器(Adapter)类:转换器,通过继承或引用适配者的对象,把适配者接口转换成目标接口,让客户按目标接口的格式访问适配者。
1、类结构型
/*
* 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
* 定义用户需要的接口类型
*/
class Target
{
public:
virtual ~Target() {}
virtual void request() = 0;
// ...
};
/*
* Adaptee
* 适配者,提供现有的接口
*/
class Adaptee
{
public:
~Adaptee() {}
void specificRequest()
{
std::cout << "specific request" << std::endl;
}
// ...
};
/*
* Adapter
* 适配器,复用现有的接口,类结构型通过多继续实现
*/
class Adapter : public Target, private Adaptee
{
public:
virtual void request()
{
specificRequest();
}
// ...
};
int main()
{
Target *t = new Adapter();
t->request();
delete t;
return 0;
}
2、对象结构型
/*
* 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
* 通过组合方式,复用已有接口
*/
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;
}
边栏推荐
猜你喜欢

Echart堆叠柱状图:色块之间添加白色间距效果设置

倍福TwinCAT通过Emergency Scan快速检测物理连接和EtherCAT网络

Word文档导出(使用固定模板)

Biff TwinCAT can quickly detect the physical connection and EtherCAT network through emergency scan

详细讲解C语言11(C语言系列)

Do you know the limitations of automated testing?

Processsing function random

轻流完成与「DaoCloud Enterprise 云原生应用云平台」兼容性认证

首批通过!百度智能云曦灵平台获信通院数字人能力评测权威认证

Software testing - concept
随机推荐
Go 结构体方法
自定义封装下拉组件
倍福EtherCAT Xml描述文件更新和下载
mariadb学习笔记
How does easygbs solve the abnormal use of intercom function?
Solution of Splunk iowait alarm
详细讲解C语言11(C语言系列)
HDU 3709 Balanced Number
Software testing - concept
goto语句实现关机小程序
processing 随机生成线动画
别乱用 FULL_CASE 和 PARALLEL_CASE
数字信号处理——线性相位型(Ⅰ、Ⅲ型)FIR滤波器设计(1)
el-form-item 包含两个input, 校验这两个input
Processing function translate (mousex, mousey) learning
详细讲解C语言10(C语言系列)
C - Common Subsequence
Sinotech software outsourcing
processing 函数translate(mouseX, mouseY)学习
倍福TwinCAT通过Emergency Scan快速检测物理连接和EtherCAT网络