我知道有很多关于这两种模式之间差异的帖子,但有一些东西我找不到。

From what I have been reading, I see that the factory method pattern allows you to define how to create a single concrete product but hiding the implementation from the client as they will see a generic product. My first question is about the abstract factory. Is its role to allow you to create families of concrete objects in (that can depend on what specific factory you use) rather than just a single concrete object? Does the abstract factory only return one very large object or many objects depending on what methods you call?

我最后两个问题是关于一句我在很多地方都见过的引语,我不能完全理解:

两者之间的一个区别是 使用抽象工厂模式,a 类委托的责任 对象实例化到另一个对象 通过合成,而工厂 方法模式使用继承和 类依赖于子类来处理 所需的对象实例化。

我的理解是,工厂方法模式有一个Creator接口,它将使ConcreteCreator负责知道要实例化哪个ConcreteProduct。这就是使用继承来处理对象实例化的意思吗?

现在,关于引用,抽象工厂模式是如何通过组合将对象实例化的责任委托给另一个对象的?这是什么意思?在我看来,抽象工厂模式似乎也使用继承来完成构造过程,但我仍然在学习这些模式。

任何帮助,特别是最后一个问题,将非常感激。


当前回答

有相当多的定义。基本上,描述工厂模式的三种常用方法是

简单的工厂

基于条件的简单对象创建方法/类。

工厂方法

使用子类提供实现的工厂方法设计模式。

抽象工厂

抽象工厂设计模式产生相关或依赖的对象族,而不指定它们的具体类。

下面的链接非常有用-工厂比较-重构。guru

其他回答

抽象工厂:工厂中的工厂;将独立但相关/依赖的工厂分组在一起,而不指定它们的具体类的工厂。 抽象工厂实例

Factory:它提供了一种将实例化逻辑委托给子类的方法。 工厂模式示例

我的第一个问题是关于抽象工厂。它的角色是否允许您在其中创建一系列具体对象(这取决于您使用的具体工厂),而不仅仅是单个具体对象?

是的。抽象工厂的目的是:

提供一个接口,用于创建一系列相关或依赖的对象,而无需指定它们的具体类。


根据调用的方法,抽象工厂只返回一个非常大的对象还是多个对象?

理想情况下,它应该为客户端调用的每个方法返回一个对象。

我的理解是,工厂方法模式有一个Creator接口,它将使ConcreteCreator负责知道要实例化哪个ConcreteProduct。这就是使用继承来处理对象实例化的意思吗?

是的。工厂方法使用继承。

抽象工厂模式委托对象实例化的责任通过组合到另一个对象?这是什么意思?

AbstractFactory定义了一个FactoryMethod,而ConcreteFactory负责构建一个ConcreteProduct。只需按照本文中的代码示例进行操作。

你可以在SE的相关文章中找到更多细节:

工厂模式和抽象工厂模式之间的基本区别是什么?

设计模式:工厂vs工厂方法vs抽象工厂

A)工厂方法模式

工厂方法是一种创建设计模式,它提供了创建对象的接口,但允许子类改变将要创建的对象的类型。

如果在基类和扩展它的子类中有一个创建方法,您可能会考虑工厂方法。

B)抽象工厂模式

抽象工厂是一种创造性的设计模式,允许在不指定具体类的情况下产生相关或依赖的对象族。

什么是“对象族”?例如,以这组类为例:传输+引擎+控制。这些可能有几种变体:

1-汽车+内燃机+方向盘

2-平面+ JetEngine +轭

如果你的程序不使用产品族,那么你就不需要抽象工厂。

同样,很多人混淆了抽象工厂模式和简单的工厂类声明为抽象。不要那样做!

裁判:https://refactoring.guru/design-patterns/factory-comparison

abstract factory design pattern with realtime example: what is an abstract factory design pattern? It is similar to the factory method design pattern. we need to use this pattern when we have multiple factories. there will be a grouping of factories defined in this pattern. factory method pattern is a subset of abstract factory design pattern. They have the same advantages as factory patterns. abstract factory relies on object composition whereas the factory method deals with inheritance. factory design pattern in java with a realtime example: what is the factory design pattern? it is mostly used design in object-oriented programming. It is one of the creational patterns. it is all about creating instances. Clients will create the object without exposed to object creational logic. it is widely used in different frameworks ex: the spring framework. we use this pattern when the class doesn’t know the objects of another it must create. Realtime example: when our car breaks down on the road. We need to inform the repairman about what type of vehicle we are using so that repairman will carry tools to fix the repair. as per our input, the repairman will fix the issue and make it ready for us to travel again. There are a few built-in methods that use these patterns. example getInstance() method in JavaUtilcalendar class. With help of getInstance(), we can get objects whenever we execute this method. Javautilcalendar : getInstance() is method return object. https://trendydevx.com/factory-design-pattern-in-java-with-realtime-example/

My conclusion: there is no difference. Why? Because I cannot see any justification to equip objects other than factories with factory methods - otherwise you get a violation of the separation of responsibility principle. In addition, I cannot see any difference between a factory with a single factory method and a factory with multiple factory methods: both create "families of related objects" unless anyone can prove that a single-family-member family is not a family. Or a collection that contains a single item is not a collection.