关闭

C++11实现一个自动注册的工厂

发表于:2016-1-19 10:19

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:qicosmos(江南)    来源:51Testing软件测试网采编

  对象工厂的辅助类的代码
struct factory
{
template<typename T>
struct register_t
{
register_t(const std::string& key)
{
factory::get().map_.emplace(key, []{ return new T; });
}
};
private:
inline static factory& get()
{
static factory instance;
return instance;
}
static std::map<std::string, FunPtr> map_;
};
  对象工厂的辅助类register_t是工厂类的一个内部模版类,非常简单,只有一个构造函数,这个构造函数中调用了factory的私有变量map_,并往map_中插入了key和泛型对象的构造器。这里用到了C++11的一个新特性:内部类可以通过外部类的实例访问外部类的私有成员,所以register_t可以直接访问factory的私有变量map_。
  自动注册的代码
  #define REGISTER_MESSAGE_VNAME(T) reg_msg_##T##_
  #define REGISTER_MESSAGE(T, key, ...) static factory::register_t<T> REGISTER_MESSAGE_VNAME(T)(key, __VA_ARGS__);
  在派生类中调用宏注册自己:
  class Message1 : public Message
  {
  //……
  };
  REGISTER_MESSAGE(Message1, "message1");
  自动注册的关键是通过一个宏来生成静态全局的register_t的实例,因为register_t的实例是用来向工厂注册目标对象的构造器。所以仅仅需要在派生类中调用这个宏就可以实现自动至注册了,而无需修改原有代码。
  我们还可以添加智能指针接口,无需让用户管理原始指针,甚至让工厂能创建带任意参数的对象。
  Factory最终的实现
#include <map>
#include <string>
#include <functional>
#include <memory>
#include "Message.hpp"
struct factory
{
template<typename T>
struct register_t
{
register_t(const std::string& key)
{
factory::get().map_.emplace(key, [] { return new T(); });
}
template<typename... Args>
register_t(const std::string& key, Args... args)
{
factory::get().map_.emplace(key, [&] { return new T(args...); });
}
};
static Message* produce(const std::string& key)
{
if (map_.find(key) == map_.end())
throw std::invalid_argument("the message key is not exist!");
return map_[key]();
}
static std::unique_ptr<Message> produce_unique(const std::string& key)
{
return std::unique_ptr<Message>(produce(key));
}
static std::shared_ptr<Message> produce_shared(const std::string& key)
{
return std::shared_ptr<Message>(produce(key));
}
private:
factory() {};
factory(const factory&) = delete;
factory(factory&&) = delete;
static factory& get()
{
static factory instance;
return instance;
}
static std::map<std::string, std::function<Message*()>> map_;
};
std::map<std::string, std::function<Message*()>> factory::map_;
#define REGISTER_MESSAGE_VNAME(T) reg_msg_##T##_
#define REGISTER_MESSAGE(T, key, ...) static factory::register_t<T> REGISTER_MESSAGE_VNAME(T)(key, ##__VA_ARGS__);
  示例
class Message
{
public:
virtual ~Message() {}
virtual void foo()
{
}
};
#include "MessageFactory.hpp"
#include "Message.hpp"
class Message1 : public Message
{
public:
Message1()
{
std::cout << "message1" << std::endl;
}
Message1(int a)
{
std::cout << "message1" << std::endl;
}
~Message1()
{
}
void foo() override
{
std::cout << "message1" << std::endl;
}
};
//REGISTER_MESSAGE(Message1, "message1", 2);
REGISTER_MESSAGE(Message1, "message1");
#include "Message1.hpp"
int main()
{
Message* p = factory::produce("message1");
p->foo();   //Message1
auto p2 = factory::produce_unique("message1");
p2->foo();
}
  总结
  使用C++11,仅仅需要几十行代码就可以实现一个自动注册的对象工厂,消除了长长的swithc-case语句,还遵循了开闭原则,简洁而优雅。
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号