参考:
适配器模式(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;
}