网页功能: 加入收藏 设为首页 网站搜索  
简单实现设计模式
发表日期:2007-03-28作者:[转贴] 出处:  

下面这些设计模式的实现都是一些简单的示例实现,如果你希望更稳固,更方便的实现,请自己修改。我以Gof的书作为蓝本,实现里面可以被抽象的设计模式,里面使用Loki, Boost, STL的一些东西,如果有疑问的话请翻阅相关书籍。

//***************************************************************************

Abstract Facrory 抽象工厂:

目的:
为了把产品的创建抽象化
为了隐藏产品的实现
为了实现一序列产品的安全创建

实现:

namespace Noir_Impl
{
template<class P>
struct CAF_Product { P* Create_Impl() { return new P; } };

template<class CProductList>
class CAF_AbstractFactory : public Loki::GenScatterHierarchy< CProductList, CAF_Product > { };
};

//IProductList是你的抽象工厂接口,CProductList是你的抽象工厂的实现

template<class IProductList, class CProductList>
struct Simple_AbstractFactory : public IProductList, private Noir_Impl::CAF_AbstractFactory< CProductList >
{
template<class IP> IP* Create()
{
typedef TypeAt< CProductList, IndexOf< IProductList, IP >::value > CP;
return CP::Create_Impl();
}
};

//***************************************************************************

Builder 产生器:

目的:
为了实现通过继承修改产品创建的某个环节

实现:

template<int stepnum> struct IBuilder;  //产生器接口,stepnum表示分几步构造

template<>
struct IBuilder<0>
{
virtual void BuildPart(Loki::Int2Type<0>&) = 0;
};

template<int stepnum>
struct IBuilder : public IBuilder< stepnum - 1 >
{
virtual void BuildPart(Loki::Int2Type<stepnum>&) = 0;
};

template<int stepnum, class T>
struct Simple_Builder //产生器
{
template<int stepnum> static void Build(T* obj)
{
obj->IBuilder<stepnum>::BuildPart(Loki::Int2Type<stepnum>());
Build< stepnum - 1 >(obj);
}
template<> static void Build<0>(T* obj)
{
obj->IBuilder<0>::BuildPart(Loki::Int2Type<0>());
}
};

//***************************************************************************

Clone Factory 克隆工厂:

目的:
为了不关心我们将创建的对象的类别
为了避免类爆炸,通过运行时的参数指定来创建新的“类”

实现:

namespace Noir_Impl
{
template<class P>
class CCL_Product

private:
P* m_obj;
public:
CCL_Product() : m_obj(NULL) { }
void Set_Impl(P* p) { assert(p); m_obj = p; }
P*   Clone_Impl()   { assert(m_obj); return new P(m_obj); } 
};

template<class CProductList>
class CCL_CloneFactory : public Loki::GenScatterHierarchy< CProductList, CCL_Product > { };
};

template<class IProductList, class CProductList>  //IProductList是你的克隆工厂接口,CProductList是你的克隆工厂的实现
struct Simple_CloneFactory : public IProductList, private Noir_Impl::CCL_CloneFactory< CProductList >
{
template<class IP> void Set(IP* p)
{
typedef TypeAt< CProductList, IndexOf< IProductList, IP >::value > CP;
CP::Set_Impl(p);
}
template<class IP> IP* Clone()
{
typedef TypeAt< CProductList, IndexOf< IProductList, IP >::value > CP;
return CP::Clone_Impl();
}
};

//***************************************************************************

FactoryMethod 对象工厂:

目的:
为了在运行时通过外部信息来创建产品
当不关心对象的类别的时候

实现;

#define CLASSID(x)   static const int CLASS_ID = x;

template< class IC, class PList >
struct Simpe_FactoryMethod : public Loki::SmallObject<>
{
template<int index> IC* Create(int id)
{
assert( id >= 0 );
typedef TypeAt<PList, index>::Result CClass;
if( id == CClass::CLASS_ID )
return new CClass;
else 
return Create<index - 1>(id);
}
template<> IC* Create<0>(int id)
{
assert( id >= 0 );
typedef TypeAt<PList, index>::Result CClass;
if( id == CClass::CLASS_ID )
return new CClass;
else 
assert(0);
}
};

