C++设计模式之六:Adapter(适配器)
来源:互联网 发布:贝塔系数的算法 编辑:程序博客网 时间:2024/06/06 06:53
一、意图
将一个类的几口转换成客户希望的另外一个借口。Adapter模式使得原先由于借口不兼容而不能一起工作的哪些类可以一起工作。
可以理解为手机的usb数据线,既能充电,又能传输数据。
二、结构图
Adapter模式分为两种:类适配器和对象适配器;
1.类适配器:采用多重继承实现;
2.对象适配器:采用组合实现;
三、组成元素
(1) 目标抽象角色(Target):定义客户所期待要使用的接口,这是一个抽象的设备,主要是形成一种接口规约,满足这种规约的接口的设备都能满足客户。
(2) 源角色(Adaptee):需要被适配的接口,比如手机数据线和电脑电源插头。
(3) 适配器角色(Adapter):用来把源接口转换成符合要求的目标接口的设备,比如电源转换器和插头转换器。
(4) 客户端(Client):使用接口完成操作。
四、实现代码
#include <iostream>using namespace std;//类适配器class Target{public:virtual void Request()=0;};class Adaptee{public:void SpecificRequest(){cout<<"SpecificRequest"<<endl;}};class Adapter:public Target,private Adaptee{public:virtual void Request(){SpecificRequest();}};//对象适配器class ObjTarget {public:virtual void Request()=0;};class ObjAdaptee{public:void ObjSpecialRequest(){cout<<"ObjSpecialRequest"<<endl;}};class ObjAdapter:public ObjTarget{private:ObjAdaptee obj_adaptee;public:virtual void Request(){obj_adaptee.ObjSpecialRequest();}};void main(){//类适配器cout<<"Class Adapter"<<endl;Target* p=new Adapter();p->Request();//实际调用Adaptee::SpecialRequest()//对象适配器cout<<"Object Adapter"<<endl;ObjTarget*o=new ObjAdapter();o->Request();delete p;delete o;}
- 设计模式之(六)--适配器模式(adapter)
- 设计模式(六)之Adapter - 适配器模式
- 设计模式之六 适配器模式(Adapter)
- 设计模式之六:适配器模式(Adapter Pattern)
- C++设计模式之六:Adapter(适配器)
- 设计模式之--适配器模式(adapter)
- 设计模式之Adapter(适配器模式)
- 设计模式之适配器(Adapter)模式
- 设计模式之--适配器模式(adapter)
- 设计模式之适配器模式(Adapter)
- 设计模式之--适配器模式(adapter)
- 设计模式之适配器模式(Adapter)
- 设计模式之--适配器模式(adapter)
- 设计模式之Adapter(适配器)模式
- 设计模式之适配器模式(Adapter)
- 设计模式之(Adapter)适配器模式
- 设计模式之适配器模式(Adapter)
- 设计模式之适配器模式(Adapter)
- 如何在模拟器上运行apk文件
- 应用程序全屏幕函数
- 有关于男女之间的50条暴笑金句4
- Text-to-Speech Tutorial
- 有关于男女之间的50条暴笑金句3
- C++设计模式之六:Adapter(适配器)
- 有关于男女之间的50条暴笑金句5
- spinlock与linux内核调度的关系
- windows phone水平滑动翻页动画效果
- map和set的排序
- C#中委托,事件以及观察者模式
- CMake初步(3)
- 比较三位数大小,输出最大的一个
- 解決在 Android 中使用 Bitmap 造成 Out of memory 的方法~