设为首页 加入收藏

TOP

c++ 模板之 抽象工厂(一)
2015-07-20 17:16:08 来源: 作者: 【 】 浏览:6
Tags:模板 抽象 工厂

?

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类中一下彻底解决问题好。 */ /////
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Can you answer these queries?(.. 下一篇HDU 3853 LOOPS (概率dp)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·怎样用 Python 写一 (2025-12-27 02:49:19)
·如何学习python数据 (2025-12-27 02:49:16)
·想要自学数据分析, (2025-12-27 02:49:14)
·Java 集合框架 - 菜 (2025-12-27 02:19:36)
·Java集合框架最全详 (2025-12-27 02:19:33)