适配器模式 Adapter
2015-07-07 20:22
260 查看
适配器模式,调整使之符合另一接口
1类适配器模式,子类继承Adaptee类
2对象适配器模式,将Adaptee类作为对象存在于Adapter类中,在Adapter类中由Target继承来的接口中调用Adaptee的操作
3.缺省适配器模式,
1类适配器模式,子类继承Adaptee类
#include <IOSTREAM> using namespace std; //类适配器 class Target{ public: virtual void action() = 0; }; class Adaptee{ public: void Adapteraction(){ cout<<"hello world"<<endl; } }; class Adapter:public Target,Adaptee{ public: void action(){ this->Adapteraction(); } }; int main(int argc, char* argv[]) { Target * p = new Adapter; p->action(); delete p; return 0; }
2对象适配器模式,将Adaptee类作为对象存在于Adapter类中,在Adapter类中由Target继承来的接口中调用Adaptee的操作
//对象适配器 class Target{ public: virtual void action() = 0; }; class Adaptee{ public: void Adapteraction(){ cout<<"hello world"<<endl; } }; class Adapter:public Target{ public: void action(){ p->Adapteraction(); } Adapter(){ p = new Adaptee; } ~Adapter(){ delete p; } private: Adaptee * p; }; int main(int argc, char* argv[]) { Target * p = new Adapter; p->action(); delete p; return 0; }
3.缺省适配器模式,
我们将这个中间过渡类称为 “缺省适配类”,缺省适配模式为一个接口提供缺省实现
#include <IOSTREAM> using namespace std; //缺省适配器 class Target{ public: virtual void action1() = 0; virtual void action2() = 0; virtual void action3() = 0; }; class DefaultAdapter:public Target{ public: void action1(){ cout<<"action1"<<endl; } void action2(){ cout<<"action2"<<endl; } void action3(){ cout<<"action3"<<endl; } }; class Adapter:public DefaultAdapter{ public: void action1(){ cout<<"only want action1"<<endl; } }; int main(int argc, char* argv[]) { Target * p = new Adapter; p->action1(); delete p; return 0; }
相关文章推荐
- Vector 容器
- Hot Days Codeforces Round #132 (Div. 2) D(贪婪)
- java之Object转Map
- OC-可变数组-可变字典嵌套(练习-省市区)
- 以操作系统的角度述说线程与进程
- Mahout-HashMap的进化版FastByIdMap
- Android 中的BroadCastReceiver
- 数据结构——九大排序算法一
- Spring 下载
- 80.创建带头的单项链表
- C#程序设计-猜猜看
- leetCode 26.Remove Duplicates from Sorted Array(删除数组重复点) 解题思路和方法
- javascript笔记02:严格模式的特定要求
- 集群安装完毕,该如何测试和使用集群-hadoop单机(伪分布)
- 多线程调用fork的问题 连续捕捉相同信号的问题
- TJU4111-Binomial efficient(数论)
- TCP知识点记录
- COJ 0801 非传统题(一)
- Murano PTL&Core
- 为什么使用剪切板时都用GlobalAlloc分配内存