为什么设计模式里抽象工厂只负责创建而不负责资源管理?
資深大佬 : fyyz 27
抽象工厂只是实现了 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() 的抽象工厂更好啊。为什么很少有人写这种模式呢?
大佬有話說 (5)