《适配器模式》

所谓适配器模式,就是为了解决或者叫融合系统之间的差异,通过提供一个类,来融合这种差异。

这让我想起来读《UNIX 编程艺术》里一个名词  就是胶合层。

 

胶合层也算是一种适配上层鱼下层之间的差异和出现的一种处理方式。   这个是解决系统差异的一个具体表现,

 

下面上图吧

 

 

代码片段

 

[cpp]  view plain copy
  1. // Adapter.cpp : 定义控制台应用程序的入口点。  
  2. ///************************************************************************/  
  3. /* @filename   Adapter.cpp 
  4.    @author       wallwind 
  5.    @createtime    2012/10/222 9:36 
  6.    @function      适配器模式 
  7.    @email       wochenglin@qq.com 
  8. */  
  9. /************************************************************************/  
  10.   
  11.   
  12. #include "stdafx.h"  
  13. #include <iostream>  
  14.   
  15. using namespace std;  
  16.   
  17. class Base  
  18. {  
  19. public:  
  20.     Base(){}  
  21.     virtual ~Base(){};  
  22.   
  23.     virtual void doAction()=0;  
  24.   
  25. };  
  26.   
  27. class Outer  
  28. {  
  29. public:  
  30.     Outer(){}  
  31.     ~Outer(){}  
  32.       
  33.     void exec()  
  34.     {  
  35.         cout<<"Outer:exec"<<endl;  
  36.     }  
  37. };  
  38.   
  39. class Adapter:public Base  
  40. {  
  41. public:  
  42.     Adapter(Outer* outer)  
  43.         :m_outer(outer)  
  44.     {  
  45.     }  
  46.     virtual ~Adapter()  
  47.     {  
  48.         if (m_outer!=NULL)  
  49.         {  
  50.             delete m_outer;  
  51.         }  
  52.     }  
  53.       
  54.     void doAction()  
  55.     {  
  56.         cout<<"Adapter:doAction"<<endl;  
  57.         m_outer->exec();  
  58.     }  
  59. private:  
  60.     Outer* m_outer;  
  61.   
  62. };  
  63. int _tmain(int argc, _TCHAR* argv[])  
  64. {  
  65.     Outer *out =new Outer;  
  66.     Base *adapter =new Adapter(out);  
  67.   
  68.     adapter->doAction();  
  69.   
  70.     if (adapter!=NULL)  
  71.     {  
  72.         delete adapter;  
  73.     }  
  74.     return 0;  
  75. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值