//***************************************************************************

Singleton 单件:

目的:
一个安全的全局变量,你可以完全监控他的行为

实现:

template<class T>
class Simple_Init
{
private:
static T _obj;
public:
static T& Instance() { return _obj; }
};
template<class T>
T Simple_Init<T>::_obj;


template<class T>
class Lazy_Init
{
public:
static T& Instance() { static T _obj; return _obj; }
};

template< class T, template<class> class S=Simple_Init>
class Simple_Singleton : private S<T>, private boost::noncopyable, private noncreatable, public T
{
private:
operator &() const;
operator T() const;
public:
using S<T>::Instance;

bool Init()   { return Instance().Init();   }
bool UnInit() { return Instance().UnInit(); }
};

//***************************************************************************

Adapter 适配器:

目的:
为了把不吻合或不搭配的接口转换成我们希望的接口

实现:
无固定实现

//***************************************************************************

Bridge 桥接:

目的:
抽象出实现的“核心部分”和“扩展部分”,“扩展部分”由“核心部分”实现,通过切换“核心部分”来使“扩展部分”相应的改变,从而达到切换整个系统的目的

实现:
无固定实现,其实就是Policy的设计方法的一个运用

//***************************************************************************

Composite 组合:

目的:
为了把对象以及他们的组合体“一视同仁”

实现:

template<class T>
class Simple_Composite_Train : private Safe_Dector, public T, public Loki::SmallObject<>
{
private:
typedef Simple_Composite_Train<T> ClassType;
std::list<ClassType*> m_childlist;
public:
void ChildAs(ClassType* p)
{
assert(p);
p->m_childlist.pus_back(this);
}
void ParentAs(ClassType* p)
{
assert(p);
p->ChildAs(this);
}
void CutFromParent(ClassType* p)
{
assert(p);
std::remove(p->m_childlist.begin(), p->m_childlist.end(), p);
}
void RemoveChild(ClassType* p)
{
assert(p);
p->CutFromParent(this);
}
void RemoveAllChild()
{
DoFunc0<RemoveChild>();
}

template<typename void(ClassType::* func)()>
void DoFunc()
{
func();
std::for_each(m_childlist.begin(), m_childlist.end(), boost::bind(func, _1));
}
template<class Param1, typename void(ClassType::* func)(Param1*)>
void DoFunc(Param1* p1)
{
func(p1);
std::for_each(m_childlist.begin(), m_childlist.end(), boost::bind(func, _1, p1));
}
template<class Param1, class Param2, typename void(ClassType::* func)(Param1*, Param2*)>
void DoFunc(Param1* p1, Param2* p2)
{
func(p1, p2);
std::for_each(m_childlist.begin(), m_childlist.end(), boost::bind(func, _1, p1, p2));
}
template<class Param1, class Param2, class Param3, typename void(ClassType::* func)(Param1*, Param2*, Param3*)>
void DoFunc(Param1* p1, Param2* p2, Param3* p3)
{
func(p1, p2, p3);
std::for_each(m_childlist.begin(), m_childlist.end(), boost::bind(func, _1, p1, p2, p3));
}
/*......*/
};

