【初学设计模式】Class Adapter (类适配器)
2011-03-17 19:53
162 查看
// 所有原创文章转载请注明作者及链接
// blackboycpp(AT)gmail.com
// QQ群: 135202158
类图:
代码实现:
类声明:
类实现:
main():
// blackboycpp(AT)gmail.com
// QQ群: 135202158
类图:
代码实现:
类声明:
/******************************************************************** File: adapter.h Author: blackboy blackboycpp@gmail.com Purpose: 适配器模式之一 类适配器 Created: 2011-03-16 Modified: 2011-03-16 09:17 *********************************************************************/ #ifndef __CLASS_ADAPTER_H__ #define __CLASS_ADAPTER_H__ namespace blackboycpp { class Target { public: virtual void Request(); }; class Adaptee { public: void SpecificRequest(); }; // 采用多重继承:对Target采用公共继承,而对Adaptee采用私有继承。 // 这样,Adapter是Target的子类型,而不是Adaptee的子类型。 class Adapter : public Target, private Adaptee { public: virtual void Request(); }; } #endif
类实现:
#include <iostream> #include "adapter.h" namespace blackboycpp { void Target::Request() { std::cout << "Target::Request()/n"; } void Adaptee::SpecificRequest() { std::cout << "Adaptee::SpecificRequest()/n"; } void Adapter::Request() { std::cout << "Adapter::Request()/n"; Adaptee::SpecificRequest(); } }
main():
#include <cstdlib> #include "adapter.h" using namespace blackboycpp; int main(int argc, char** argv) { Target* target = new Adapter; target->Request(); delete target; system("pause"); return 0; }
相关文章推荐
- 【初学设计模式】Object Adapter (对象适配器)
- 设计模式袖珍版 连续转载之 - Adapter(适配器)
- [原]Java设计模式透析之 —— 适配器(Adapter)
- 设计模式之适配器模式
- 设计模式---适配器
- 适配器(JAVA设计模式)
- java设计模式适配器模式
- 适配器设计模式简单实现
- Android 设计模式模式适配器
- JAVA中适配器设计模式的简单介绍
- TypeScript设计模式之门面、适配器
- 【初学设计模式】序
- 设计模式之适配器
- Java设计模式透析之 —— 适配器(Adapter)
- PHP设计模式系列 - 适配器
- Java设计模式—适配器(Adapter)
- PHP设计模式系列 - 适配器
- 适配器(Adapter)设计模式和接口实现完全解耦
- 设计模式之适配器设计
- Java设计模式(一) Adapter(适配器)模式及I/O实例引申