设计模式:简单实现设计模式



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

Abstract Facrory 抽象工厂:

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

实现:

Noir_Impl
{
template< P>
struct CAF_Product { P* Create_Impl { P; } };

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

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

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

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

Builder 产生器:

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

实现:

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

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

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

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

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

Clone Factory 克隆工厂:

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

实现:

Noir_Impl
{
template< P>
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); P(m_obj); }
};

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

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

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

FactoryMethod 对象工厂:

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

实现;

# CLASSID(x) const CLASS_ID = x;

template< IC, PList >
struct Simpe_FactoryMethod : public Loki::SmallObject<>
{
template< index> IC* Create( id)
{
assert( id >= 0 );
typedef TypeAt<PList, index>::Result CClass;
( id CClass::CLASS_ID )
CClass;

Create<index - 1>(id);
}
template<> IC* Create<0>( id)
{
assert( id >= 0 );
typedef TypeAt<PList, index>::Result CClass;
( id CClass::CLASS_ID )
CClass;

assert(0);
}
};

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

Singleton 单件:

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

实现:

template< T>
Simple_Init
{
private:
T _obj;
public:
T& Instance { _obj; }
};
template< T>
T Simple_Init<T>::_obj;


template< T>
Lazy_Init
{
public:
T& Instance { T _obj; _obj; }
};

template< T, template<> S=Simple_Init>
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 { Instance.Init; }
bool UnInit { Instance.UnInit; }
};




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

Adapter 适配器:

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

实现:
无固定实现

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

Bridge 桥接:

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

实现:
无固定实现其实就是Policy设计思路方法个运用

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

Composite 组合:

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

实现:

template< T>
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< 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< Param1, 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< Param1, Param2, 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< T>
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< Param1, typename void(ClassType::* func)(Param1*)>
void DoFunc(Param1* p1)
{
ClassType* ptemp = this;
while(!ptemp)
{
temp->func(p1);
temp = temp->m_parent;
}
}
template< Param1, 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< Param1, Param2, 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< T>
Simple_Decorator : public Simple_Composite_Resever<T> { };

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

Facade 外观:

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

实现:

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

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

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

FlyWieght 享元

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

实现:

Noir_Impl
{
const MAX_FLYWEIGHT_NUM = 256;

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

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


}
}

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

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

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

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

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

Porxy 代理:

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

实现:
没有固定实现


Chain Of Responsibility 职责链:

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

实现:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Command 命令:

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

实现:
boost::signals

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

Interpreter 解释器:

:
为了完全动态控制和修改

实现:
boost::regex

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

iterator 迭代器:

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

实现:
没有固定实现STL有些范例

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

Mediator 中介者:

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

实现:
无固定实现

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

Memento 备忘录:



:
为了实现场景恢复

实现:

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

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

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

Observer 观察者:

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

实现:

template< I>
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< T1>
void fire_all(void(*func)(T1), T1 t1)
{ std::for_each( m_observerlist.begin, m_observerlist.end, boost::bind( func, _1, t1 ) ); }

template< T1, 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< T1, T2, 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< T1, T2, T3, 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< I>
Simple_Observer_Target : public Simple_Singleton< Simple_Observer_Target_Impl<I> > { };

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

State 状态:

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

实现:
无固定实现

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

Strategy 策略:

:
又叫Policy言难尽请看我其他帖子

实现:
无固定实现

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

Template Method 模板思路方法:

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

实现:
使用Policy思想

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

Visitor 访问者:

:
为了增加个类体系能力

实现:

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

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

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

Strategy 策略这里可以给出个简单实现比如游戏中某个AI有若干套AI可以定义个抽象AI基类然后其他AI类从他继承这样只需要个只向基类指针就很容易在运行时改变AI了




Tags:  软件设计模式 java设计模式 设计模式

延伸阅读

最新评论

发表评论