抽象工厂模式
来源:互联网 发布:淘宝下架眼药水 编辑:程序博客网 时间:2024/06/06 01:42
#pragma once
#pragma warning (disable:4786)
/********************************************************************
created: 2008-10-22
author: mark
filename: dynclass.hpp
purpose: a generic factory method lib
Copyright (C) 2008 - All Rights Reserved
*********************************************************************/
#include <string>
#include <map>
namespace lib
{
//通用工厂类,根据传入的id创建产品对象
template <typename BaseType, typename KeyType=std::string>
class factory
{
private:
typedef std::auto_ptr<BaseType> (*BaseCreateFunc)();
typedef std::map<KeyType, BaseCreateFunc> FuncRegistry;
public:
static factory<BaseType, KeyType>& get_instance() //static singleton,不适用于多线程
{
static factory<BaseType, KeyType> obj;
return obj;
}
std::auto_ptr<BaseType> create(const KeyType& id) const
{
std::auto_ptr<BaseType> obj;
FuncRegistry::const_iterator regEntry = _registry.find(id);
if (regEntry != _registry.end())
{
obj = regEntry->second();
}
return obj;
}
void _register_create_function(const KeyType& id, BaseCreateFunc func){_registry[id] = func;}
private:
factory(void){}
factory(const factory& other);
factory operator=(const factory& other);
private:
FuncRegistry _registry;
};
//类型(DerivedType)注册类,只要在DerivedType类定义(DerivedType.cpp文件)的最后声明一个 lib::factory_register<Base, Derived> reg(id);对象即可
template <typename BaseType, typename DerivedType, typename KeyType=std::string>
class factory_register
{
public:
factory_register(const KeyType& id)
{
factory<BaseType, KeyType>::get_instance()._register_create_function(id, _create_instance);
}
private:
static std::auto_ptr<BaseType> _create_instance(){return std::auto_ptr<BaseType>(new DerivedType);}
private:
friend class factory<BaseType, KeyType>;
};
}
#pragma warning (disable:4786)
/********************************************************************
created: 2008-10-22
author: mark
filename: dynclass.hpp
purpose: a generic factory method lib
Copyright (C) 2008 - All Rights Reserved
*********************************************************************/
#include <string>
#include <map>
namespace lib
{
//通用工厂类,根据传入的id创建产品对象
template <typename BaseType, typename KeyType=std::string>
class factory
{
private:
typedef std::auto_ptr<BaseType> (*BaseCreateFunc)();
typedef std::map<KeyType, BaseCreateFunc> FuncRegistry;
public:
static factory<BaseType, KeyType>& get_instance() //static singleton,不适用于多线程
{
static factory<BaseType, KeyType> obj;
return obj;
}
std::auto_ptr<BaseType> create(const KeyType& id) const
{
std::auto_ptr<BaseType> obj;
FuncRegistry::const_iterator regEntry = _registry.find(id);
if (regEntry != _registry.end())
{
obj = regEntry->second();
}
return obj;
}
void _register_create_function(const KeyType& id, BaseCreateFunc func){_registry[id] = func;}
private:
factory(void){}
factory(const factory& other);
factory operator=(const factory& other);
private:
FuncRegistry _registry;
};
//类型(DerivedType)注册类,只要在DerivedType类定义(DerivedType.cpp文件)的最后声明一个 lib::factory_register<Base, Derived> reg(id);对象即可
template <typename BaseType, typename DerivedType, typename KeyType=std::string>
class factory_register
{
public:
factory_register(const KeyType& id)
{
factory<BaseType, KeyType>::get_instance()._register_create_function(id, _create_instance);
}
private:
static std::auto_ptr<BaseType> _create_instance(){return std::auto_ptr<BaseType>(new DerivedType);}
private:
friend class factory<BaseType, KeyType>;
};
}
测试代码:
#include <iostream>
#include "factory.hpp"
//基类
class Base
{
public:
virtual void print(void)
{
std::cout<<"base class"<<std::endl;
}
};
lib::factory_register<Base, Base> reg1("base"); //注册Base类型,定义一个file scope的变量,通常应该置于Base.cpp文件结尾
//派生类
class Derived:public Base
{
public:
virtual void print(void)
{
std::cout<<"derived class"<<std::endl;
}
};
lib::factory_register<Base, Derived> reg2("derived"); //注册Derived类型,定义一个file scope的变量,通常应该置于Derived.cpp文件结尾
//声明一个全局函数(简化操作,非必需)
lib::factory<Base>& glb_GetFactory(void)
{
return lib::factory<Base>::get_instance();
}
int main(int argc, char* argv[])
{
std::auto_ptr<Base> base=glb_GetFactory().create("base");
if (base.get())
{
base->print();
}
std::auto_ptr<Base> derived=glb_GetFactory().create("derived");
if (derived.get())
{
derived->print();
}
system("pause");
return 0;
}
#include "factory.hpp"
//基类
class Base
{
public:
virtual void print(void)
{
std::cout<<"base class"<<std::endl;
}
};
lib::factory_register<Base, Base> reg1("base"); //注册Base类型,定义一个file scope的变量,通常应该置于Base.cpp文件结尾
//派生类
class Derived:public Base
{
public:
virtual void print(void)
{
std::cout<<"derived class"<<std::endl;
}
};
lib::factory_register<Base, Derived> reg2("derived"); //注册Derived类型,定义一个file scope的变量,通常应该置于Derived.cpp文件结尾
//声明一个全局函数(简化操作,非必需)
lib::factory<Base>& glb_GetFactory(void)
{
return lib::factory<Base>::get_instance();
}
int main(int argc, char* argv[])
{
std::auto_ptr<Base> base=glb_GetFactory().create("base");
if (base.get())
{
base->print();
}
std::auto_ptr<Base> derived=glb_GetFactory().create("derived");
if (derived.get())
{
derived->print();
}
system("pause");
return 0;
}
0 0
- 工厂模式 -- 抽象工厂
- 工厂模式-抽象工厂
- 工厂模式-抽象工厂
- 工厂模式/抽象工厂模式
- 工厂模式&&抽象工厂模式
- 工厂模式---抽象工厂模式
- 工厂模式&抽象工厂模式
- 工厂模式,抽象工厂模式
- 【模式】抽象工厂模式
- 简单工厂、工厂模式、抽象工厂模式
- 简单工厂、工厂模式、抽象工厂模式
- 简单工厂&工厂模式&抽象工厂模式
- 工厂方法,抽象工厂模式
- 2,工厂模式--抽象工厂
- C#工厂模式-抽象工厂
- 工厂模式与抽象工厂
- 抽象工厂设计模式
- 抽象工厂模式
- 桥接模式,场景与实现
- Linux netlink机制介绍
- 使用udp发送大于128K的消息会报ENOBUFS的错误
- onpain重绘
- Repast HPC 2.0安装及实例模型运行总结
- 抽象工厂模式
- STL(Standard Template Library)
- 程序员面试题精选100题(22)-整数二进制表示中1的个数[算法]
- oracle 12c 创建 pdb user
- 如何忽略usb host 模式设备连接确认对话框
- JNI使用总结
- 工厂类模式
- 黑马程序员---java学习笔记之IO3
- sql连接操作详解