#pragma once
#include "iostream"
class Implementor
{
public:
virtual void operatorImp() = 0;
};
class ImplementorA : public Implementor
{
public:
virtual void operatorImp() {
std::cout << __FUNCTION__ << ": A" <<std::endl;
}
};
class ImplementorB : public Implementor
{
public:
virtual void operatorImp() {
std::cout << __FUNCTION__ << " : B" << std::endl;
}
};
class ImpFactory
{
public:
static ImpFactory* getInstance() {
if (imp_factory == NULL)
{
imp_factory = new ImpFactory();
std::cout << __FUNCTION__ << std::endl;
}
return imp_factory;
}
static Implementor* makeImp(int type) {
std::cout << __FUNCTION__ << std::endl;
if (type == 1)
{
return new ImplementorA();
}
else if(type == 2)
{
return new ImplementorB();
}
else
{
return NULL;
}
}
static ImpFactory* imp_factory;
protected:
ImpFactory() {
std::cout << __FUNCTION__ << std::endl;
};
};
class Abstract
{
public:
virtual Implementor* GetImp(int type) {
std::cout << __FUNCTION__ << std::endl;
if (_imp == NULL)
{
_imp = ImpFactory::getInstance()->makeImp(type);
}
return _imp;
}
virtual void Draw() {
_imp->operatorImp();
}
protected:
Implementor* _imp;
};
class RedfinedAbstract : Abstract
{
void Draw() {
std::cout << __FUNCTION__ << std::endl;
}
};
#include "stdafx.h"
#include "Imp.h"
ImpFactory* ImpFactory::imp_factory = NULL;
调用介绍
Abstract abs;
abs.GetImp(2);
abs.Draw();
结果展示:
Abstract::GetImp
ImpFactory::ImpFactory
ImpFactory::getInstance
ImpFactory::makeImp
ImplementorB::operatorImp : B
log4cpp工厂模式的使用可参考如下:
static LayoutsFactory* layouts_factory_ = 0;\
std::auto_ptr<Layout> create_simple_layout(const FactoryParams& params);
std::auto_ptr<Layout> create_basic_layout(const FactoryParams& params);
std::auto_ptr<Layout> create_pattern_layout(const FactoryParams& params);
std::auto_ptr<Layout> create_pattern_layout(const FactoryParams& params);
std::auto_ptr<Layout> create_pass_through_layout(const FactoryParams& params);
LayoutsFactory& LayoutsFactory::getInstance()
{
if (!layouts_factory_)
{
std::auto_ptr<LayoutsFactory> lf(new LayoutsFactory);
lf->registerCreator("simple", &create_simple_layout);
lf->registerCreator("basic", &create_basic_layout);
lf->registerCreator("pattern", &create_pattern_layout);
lf->registerCreator("pass through", &create_pass_through_layout);
layouts_factory_ = lf.release();
}
return *layouts_factory_;
}
void LayoutsFactory::registerCreator(const std::string& class_name, create_function_t create_function)
{
const_iterator i = creators_.find(class_name);
if (i != creators_.end())
throw std::invalid_argument("Layout creator for type name '" + class_name + "' allready registered");
creators_[class_name] = create_function;
}
std::auto_ptr<Layout> LayoutsFactory::create(const std::string& class_name, const params_t& params)
{
const_iterator i = creators_.find(class_name);
if (i == creators_.end())
throw std::invalid_argument("There is no layout with type name '" + class_name + "'");
return (*i->second)(params);
}
bool LayoutsFactory::registed(const std::string& class_name) const
{
return creators_.find(class_name) != creators_.end();
}
这个是log4cpp采用的工厂模块,将每一个模块关联了名字和函数指针,注册的时候只需要知道某个模块的名字就可以直接注册了