?
1. 设计模式中抽象工厂的泛型 实现
2. c++ 自动生成模板代码 的例子 具体实现见:c++ 泛型编程 之 自动生成代码
?
?
?
?
////////////////////////////////////////////////////////////////////////////////
// The Loki Library
// Copyright (c) 2001 by Andrei Alexandrescu
// This code accompanies the book:
// Alexandrescu, Andrei. Modern C++ Design: Generic Programming and Design
// Patterns Applied. Copyright (c) 2001. Addison-Wesley.
// Permission to use, copy, modify, distribute and sell this software for any
// purpose is hereby granted without fee, provided that the above copyright
// notice appear in all copies and that both that copyright notice and this
// permission notice appear in supporting documentation.
// The author or Addison-Wesley Longman make no representations about the
// suitability of this software for any purpose. It is provided as is
// without express or implied warranty.
////////////////////////////////////////////////////////////////////////////////
#ifndef LOKI_ABSTRACTFACTORY_INC_
#define LOKI_ABSTRACTFACTORY_INC_
// $Id: AbstractFactory.h 771 2006-10-27 18:05:03Z clitte_bbt $
#include HierarchyGenerators.h
#include typelists.h
#include
#include
namespace Loki { //////////////////////////////////////////////////////////////////////////////// //Type2Type
用于消除歧义,因为同一个继承体会有多个AbstractFactoryUnit的具现体 //如:AbstractFactoryUnit
,AbstractFactoryUnit等 //DoCreate这个函数返回的并不是一个常类型,因为T为可变的 //在c++中,你可以返回型别为Pointer to Derived class 的函数改写(overide)为“返回 // pointer to base class的函数。这个就是”协变式返回型别(covariant return types) /* class A{ public: virtual A * ff() = 0; }; class B:public A{ public: B * ff(){return this;} }; 使用: B b; */ //////////////////////////////////////////////////////////////////////////////// template
class AbstractFactoryUnit { public: virtual T* DoCreate(Type2Type
) = 0; virtual ~AbstractFactoryUnit() {} }; //////////////////////////////////////////////////////////////////////////////// // class template AbstractFactory // Defines an Abstract Factory interface starting from a typelist //////////////////////////////////////////////////////////////////////////////// template < class TList, template
class Unit = AbstractFactoryUnit > class AbstractFactory : public GenScatterHierarchy
{ public: typedef TList ProductList; template
T* Create() { Unit
& unit = *this; return unit.DoCreate(Type2Type
()); } }; //////////////////////////////////////////////////////////////////////////////// // class template OpNewFactoryUnit // Creates an object by invoking the new operator //////////////////////////////////////////////////////////////////////////////// template
class OpNewFactoryUnit : public Base { typedef typename Base::ProductList BaseProductList; protected: typedef typename BaseProductList::Tail ProductList; public: typedef typename BaseProductList::Head AbstractProduct; ConcreteProduct* DoCreate(Type2Type
) {//AbstractProduct抽象产品,ConcreteProduct具体产品,同Soldier和SillySoldier的关系 std::cout<<基类:<
)中的AbstractProduct的顺序相反,所以需要Reverse一下 当然也可以修改OpNewFactoryUnit 而不翻转ConcreteFactory中的TList 修改成如下: typedef typename Base::ProductList BaseProductList; protected: typedef typename Reverse
%3CBaseProductList%3E::Result::Tail>::Result ProductList; public: typedef typename Reverse
::Result::Head AbstractProduct; 不过这种设计不太好,因为每个Creator都要处理这种翻转情况,不如在ConcreteFactory类中一下彻底解决问题好。 */ /////