什么时候使用抽象工厂模式?

发布于 2024-10-05 08:16:37 字数 2903 浏览 1 评论 0原文

我试图为我自己和我的团队简洁地描述何时使用工厂。我遇到了以下相关问题,这在一定程度上有所帮助:

基于这些链接和一堆其他来源(在底部列出),我提出了以下内容:

何时使用抽象工厂模式:< /strong>

  • 当您使用接口 var 或“new”运算符时
    • 例如,用户 user = new ConcreteUserImpl();
  • 并且您正在编写的代码在某些时候应该是可测试/可扩展的

解释:

  • 接口本质上意味着多种实现(有利于单元测试)
  • 接口变量意味着符合 OCP 和 LSP 的代码(支持子-classing)
  • 使用“new”运算符会破坏 OCP/DI,因为高度耦合的类很难测试或更改

“我要为每种对象类型创建一个工厂吗?这似乎太过分了。”

  • 否,您可以拥有一个(或几个)工厂来生成大量(通常是相关的)对象类型,
  • 例如 appFactory.createUser(); appFactory.createCatalog(); 化

何时不使用工厂:

  • 新对象非常简单,不太可能被子类
    • 例如List list = new ArrayList();
  • 新对象没有兴趣测试
    • 没有依赖项
    • 不执行任何相关或长期运行的工作
    • 例如 Logger log = new SimpleLogger();

参考文献:


我的问题是: 我的总结准确吗?有意义吗?有什么是我忽略的吗?

提前致谢。

I'm trying to succinctly describe when to use a factory, for both myself and my team. I ran across the following related questions, which helped somewhat:

Based on these links, and a bunch of other sources (listed at the bottom), I've come up with the following:

When to use the abstract factory pattern:

  • when you use an interface var or the 'new' operator
    • e.g. User user = new ConcreteUserImpl();
  • and the code you are writing should be testable / extensible at some point

Explanation:

  • interfaces by their very nature imply multiple implementations (good for unit testing)
  • interface vars imply OCP- and LSP-compliant code (support sub-classing)
  • use of the 'new' operator breaks OCP/DI, because highly-coupled classes are hard to test or change

"Do I create a factory for every object type? That seems excessive."

  • no, you can have one (or a few) factories that produce a lot of (usually related) object types
  • e.g. appFactory.createUser(); appFactory.createCatalog(); etc.

When NOT to use a factory:

  • the new object is very simple and unlikely to be sub-classed
    • e.g. List list = new ArrayList();
  • the new object is not interesting to test
    • has no dependencies
    • performs no relevant or long-running work
    • e.g. Logger log = new SimpleLogger();

References:


My question is: is my summary accurate, and does it make sense? Is there anything I've overlooked?

Thanks in advance.

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

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

发布评论

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

评论(3

大姐,你呐 2024-10-12 08:16:37

我还想说,当您有想要的特定实现时,不要使用工厂。继续 List 示例,我知道我需要一个 ArrayList 因为我正在进行随机访问。当我自己可以做的时候,我不想依赖工厂来解决这个问题。

相反,当我不想知道具体的子类时,我可以使用工厂并让它担心实际实例化哪个对象。

我想我建议您在“何时使用抽象工厂模式”中添加一个项目符号,即“并且您并不真正关心您获得哪个具体子类”,反之亦然的是“何时不使用工厂” ”。

编辑:小心避免 通用工具构建工厂工厂

I'd also say don't use a factory when you have a particular implementation that you want. To continue the List example, I know that I want an ArrayList because I'm doing random access. I don't want to rely on a factory getting this right when I can do it myself.

Conversely, when I don't want to know about the concrete subclass then I can use a factory and let it worry about which object to actually instantiate.

I guess I'd suggest that you add a bullet to the "when to use the abstract factory pattern" that says "and you don't really care which concrete subclass you get", and the converse to "when not to use a factory".

EDIT: Be careful to avoid the general-purpose tool-building factory factory factory.

稚然 2024-10-12 08:16:37

一般来说,当您希望能够通过外部配置切换实现时使用它。

JDBC 和 JAXP 就是很好的例子。如需更多示例,请查看此答案

In general, use it when you want to be able to switch of implementation by external configuration.

JDBC and JAXP are excellent examples. For more examples, check this answer.

街角迷惘 2024-10-12 08:16:37

抽象工厂模式提供了一种封装具体工厂的方法,这些工厂彼此共享一些共性,这意味着它们实现相同的接口/抽象类。

每当您想要控制对象的初始化时,您就需要使用工厂模式,而不是将控制权交给使用者。

Abstract Factory pattern provides with a way to encapsulate concrete factories that share some commonality with each other, meaning they implement same interface/abstract class.

You need to use factory pattern whenever you want to control the initialization of your objects, instead of giving the control to the consumer.

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