自动注册工厂

引言:前两天看到关注的公众号上使用模板和宏进行工厂类的自动注册,觉得很不错,但是对模板和宏不是很熟悉,于是删去这两个要素重新编写,实现要简单一些,思路会更加清晰一点。

废话不多说,上代码。之后会对模板和宏进行复习,不一定是用在自动注册工厂类上。

#include <iostream>
#include <map>
#include <utility>
#include <functional>

using namespace std;
/*********************
*1.一个单例的工厂模式
*2.工厂的注册对象
*3.一个辅助类
*4.利用宏去进行对象的注册
*5.在派生类当中去调用
**********************/

class Message
{
public:
	virtual void _cout() {}
}; 

class registerFactory
{
public:

	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));
	}

	static void register_t(const string &key,Message *e)
	{
		registerFactory::getObj().map_.emplace(std::make_pair(key, e));
	};

	
private:
	registerFactory() {};
	registerFactory(const registerFactory&) = delete;
	registerFactory(registerFactory&&) = delete;
	inline static registerFactory& getObj()
	{
		static registerFactory instance;
		return instance;
	}
	static std::map<std::string , Message* > map_;
};
 std::map<std::string, Message* > registerFactory::map_;

class Message1 : public Message
{
public:

	void _cout() override
	{
		cout << name.c_str()<<endl;
	}
private:
	std::string name = "Message1";
};
int main()
{
	Message1 *mess = new Message1();
	registerFactory::register_t("Message1", mess);
	Message *mess1=registerFactory::produce("Message1");
	Message1 *me=dynamic_cast<Message1*>(mess1);
	me->_cout();
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ TcpSocket自动工厂类的实现可以参考以下代码: ```cpp #include <iostream> #include <map> #include <functional> #include <memory> #include <string> #include <stdexcept> #include <arpa/inet.h> #include <netinet/in.h> #include <sys/socket.h> #include <unistd.h> class TcpSocket { public: TcpSocket(int fd) : fd_(fd) { } int fd() const { return fd_; } ~TcpSocket() { if (fd_ != -1) { close(fd_); } } private: int fd_; }; class TcpServer { public: TcpServer() : fd_(socket(AF_INET, SOCK_STREAM, 0)) { if (fd_ == -1) { throw std::runtime_error("socket error"); } } void bind(const std::string& host, int port) { sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr(host.c_str()); addr.sin_port = htons(port); if (::bind(fd_, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == -1) { throw std::runtime_error("bind error"); } } void listen() { if (::listen(fd_, 128) == -1) { throw std::runtime_error("listen error"); } } std::unique_ptr<TcpSocket> accept() { int client_fd = ::accept(fd_, nullptr, nullptr); if (client_fd == -1) { throw std::runtime_error("accept error"); } return std::make_unique<TcpSocket>(client_fd); } ~TcpServer() { if (fd_ != -1) { close(fd_); } } private: int fd_; }; class TcpSocketFactory { public: TcpSocketFactory() { register_creator("tcp", []() { return std::make_unique<TcpSocket>(); }); } void register_creator(const std::string& protocol, std::function<std::unique_ptr<TcpSocket>()> creator) { creators_[protocol] = std::move(creator); } std::unique_ptr<TcpSocket> create(const std::string& url) { size_t pos = url.find("://"); if (pos == std::string::npos) { throw std::runtime_error("invalid url"); } std::string protocol = url.substr(0, pos); auto it = creators_.find(protocol); if (it == creators_.end()) { throw std::runtime_error("unsupported protocol"); } return it->second(); } private: std::map<std::string, std::function<std::unique_ptr<TcpSocket>()>> creators_; }; int main() { TcpSocketFactory factory; // 注册TcpSocket的创建函数 factory.register_creator("tcp", []() { return std::make_unique<TcpSocket>(); }); // 使用TcpSocketFactory创建TcpServer TcpServer server; server.bind("127.0.0.1", 12345); server.listen(); while (true) { std::unique_ptr<TcpSocket> client = server.accept(); std::string url = "tcp://127.0.0.1:12345"; // 假设这是客户端连接的url std::unique_ptr<TcpSocket> socket = factory.create(url); std::cout << "client connected" << std::endl; } return 0; } ``` 上述代码中,`TcpSocketFactory` 实现了一个自动工厂类,可以根据不同的协议创建不同的 `TcpSocket` 对象。在 `TcpSocketFactory` 中,我们使用 `std::map` 存储了不同协议对应的创建函数,然后在 `create` 方法中根据传入的 url 解析协议,调用对应的创建函数创建 `TcpSocket` 对象。 在 `main` 函数中,我们首先需要实例化一个 `TcpSocketFactory` 对象,并注册 `TcpSocket` 的创建函数。然后,我们创建了一个 `TcpServer` 对象,并调用 `accept` 方法接受客户端连接。在连接建立后,我们可以使用 `TcpSocketFactory` 创建新的 `TcpSocket` 对象来与客户端通信。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值