工厂设计模式:工厂可以保存实例吗?

发布于 2024-10-22 02:12:36 字数 111 浏览 1 评论 0原文

我需要创建已决定使用工厂模式的同一类的多个实例。我们需要提供对工厂创建的实例的查询功能。

那么按照工厂模式的标准,工厂可以保存它创建的对象吗?或者我需要创建另一个允许对这些实例进行查询的组件?

I need to create multiple instance of the same class for which it has been decided to use factory pattern. We need to provide the querying capabilities on the instances created by the factory.

So as per the standards of factory pattern, can a factory hold the objects that it has created? or I need to create another component which will allow the querying on these instances?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔戏命师 2024-10-29 02:12:36

它当然可以保存它所创建的对象,但这意味着工厂有两个职责。如果您创建一个负责保存对所创建对象的引用的存储库类,那就更好了。如果存储库类还没有具有该 ID 的对象,则它还可以使用工厂来创建新实例。

It certainly COULD hold the object it has created, but that would mean that the factory has two responsibilities. It would be better if you created a repository class that is responsible for keeping references to the created objects. The repository class could also use the factory to create new instances if it does not already have an object with that ID.

时光磨忆 2024-10-29 02:12:36

一般来说,对象的所有者应该执行与释放或释放其创建的对象分配的资源相关的任务。

在您的示例中,工厂类(或您选择的任何模式)将执行这些任务。

In general the owner of your objects should perform tasks associated with freeing or releasing resources allocated by the objects it creates.

At your example, it is the factory class (or whatever pattern you choose) that will perform these tasks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文