template<class T>
class Simple_Composite_Resever : private Safe_Dector, public T, public Loki::SmallObject<>
{
private:
typedef Simple_Composite_Resever<T> ClassType;
ClassType* m_parent;
public:
Simple_Composite_Resever() : m_parent(NULL) { }

void ChildAs(ClassType* p)
{
assert(p);
m_parent = p;
}
void ParentAs(ClassType* p)
{
assert(p);
p->ChildAs(this);
}
void CutFromParent()
{
assert(p);
m_parent = NULL;
}

template<typename void(ClassType::* func)()>
void DoFunc()
{
ClassType* ptemp = this;
while(!ptemp)
{
temp->func();
temp = temp->m_parent;
}
}
template<class Param1, typename void(ClassType::* func)(Param1*)>
void DoFunc(Param1* p1)
{
ClassType* ptemp = this;
while(!ptemp)
{
temp->func(p1);
temp = temp->m_parent;
}
}
template<class Param1, class Param2, typename void(ClassType::* func)(Param1*, Param2*)>
void DoFunc(Param1* p1, Param2* p2)
{
ClassType* ptemp = this;
while(!ptemp)
{
temp->func(p1, p2);
temp = temp->m_parent;
}
}
template<class Param1, class Param2, class Param3, typename void(ClassType::* func)(Param1*, Param2*, Param3*)>
void DoFunc(Param1* p1, Param2* p2, Param3* p3)
{
ClassType* ptemp = this;
while(!ptemp)
{
temp->func(p1, p2, p3);
temp = temp->m_parent;
}
}
/*......*/
};

//***************************************************************************

Decorator 修饰:

目的:
为了动态的增加对象的职能:

实现:

template<class T>
class Simple_Decorator : public Simple_Composite_Resever<T> { };

//***************************************************************************

Facade 外观:

目的:
为复杂的借口提供一个简单的访问点

实现:

namespace Noir_Impl
{
template <class AtomicType, class Base> 
struct Empty_Unit { };
};

template<class TList>
class Simple_Facade : public Loki::GenLinearHierarchy<TList, Noir_Impl::Empty_Unit>  { };

//***************************************************************************

FlyWieght 享元

目的:
为了使数量巨大的对象共享大量重复的对象或属性

实现:

namespace Noir_Impl
{
const int MAX_FLYWEIGHT_NUM = 256;

template<class T>
class FlyWeight_Unit
{
private:
T* m_fwlist[MAX_FLYWEIGHT_NUM];
public:
FlyWeight_Unit() { ZeroMemory(m_fwlist, sizeof(m_fwlist)); }

int Insert(T* p)
{
assert(p);
for(int iter=0; iter<MAX_FLYWEIGHT_NUM; iter++)
{
if(m_fwlist[iter] == NULL)
{
m_fwlist[iter] = p;
return iter;
}
}
}

int Remove(T* p)
{
assert(p);
for(int iter=0; iter<MAX_FLYWEIGHT_NUM; iter++)
{
if(m_fwlist[iter] == p)
{
S_DELETE(m_fwlist[iter]);
return iter;
}
}
assert(0);
return MAX_FLYWEIGHT_NUM;
}

void Clear()
{
for(int iter=0; iter<MAX_FLYWEIGHT_NUM; iter++)
{
S_DELETE(m_fwlist[iter]);
}
}

T* Get(int iter)
{
assert( iter >= 0 && iter < MAX_FLYWEIGHT_NUM );
assert( m_fwlist[iter] );
return m_fwlist[iter];
}
};
};

template<class fwclasslist>
struct Simple_FlyWeight : public Loki::GenScatterHierarchy<fwclasslist, Noir_Impl::FlyWeight_Unit>
{
template<class T> int Insert(T* p)  { return FlyWeight_Unit<T>::Insert(p); }
template<class T> int Remove(T* p)  { return FlyWeight_Unit<T>::Remove(p); }
template<class T> void Clear()      { FlyWeight_Unit<T>::Clear(); }
template<class T> T* Get(int iter)  { return FlyWeight_Unit<T>::Get(iter); }
};

//***************************************************************************

Porxy 代理:

目的:
为了对某个对象增加某些辅助能力或限制而且对使用者完全不透明

实现:
没有固定实现

Chain Of Responsibility 职责链:

目的:
为了把消息发送者和消息处理者解偶

实现:

