我打算创建一个工厂模式示例。我的目标是我不必更改工厂代码。当我添加新的派生类时,它们将自动在工厂中工作。以下是我为注册系统提出的建议。它使用单件工厂。 他们使用单身工厂的垮台了吗? 我打算在一些RPC服务器代码中使用它。
#include <string>
#include <iostream>
#include <stdexcept>
#include <map>
#include <memory>
namespace configNM{
class config{
public:
config(const std::string & filename);
config(){};
std::string getConfFileName()const{return _confFilename;};
private:
std::string _confFilename;
protected:
};
class configColumnHeaders:public config{
private:
static bool registered;
configColumnHeaders(){};
configColumnHeaders(const std::string & filename):config(filename){std::cout << "configColumnHeaders" << std::endl;};
protected:
public:
static std::unique_ptr<config> create(std::string in){
std::cout << "creating configColumnHeaders" << std::endl;
std::unique_ptr<configColumnHeaders> MyPtr(new configColumnHeaders(in));
return std::move(MyPtr);
};
};
class configColumnsNoHeaders:public config{
private:
configColumnsNoHeaders(){};
configColumnsNoHeaders(const std::string & filename):config(filename){std::cout << "configColumnsNoHeaders" << std::endl;};
static bool registered;
protected:
public:
static std::unique_ptr<config> create(std::string in){
std::unique_ptr<configColumnsNoHeaders> MyPtr(new configColumnsNoHeaders(in));
return std::move(MyPtr);
};
};
class configFixed:public config{
private:
configFixed(){};
configFixed(const std::string & filename):config(filename){std::cout << "configFixed" << std::endl;};
static bool registered;
protected:
static std::unique_ptr<config> create(std::string in){
std::unique_ptr<configFixed> MyPtr(new configFixed(in));
return std::move(MyPtr);
};
public:
};
class configFactory{
public:
typedef std::unique_ptr<config> (*configCreateFunction)(std::string);
std::map<std::string,configCreateFunction> registeredConfigs;
static configFactory* instance(){
static configFactory factory;
return &factory;
}
bool registerConfig(const std::string & name,std::unique_ptr<config> (createFunction)(std::string)){
registeredConfigs.insert(std::make_pair(name,createFunction));
return true;
}
std::unique_ptr<config> createConfig(const std::string& name, std::string filename)
{
return registeredConfigs[name](filename);
}
};
}
using namespace configNM;
config::config(const std::string & filename){
_confFilename=filename;
}
bool configColumnHeaders::registered = configFactory::instance()->registerConfig("column_headers", configColumnHeaders::create);
bool configColumnsNoHeaders::registered = configFactory::instance()->registerConfig("columns_no_headers", configColumnsNoHeaders::create);
bool configFixed::registered = configFactory::instance()->registerConfig("fixed", configFixed::create);
int main(void){
config myConfig("columnHeaders.json");
auto myPtr = configFactory::instance()->createConfig("column_headers","parameter");
auto myPtr2 = configFactory::instance()->createConfig("columns_no_headers","parameter");
auto myPtr3 = configFactory::instance()->createConfig("fixed","parameter");
return 0;
}