设计模式读书笔记:Adapter Class/Object(适配器)
来源:互联网 发布:最后生还者 巴黎 知乎 编辑:程序博客网 时间:2024/06/06 20:15
意图:
将一个类的接口转换成客户希望的另外一个接口。Adapter 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
结构图:来自 《23种设计模式 - 郗晓勇》
实现:https://github.com/panshiqu/patterns/tree/master/Adapter
Adaptee
#include <iostream>namespace NS_ADAPTER {class Adaptee {public:Adaptee() {}virtual ~Adaptee() {}void SpecificRequest(void){std::cout << "SpecificRequest." << std::endl;}};} /* namespace NS_ADAPTER */AdapterA
// 类适配器#include "Target.h"#include "Adaptee.h"namespace NS_ADAPTER {class AdapterA : public Target, private Adaptee{public:AdapterA() {}virtual ~AdapterA() {}virtual void Request(void){SpecificRequest();}};} /* namespace NS_ADAPTER */AdapterB
// 对象适配器#include "Target.h"#include "Adaptee.h"namespace NS_ADAPTER {class AdapterB : public Target{public:AdapterB(Adaptee *adaptee) : _adaptee(adaptee) {}virtual ~AdapterB() {}virtual void Request(void){_adaptee->SpecificRequest();}private:Adaptee *_adaptee;};} /* namespace NS_ADAPTER */Target
#include <iostream>namespace NS_ADAPTER {class Target {public:Target() {}virtual ~Target() {}virtual void Request(void){std::cout << "Request." << std::endl;}};} /* namespace NS_ADAPTER */main
#include "Adapter/AdapterA.h"#include "Adapter/AdapterB.h"using namespace NS_ADAPTER;int main(void){// 类适配器AdapterA aa;aa.Request();// 对象适配器Adaptee ada;AdapterB ab(&ada);ab.Request();}附加:
0 0
- 设计模式读书笔记:Adapter Class/Object(适配器)
- c++设计模式(五)Adapter class/object 适配器
- 适配器模式(Adapter Class/Object)
- 设计模式之适配器模式(Adapter Class/Object)
- Adapter Class/Object(适配器)
- 设计模式--class adapter object adapter
- 设计模式读书笔记之适配器模式(Adapter)
- 设计模式读书笔记之适配器模式(Adapter)
- 【初学设计模式】Object Adapter (对象适配器)
- 【初学设计模式】Class Adapter (类适配器)
- 设计模式----Adapter(适配器)
- 适配器设计模式(Adapter)
- 《设计模式解析》读书笔记之Adapter Pattern(适配器模式)
- 设计模式--适配器(Adapter)
- 适配器Adapter 设计模式
- 设计模式--Adapter(适配器)
- 设计模式 - 适配器(Adapter)
- 适配器设计模式-Adapter
- Linux下实现RAID
- java计算时间差及比较时间
- 微信移动用户他入组
- Ubuntu/Debian上安装Node.js和npm
- 高光BRDF化简公式总结(转)
- 设计模式读书笔记:Adapter Class/Object(适配器)
- 百度地图 引擎初始化失败
- INSTALL_PARSE_FAILED_NO_CERTIFICATES 解决办法
- C++ exit 与 return 浅析
- struts2学习笔记(5)---自定义拦截器
- hadoop2 namonode为HA 得到hadoop的active namenode具体地址代码
- C语言实现封装、继承和多态
- performSelector 那点事
- 算法二:深度和广度优先搜索