template<class T>
class Simple_ChainOfResp_Train : public Simple_Composite_Train<T>
{
public:
template<typename void(ClassType::* func)()>
bool DoResp()
{
if( func() )  return true;

list<ClassType*>::iterator i(m_childlist.begin());
list<ClassType*>::iterator e(m_childlist.end());
for(; i!=e; ++i)  if( ((*i)->*func)() ) return true;

return false;
}
template<class Param1, typename void(ClassType::* func)(Param1*)>
bool DoResp(Param1* p1)
{
if( func(p1) )  return true;

list<ClassType*>::iterator i(m_childlist.begin());
list<ClassType*>::iterator e(m_childlist.end());
for(; i!=e; ++i)  if( ((*i)->*func)(p1) ) return true;

return false;
}
template<class Param1, class Param2, typename void(ClassType::* func)(Param1*, Param2*)>
bool DoResp(Param1* p1, Param2* p2)
{
if( func(p1, p2) )  return true;

list<ClassType*>::iterator i(m_childlist.begin());
list<ClassType*>::iterator e(m_childlist.end());
for(; i!=e; ++i)  if( ((*i)->*func)(p1, p2) ) return true;

return false;
}
template<class Param1, class Param2, class Param3, typename void(ClassType::* func)(Param1*, Param2*, Param3*)>
bool DoResp(Param1* p1, Param2* p2, Param3* p3)
{
if( func(p1, p2, p3) )  return true;

list<ClassType*>::iterator i(m_childlist.begin());
list<ClassType*>::iterator e(m_childlist.end());
for(; i!=e; ++i)  if( ((*i)->*func)(p1, p2, p3) )  return true;

return false;
}
/*......*/
};

template<class T>
class Simple_ChainOfResp_Resever : public Simple_Composite_Resever<T>
{
public:
template<typename void(ClassType::* func)()>
bool DoResp()
{
ClassType* ptemp = this;
while(!ptemp)
{
if( temp->func() ) return true;
temp = temp->m_parent;
}

return false;
}
template<class Param1, typename void(ClassType::* func)(Param1*)>
bool DoResp(Param1* p1)
{
ClassType* ptemp = this;
while(!ptemp)
{
if( temp->func(p1) ) return true;
temp = temp->m_parent;
}

return false;
}
template<class Param1, class Param2, typename void(ClassType::* func)(Param1*, Param2*)>
bool DoResp(Param1* p1, Param2* p2)
{
ClassType* ptemp = this;
while(!ptemp)
{
if( temp->func(p1, p2) ) return true;
temp = temp->m_parent;
}

return false;
}
template<class Param1, class Param2, class Param3, typename void(ClassType::* func)(Param1*, Param2*, Param3*)>
bool DoResp(Param1* p1, Param2* p2, Param3* p3)
{
ClassType* ptemp = this;
while(!ptemp)
{
if( temp->func(p1, p2, p3) ) return true;
temp = temp->m_parent;
}

return false;
}
/*......*/
};

//***************************************************************************

Command 命令:

目的:
把命令发起者和执行者解偶

实现:
boost::signals

//***************************************************************************

Interpreter 解释器:

目的:
为了完全动态的控制和修改程序

实现:
boost::regex

//***************************************************************************

iterator 迭代器:

目的:
把容器的内部表示和访问解偶

实现:
没有固定的实现,STL有一些范例

//***************************************************************************

Mediator 中介者:

目的:
把对象间的交互抽象出来,这样可减少交互对象之间的偶合,因为偶合被转移到中介者身上

实现:
无固定实现

//***************************************************************************

Memento 备忘录:

目的:
为了实现场景的恢复

实现:

注意下面这个实现在不同的编译器和平台上都有不同的表现,但对于VC7来说是对的

namespace Noir_Impl
{
template<class T>
struct Raw_Copy_Impl : public Relex<T>
{
void Copy_Impl(T* dest, T* src)
{
if(dest == src) return;
mempcy(dest, src, sizeof(T));
}
};
};
template<class T, template<class> class Copy=Noir_Impl::Raw_Copy_Impl>
class Simple_Memento : public T, private Copy<Simple_Memento> //一定要注意字节对齐问题
{
public:
T* GetMemento() { return new MementoType(this); }
void SetMemento(T* p) { assert(p); Copy_Impl(this, p); }
};

