抽象工厂只是实现了 CRUD 中的 C,我想实现这样的工厂:
/* C++ */
class abstract_factory
{
};
template<typename T>
class factory:public abstract_factory
{
public:
	std::weak_ptr<T> create();
	std::weak_ptr<T> retrieve(const std::uint64_t& product_id);
	void destroy(const std::uint64_t& product_id);
private:
	std::uint64_t increment_serial_id_ = 0;
	std::map<std::uint32_t, std::shared_ptr<T> products_;
	std::uint64_t generate_id();
};
这种写法直接包含了 CRUD,同时内部也持有了对象。我觉得这种写法明显比只有 create() 的抽象工厂更好啊。为什么很少有人写这种模式呢?
|  |      1binux      2020-04-02 13:10:54 +08:00 你不还是要显式 destroy ? | 
|  |      4fyyz OP 明白了,原来这个是享元模式 | 
|      5TransAM      2020-04-02 16:23:13 +08:00 via Android  1 所以它叫工厂,而不是容器 /对象池。 |