【初学设计模式】Object Adapter (对象适配器)
2011-03-17 19:57
375 查看
// 所有原创文章转载请注明作者及链接
// blackboycpp(AT)gmail.com
// QQ群: 135202158
类图:
代码实现:
类声明:
类实现:
main():
// blackboycpp(AT)gmail.com
// QQ群: 135202158
类图:
代码实现:
类声明:
/******************************************************************** File: adapter.h Author: blackboy blackboycpp@gmail.com Purpose: 适配器模式2 对象适配器 Created: 2011-03-16 Modified: 2011-03-16 15:10 *********************************************************************/ #ifndef __OBJECT_ADPATER_H__ #define __OBJECT_ADPATER_H__ namespace blackboycpp { class Target { public: virtual void Request(); }; class Adaptee { public: virtual void SpecificRequest(); }; class Adapter : public Target { public: Adapter(); Adapter(Adaptee*); virtual void Request(); private: Adaptee* _adaptee; }; } #endif
类实现:
#include <iostream> #include "adapter.h" namespace blackboycpp { void Target::Request() { std::cout << "Target::Request()/n"; } void Adaptee::SpecificRequest() { std::cout << "Adaptee::SpecificRequest()/n"; } Adapter::Adapter() { std::cout << "Adapter::Adapter()/n"; _adaptee = 0; } Adapter::Adapter(Adaptee* p) { std::cout << "Adapter::Adapter(Adaptee*)/n"; _adaptee = p; } 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) { Adaptee* adaptee = new Adaptee; Target* target = new Adapter(adaptee); target->Request(); delete target; delete adaptee; system("pause"); return 0; }
相关文章推荐
- 设计模式之适配器(Adapter)---类对象结构型模式
- 设计模式之类对象结构型 — ADAPTER (适配器)模式
- 设计模式之九:Adapter(适配器)—类对象结构型模式
- [设计模式](五):适配器模式(Adapter)[类适配器、对象适配器、接口适配器]
- 设计模式七:Adapter(适配器)——类对象结构型模式
- 设计模式之适配器模式 adapter 适配器模式分类概念角色详解 类适配器 对象适配器 接口适配器 双向适配器
- JAVA设计模式-适配器(类适配器、对象适配器)模式-Adapter
- 设计模式读书笔记:Adapter Class/Object(适配器)
- 设计模式--Adapter(适配器)
- ASP.NET的适配器设计模式(Adapter)应用详解
- Java设计模式透析之 —— 适配器(Adapter)
- 设计模式之适配器(Adapter)模式
- Java 设计模式 接口型模式 之 适配器Adapter模式
- Java设计模式(2)Adapter模式 加个“适配器”以便于复用
- 设计模式--适配器(Adapter)
- 设计模式之适配器模式(Adapter Class/Object)
- 设计模式- 主动对象(Active Object)
- [原]Java设计模式透析之 —— 适配器(Adapter)
- 设计模式之美:Adapter(适配器)
- 架构设计之设计模式 (一) 适配器(Adapter)---提高复用性