//***************************************************************************

Observer 观察者:

目的:
为了实现一种一对多的关系,当一个对象发生变化,那么所有依赖他的对象都需要发生变化

实现:

template<class I>
class Simple_Observer_Target_Impl
{
private:
std::vector<I*> m_observerlist;
public:
void insert(I* p) { assert(p); m_observerlist.insert(); }
void remove(I* p) 
{
assert(p);
m_observerlist.erase( std::remove( m_observerlist.begin(), m_observerlist.end(), p ) );
}
void clear() { m_observerlist.clear(); }

void fire_all(void(*func)()) 
{ std::for_each( m_observerlist.begin(), m_observerlist.end(), boost::bind( func, _1 ) ); }

template<class T1> 
void fire_all(void(*func)(T1), T1 t1)
{ std::for_each( m_observerlist.begin(), m_observerlist.end(), boost::bind( func, _1, t1 ) ); }

template<class T1, class T2> 
void fire_all(void(*func)(T1, T2), T1 t1, T2 t2) 
{ std::for_each( m_observerlist.begin(), m_observerlist.end(), boost::bind( func, _1, t1, t2 ) ); }

template<class T1, class T2, class T3> 
void fire_all(void(*func)(T1, T2, T3), T1 t1, T2 t2, T3 t3) 
{ std::for_each( m_observerlist.begin(), m_observerlist.end(), boost::bind( func, _1, t1, t2, t3 ) ); }

template<class T1, class T2, class T3, class T4> 
void fire_all(void(*func)(T1, T2, T3, T4), T1 t1, T2 t2, T3 t3, T4 t4) 
{ std::for_each( m_observerlist.begin(), m_observerlist.end(), boost::bind( func, _1, t1, t2, t3, t4 ) ); }
};

template<class I> 
class Simple_Observer_Target : public Simple_Singleton< Simple_Observer_Target_Impl<I> > { };

//***************************************************************************

State 状态:

目的:
为了方便的在改变了状态的情况下改变行为

实现:
无固定实现

//***************************************************************************

Strategy 策略:

目的:
又叫Policy,一言难尽,请看我的其他帖子

实现:
无固定实现

//***************************************************************************

Template Method 模板方法:

目的:
抽象出算法和流程框架,然后允许子类改变实现的细节

实现:
使用Policy的思想

//***************************************************************************

Visitor 访问者:

目的:
为了增加一个类体系的能力

实现:

#define MAKE_VISIT()  \
public: \
template<class U>  \
void Accept(U& obj) \
{  \
obj.Visit( *this );  \
}

namespace Noir_Impl
{
template <class AtomicType, class Base> 
struct Visit_Unit
{
virtual void Visit(AtomicType& obj) { }
};
};

template<class ClassList>
class Simple_Visitor : public Loki::GenLinearHierarchy< ClassList, Noir_Impl::Visit_Unit > { };


我来说两句】 【加入收藏】 【返加顶部】 【打印本页】 【关闭窗口
中搜索 简单实现设计模式
本类热点文章
  智能指针的标准之争:Boost vs. Loki
  创建模块化游戏 I(翻译)(Creating M..
  C++基本功和 Design Pattern系列(6) pu..
  C++基本功和 Design Pattern系列(11) E..
  网络在线游戏开发心得(服务器端、Java)
  用3D方法实现2D斜视角地图
  C++基本功和 Design Pattern系列(8) in..
  物品管理系统
  C++的学习感想
  C++基本功和 Design Pattern系列(10) B..
  解析boost
  C++基本功和 Design Pattern系列(9) vi..
最新分类信息我要发布 
最新招聘信息

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放  
Copyright ©2003-2024 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.00421