当前位置:网站首页>适配器模式
适配器模式
2022-08-04 16:39:00 【l_ethan】
目录
简介
适配器是一种结构型设计模式,它能使接口不兼容的对象能够相互合作。
适配器可以转换不同格式的数据,有助于采用不同接口的对象之间的合作
运作方式
1.适配器实现与其中一个现有对象兼容的接口
2.现有对象可以使用该接口安全地调用适配器方法。
3.适配器方法被调用后将另一个对象兼容的格式和顺序将请求传递给该对象
优点:
1.单一职责原则你可以将接口或数据转换代码从程序主要业务逻辑中分离
2.开闭原则,只要客户端代码通过客户端接口与适配器进行交互,就能在不修改现有客户端代码的情况下在程序中添加新类型的适配器
缺点:
代码整体复杂度增加,因为需要新增一系列接口和类。有时直接更改服务类使其与其他代码兼容更简单
结构

适配器实现了其中一个对象的几口,并对另一个对象进行封装。
1.客户端(client)是包含当前程序业务逻辑的类
2.客户端接口(Client Interface)描述了其他类与客户端代码合作时必须遵循的协议
3.服务(Service)中有一些功能类(通常来自第三方或遗留系统)。客户端与其接口不兼容,因此无法直接调用其功能。
4.适配器(Adapter)是一个可以同时与客户端和服务交互的类:它在实现客户端接口的同时封装了服务对象。适配器接受客户端通过适配器接口发起的调用,并将其转化为为适用于被封装服务对象的调用。
5.客户端代码只需通过接口与适配器交互即可,无需与具体的适配器耦合。因此,可以向程序中添加新类型的适配器而无需修改已有代码。这在服务类的接口被更改或替换时很有用:无需修改客户端代码就可以创建新的适配器类。
类适配器

这一实现使用了继承机制:适配器同时继承两个对象的接口。这种方式仅能在支持多重继承的编程中语言实现如C++。
类适配器不需要封装任何对象,它同时继承了客户端和服务的行为。适配功能在重写的方法中完成。最后生成的适配器可替代已有的客户端进行使用。
案例
#include <iostream>
#include <string>
using namespace std;
//圆钉:客户端接口
class RoundPeg {
public:
RoundPeg(){}
virtual int get_radius() = 0;
};
//方钉:适配者类,即和客户端不兼容的类
class SquarePeg {
public:
explicit SquarePeg(int w) :width(w) {
}
int get_width() {
return width;
}
private:
int width;
};
//方钉适配器:该适配器能让客户端将方钉放入圆孔中
class SquarePegAdapter :public RoundPeg {
public:
explicit SquarePegAdapter(SquarePeg *sp):square_peg(sp){}
int get_radius() override {
return square_peg->get_width() * sqrt(2) / 2;
}
private:
SquarePeg* square_peg;
};
//圆孔:客户端类
class RoundHole {
public:
explicit RoundHole(int r) :radius(r) {
}
int get_radius() {
return radius;
}
bool isFit(RoundPeg* rp) {
return radius >= rp->get_radius();
}
private:
int radius;
};
int main()
{
RoundHole* hole = new RoundHole(10);
//半径分别为5和20大小方钉+它们的适配器
SquarePeg* small_square_peg = new SquarePeg(5);
SquarePeg* large_square_peg = new SquarePeg(20);
SquarePegAdapter* small_square_peg_adapter = new SquarePegAdapter(small_square_peg);
SquarePegAdapter* large_square_peg_adapter = new SquarePegAdapter(large_square_peg);
if (hole->isFit(small_square_peg_adapter)) {
cout << "small square fits the hole" << endl;
}
else {
cout << "small square don't fits the hole" << endl;
}
if (hole->isFit(large_square_peg_adapter)) {
cout << "large square fits the hole" << endl;
}
else {
cout << "large square don't fits the hole" << endl;
}
return 0;
}边栏推荐
- DMS 有接口获取每个实例下的数据库列表吗
- 广东湛江海关破获3起走私冻海产品案 查证案值约1亿元
- LeetCode·84.柱状图中最大的矩形·单调递增栈
- It took half a month to finally make a collection of high-frequency interview questions of first-tier manufacturers
- Hubei Telecom Tianyi TY1608_S905L3B_MT7668_ card brush firmware package
- Jenkins 如何玩转接口自动化测试?
- \/ PN的综合实验
- 《分布式云最佳实践》分论坛,8月11日深圳见
- 跨链桥已成行业最大安全隐患 为什么和怎么办
- 拼多多详情API接口深度解读
猜你喜欢

移动中兴ZXV10 B860AV2.1-A_S905L2_MT7668_线刷固件包

leetcode:743. 网络延迟时间【单源最短路 + dijkstra模板】

Mobile Hisense IP102H_905L3-B_wire brush firmware package

海报 | 夏季高温,危化品安全风险的注意事项必须get!

Minecraft 服务器安装Forge 并添加Mod

codeforces:808D. Array Division【二分 + 找规律】

LeetCode·85.最大矩形·单调栈

Mysql Explain

Mobile BesTV_R3300-L_S905L_8189_wire brush firmware package

测试开发必备技能-Jmeter二次开发
随机推荐
工龄10年的测试员从大厂“裸辞”后...
从-99打造Sentinel高可用集群限流中间件
jasmine.any(Function) 的一个使用场景
MetaAI科学家解读最新模型:200+语言互译,扩充千倍翻译数据,全球元宇宙用户自由交流
番茄插件番茄助手下载
codeforces:808D. Array Division【二分 + 找规律】
“敏捷欺骗了开发人员”
shell中当basename和dirname无法满足你的需求时你一定要想到的命令
【Pick-in】Advertising-information flow cross-domain CTR estimation (to be updated)
It took half a month to finally make a collection of high-frequency interview questions of first-tier manufacturers
【IDEA】idea配置
码蹄集 - MT3029 - 新月轩就餐
MySQL 性能调优和优化技巧
开一个羽毛球馆大概需要多少钱?大约15万左右可以搞定!
服装店如何利用好积分?
饿了么智能头盔专利获授权 进一步提升骑手安全保障
什么是会话劫持攻击以及如何防止会话劫持
闭包及闭包的使用
shell脚本详解-------循环语句wuile循环和until循环
LeetCode 0167. 两数之和 II - 输入有序数组