适配器模式
来源:互联网 发布:hadoop做数据分析 编辑:程序博客网 时间:2024/05/17 23:42
两种适配器模式:类适配器模式和对象适配器模式
类适配器模式:
三个类:Target,Adapter,Adaptee。
因为现在没有画图工具,所以结构图就暂时不画了。(继承关系是:Adapter继承Target和Adaptee)。
其中Target是我们想要得到的接口,而Target中有些行为是没有的,但是Adaptee有,所以我们就可以通过适配来使用Adaptee,那么怎么才能获得Adaptee方法呢?继承和组合。
类适配器是采用继承的方法。
比如:鸟都有飞的功能,但是没有游的功能,但是鱼有游的功能。所以可以将鱼的功能适配给鸟。形成一种变种鸟。
#include<iostream>using namespace std;//Adapteeclass Fish{public:void swim(){cout<<"I can swim"<<endl;}};//Targetclass Bird{public:virtual void fly()//子类可以随意的改变此行为{cout<<"fly whith A"<<endl;}virtual void Specificswim()=0;//需要获取的行为};//Adapterclass VariantBird:public Bird,public Fish{public:void fly(){cout<<"fly whith A"<<endl;}void Specificswim(){this->swim();}};void main(){Bird* bird=new VariantBird();bird->Specificswim();}
对象适配器模式:采用组合的方式而不是继承。#include<iostream>using namespace std;//Adapteeclass Fish{public:void swim(){cout<<"I can swim"<<endl;}};//Targetclass Bird{public:virtual void fly()//子类可以随意的改变此行为{cout<<"fly whith A"<<endl;}virtual void Specificswim()=0;//需要获取的行为};//Adapterclass VariantBird:public Bird{public:VariantBird(){fish=new Fish();}~VariantBird(){if(fish){delete fish;fish=NULL;}}void fly(){cout<<"fly whith A"<<endl;}void Specificswim(){fish->swim();}private:Fish* fish;};void main(){Bird* bird=new VariantBird();bird->Specificswim();}
- 适配器及适配器模式
- 适配器及适配器模式
- 适配器模式(类适配器)
- 适配器模式(默认适配器)
- 适配器模式(对象适配器)
- 适配器模式-类适配器
- 适配器模式-对象适配器
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 剑指offer-4/题目1510:替换空格
- hdu 4107 Gangster(线段树。时间卡的很紧)
- 好记星不如烂笔头之new的操作
- sql查询语句
- UVa 10137 The Trip (小数四舍五入&需要注意的地方)
- 适配器模式
- 监听返回键并退出所有Activity示例
- 伸展树
- Treap(树堆)
- Apache的OpenOffice
- SkipList 跳表
- c primer读书笔记 第一章
- eclipse的vi插件:viPlugin的安装和破解方法
- HDU 3694 Fermat Point in Quadrangle (数学-费马点)