第八章 对象工厂

#include 
#include 
#include 
#include 
#include "LokiTypeInfo.h"


using std::map;
using std::string;
using std::type_info;
using namespace Loki;


/*
        备注,此处的对象工厂以及克隆工厂只是为了阐述原理,不具有实际意义,
    实际中使用,难免在Create函数中传入参数,详见Loki
*/


//对象工厂
template
<
    typename TBaseClassType,                    //最底层基类类型
    typename TIdentify,                         //类的标识符
    typename TCreate = TBaseClassType* (*) ()   //类对象的生成函数
>
class CFactory
{
public:
    //注册
    const bool Register(const TIdentify& tIdC, TCreate tCreate)
    {
        return mapAssociate.insert(std::make_pair(tIdC, tCreate)).second;
    }

    //注销
    const bool Unregister(const TIdentify& tIdC)
    {
        return 1 == mapAssociate.erase(tIdC);
    }
    
    //创建
    TBaseClassType* Create(const TIdentify& tIdC)
    {
        auto it = mapAssociate.find(tIdC);
        if (it != mapAssociate.end())
        {
            return (it->second)();
        }
        /*
            若有需要,此处可以以 policy-based class 方式设计,
            让CFactory以public方式继承一个错误处理基类
        */
        return nullptr;
    }

private:
    map mapAssociate;
};


//克隆工厂
template
<
    typename TBaseClassType,                        //最底层基类类型
    typename TCreate = 
        TBaseClassType* (*) (const TBaseClassType*) //类对象的生成函数
>
class CCloneFactory
{
public:
    const bool Register(const TypeInfo& tTemC, TCreate tCreate)
    {
        return mapAssociate.insert(std::make_pair(tTemC, tCreate)).second;
    }
    const bool Unregister(const TypeInfo& typeInfoC)
    {
        return 1 == mapAssociate.erase(typeInfoC);
    }
    TBaseClassType* Create(const TBaseClassType* pObjectC)
    {
        if (pObjectC == nullptr)
        {
            return nullptr;
        }

        auto it = mapAssociate.find(typeid(*pObjectC));

        if (it != mapAssociate.end())
        {
            return (it->second)(pObjectC);
        }

        return nullptr;
    }

private:
    map mapAssociate;    //TypeInfo来自Loki
/*
        type_info::name()返回的结果只适合调试,无法保证此字符串就是实际类名称,
    并且无法保证这个字符串在整个程序中是独一无二的,不同的类可能返回相同字符串,
    所以这里不能用type_info::name()代表的字符串充当key
        此处的Key必须具备RTTI特性,否则毫无用处
*/
};


class CFather
{
public:
    CFather() : nV0(0) {}
    virtual ~CFather(){}
public:
    int nV0;
};
class CChild0 : public CFather
{
public:
    CChild0() { nV0 = 1; }
};
class CChild1 : public CFather
{
public:
    CChild1() { nV0 = 2; }
};

template
TBase* CreateTest()
{
    return new T;
}

template
TBase* CloneTest(const TBase* pTemC)
{
    return new T(static_cast(*pTemC));
}


int main()
{
    CFactory Factor;
    Factor.Register(string("Father"), CreateTest);
    Factor.Register(string("Child0"), CreateTest);
    Factor.Register(string("Child1"), CreateTest);
    int aValue[] = 
    {
        Factor.Create("Father")->nV0,   //0
        Factor.Create("Child0")->nV0,   //1
        Factor.Create("Child1")->nV0    //2
    };

    CCloneFactory CloneFactory;
    CloneFactory.Register(typeid(CFather), CloneTest);
    CloneFactory.Register(typeid(CChild0), CloneTest);
    CloneFactory.Register(typeid(CChild1), CloneTest);
    CFather* pChild1 = new CChild1;
    int nTem = CloneFactory.Create(pChild1)->nV0;   //nTem = 2

    return 0;
}

你可能感兴趣的:(第八章 对象工厂)