Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2652655
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2008-07-28 16:07:50

  今天在看<<设计模式精解-GoF 23 种设计模式解析附 C++实现源码>>中的Adapter模式时,作者的理解没有错,但在代码上的实现有点异议

  他用了类模式的Adapter与对象模式的Adapter的代码,以实现统一接口,但运行时,没有实现动态得到Request,并没有达到实现Adapter的目的,具体源码可参考:http://www.cnblogs.com/k-eckel/articles/188896.html

实现也可参考java的Adapter采用接口方式(有空再试验)。
下面是C++实现的一下样例,可供参考:

#include "stdafx.h"
#include
using namespace std;
class ExecuteInterface { 
public: 
 // 1. Specify the new i/f 
 virtual ~ExecuteInterface(){} 
 virtual void execute() = 0;
}; 
template   // 2. Design a "wrapper" or
class ExecuteAdapter: public ExecuteInterface {
 // "adapter" class 
public: 
 ExecuteAdapter(TYPE *o, void(TYPE:: *m)()) 
 { 
  object = o; 
  method = m; 
 } 
 ~ExecuteAdapter() 
 { 
  delete object; 
 } 
 // 4. The adapter/wrapper "maps" the new to the legacy implementation 
 void execute() /* the new */ 
 { 
  (object->*method)(); 
 } 
private: 
 TYPE *object; // ptr-to-object attribute 
 void(TYPE:: *method)(); /* the old */ // ptr-to-member-function
};
 // attribute 
 // The old: three totally incompatible classes 
 // no common base class,
class Fea { 
public: 
 // no hope of polymorphism 
 ~Fea() 
 { 
  cout << "Fea::dtor" << endl; 
 } 
 void doThis() 
 { 
  cout << "Fea::doThis()" << endl; 
 }
}; 
class Feye { 
 public:~Feye() 
 { 
  cout << "Feye::dtor" << endl; 
 } 
 void doThat() 
 { 
  cout << "Feye::doThat()" << endl; 
 }
}; 
class Pheau { 
public: 
 ~Pheau() 
 { 
  cout << "Pheau::dtor" << endl; 
 } 
 void doTheOther() 
 { 
  cout << "Pheau::doTheOther()" << endl; 
 }
}; 
 /* the new is returned */
 ExecuteInterface **initialize() { 
  ExecuteInterface **array = new ExecuteInterface *[3]; /* the old is below */ 
  array[0] = new ExecuteAdapter < Fea > (new Fea(), &Fea::doThis); 
  array[1] = new ExecuteAdapter < Feye > (new Feye(), &Feye::doThat); 
  array[2] = new ExecuteAdapter < Pheau > (new Pheau(), &Pheau::doTheOther); 
  return array;
 } 
int main10()

 ExecuteInterface **objects = initialize(); 
 for (int i = 0; i < 3; i++) 
  objects[i]->execute(); 
 // 3. Client uses the new 
 // (polymporphism) 
 for (i = 0; i < 3; i++) 
  delete objects[i]; 
 delete objects;
 return 0;
}

参考:
功能
将一个类的接口转换成客户希望的另外一个接口,解决两个已有接口之间不匹配的问题。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

下载是一个运行的真实实例(非示例,本人运行示例时浪费了点时间,希望别人少浪费时间)
/*********************************************/
using namespace std;  
class T1
{
public:
 T1(int i) { m_i = i; }
 void Proc() {printf("T1 Proc %d\n", m_i);}
 int m_i;
} ;
class T2
{
public:
 T2(char c) { m_c = c; }
 void Proc() {printf("T2 Proc %c\n", m_c);}
 char m_c;
} ;
// class IAdaptor,抽象基类
class IAdaptor
{
public:
 virtual void Proc() = 0 ;
};
// class Adaptor
template
class Adaptor : public IAdaptor, private T //实现继承
{
public:
 Adaptor(int n) : T(n) {}
 Adaptor(char c) : T(c) {}
 virtual void Proc() { T::Proc() ; }
} ;
// 以统一方式调用函数Proc,而不关心是T1、T2或其他什么类
class Test
{
public:
 Test(int n) : sp(new Adaptor< T1 >(n)) {}
 Test(char c) : sp(new Adaptor < T2 >(c)) {}
 void Proc() { sp->Proc() ; }
private:
 std::auto_ptr< IAdaptor > sp ;
} ;
int main() {
 Test t1(10) ;
 t1.Proc() ;
 Test t2('c') ;
 t2.Proc() ;
 return 0;
}

关键在于:
void   Test(const   std::auto_ptr& sp)   
{   
  sp->Proc();   
}   
    
Test(std::auto_ptr(new   Adaptor));  
 
阅读(1503) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-07-29 08:47:51

Template模式:: 在面向对象系统的分析与设计过程中经常会遇到这样一种情况:对于某一个业务逻辑(算法实现)在不同的对象中有不同的细节实现,但是逻辑(算法)的框架(或通用的应用算法)是相同的。Template提供了这种情况的一个实现框架。Template模式是采用继承的方式实现这一点:将逻辑(算法)框架放在抽象基类中,并定义好细节的接口,子类中实现细节 Template模式可简化为:相同逻辑架构,不同对象细节