实现设计模式:工厂模式
来源:互联网 发布:淘宝信用贷款逾期一天 编辑:程序博客网 时间:2024/06/05 03:58
工厂模式将创建对象的代码从客户代码中分离,提供了一种在大系统中管理创建众多对象的有效方法。当从配置文件或流中创建对象时,经常采用这种模式。
这里提供一种通用实现,可以作为参考。
#include <map>#include <boost/function.hpp>namespace factory{template<class T, class Allocator=std::allocator<T> >class simple_factory{public:>template<typename Key>T* operator()(Key&){T* pT=static_cast<T*>(m_allocate.allocate(1));m_allocate.construct(pT, T());return pT;}private:Allocator m_allocate;};template<typename Key, class T, class Allocator=std::allocator<T> >class factory{public:typedef Allocator allocator_type;typedef typename Allocator::value_type value_type;typedef typename Allocator::pointer pointer;typedef typename Allocator::reference reference;typedef Key key_type;template<class Factory>void insert(const key_type& key, Factory& factory){m_factories.insert(std::make_pair(key, factory));}template<class Derive>void insert_class(const key_type& key){insert(key, simple_factory<Derive, allocator_type>());}pointer construct(const key_type& key) const{factories_type::const_iterator it=m_factories.find(key);if(it!=m_factories.end())return (it->second)(key);return NULL;}void destroy(pointer p){allocator_type a;a.destroy(p);a.deallocate(p, 1);}private: typedef boost::function<pointer (key_type)> factory_type; typedef std::map<key_type, factory_type, std::less<key_type>, typename allocator_type::rebind<std::pair<const typename key_type, typename factory_type> >::other > factories_type;factories_type m_factories;};}
为了保证创建和销毁对象的一致性,工厂类提供了销毁对象的方法。
以下是个测试例子:
struct Base{int x;};struct A : public Base{A() { x=1; }};struct B : public Base{B() { x=2; }};void test(){factory::factory<int, Base> fact;fact.insert_class<A>(1);fact.insert_class<B>(2);Base* pBase=fact.construct(1);cout<<pBase->x<<endl;fact.destroy(pBase);}
- 实现设计模式:工厂模式
- 设计模式之C++实现 - 工厂模式
- 设计模式 -- 简单工厂模式 -- c++实现
- C++实现设计模式: Factory 工厂模式
- C++设计模式实现--工厂模式
- C++实现设计模式之工厂模式
- C++实现设计模式: Factory 工厂模式
- 设计模式 工厂方法模式 C++实现
- C++设计模式实现--工厂模式
- C++实现设计模式: Factory 工厂模式
- 设计模式1--工厂模式--Java实现
- 设计模式C++实现一 工厂模式
- 设计模式C++实现--工厂模式
- 设计模式--工厂模式(C++实现)
- 设计模式--抽象工厂模式(C++实现)
- 【设计模式】Lambda实现工厂模式
- Java实现设计模式之工厂模式
- 设计模式--工厂模式--Java实现
- NS3中日志信息输出到文件
- Cities
- ASP.NET MVC3.0中同一View如何返回多个Model或数据集
- 【AWK】Scanning all Elements of an Array--每日一译2013-02-05
- SVN中的Import
- 实现设计模式:工厂模式
- Android 内存优化
- 织梦当前位置不显示最后一个符号
- Android混淆注意事项
- HDOJ 4576
- NAND FLASH扇区管理及初始化
- EOS开发五
- 砖6 内核线程的创建 kthread_create
- 哲学家进餐问题