设计模式。抽象工厂与工厂方法[英] Design Patterns: Abstract Factory vs Factory Method

本文是小编为大家收集整理的关于设计模式。抽象工厂与工厂方法的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

注意:问题在帖子的末尾.

我已经阅读了有关抽象工厂与工厂方法的其他Stackoverflow线程.我了解每种模式的意图.但是,我对定义尚不清楚.

工厂方法定义了一个接口 用于创建对象,但让我们 子类决定哪个 实例化.工厂方法让 课程将实例化为 子类.

相比之下,抽象工厂 提供了创建的界面 相关或依赖的家庭 对象没有指定他们的 具体类.

- John feminella

抽象工厂看起来与工厂方法非常相似.我绘制了一些UML课程来说明我的观点.

注意:

  • 该图来自 www.yuml.com ,因此它们不是完全方向的.但是它是一项免费服务:).
  • 这些图可能不是完美的.我仍在学习 gof 设计模式.

工厂方法:

工厂方法

抽象工厂(只有1个成员):

摘要工厂(只有1个成员)

抽象工厂(更多成员):

 alt text

问题:

  1. 如果抽象工厂只有一个创建者和一个产品,那么它仍然是抽象工厂模式吗? (用于创建家庭的接口)
  2. 工厂方法可以从接口创建混凝土创建者,还是必须来自类? (类将实例化为子类)
  3. 如果抽象工厂只能拥有一个创作者和一个产品,则是抽象工厂和工厂方法之间的唯一区别,前者的创建者是一个接口和后者的创建者是类?

推荐答案

希望这会有所帮助.它描述了各种工厂.我使用头部首次设计模式作为我的参考书.我使用 yuml.me 到图.

静态工厂

是具有静态方法的类,用于产品各种子类型的产品.

 static Factory

简单工厂

是可以生产各种子类型产品的类. (它比静态工厂要好.添加新类型时,基本产品类不需要更改,只有简单的工厂类)

 simpl FactoryT

工厂方法

包含一种生产与其类型相关的产品的方法. (这比简单的工厂更好,因为该类型被推迟到子级.)

工厂方法

抽象工厂

产生相关类型的家庭.它与工厂方法明显不同,因为它具有多种类型的方法. (这很复杂,请参阅下图以获取更好的现实生活示例).

 Abstract Factory

.NET框架的示例

dbFactoriesProvider是一个简单的工厂,因为它没有子类型. DBFACTORYPROVIDER是一个抽象的工厂,因为它可以创建各种相关的数据库对象,例如连接和命令对象.

来自.net Framework的抽象工厂

其他推荐答案

两种模式肯定是相关的!

模式之间的差异通常是意图.

工厂方法的 是"定义了创建对象的接口,但是让子类决定要实例化的类.工厂方法让类延迟实例化对子类别."

意图 抽象工厂的是"为创建相关对象或依赖对象的家庭提供的界面而无需指定其具体类别."

纯粹基于这些意图陈述(从GOF引用),我想说的是工厂方法在某种意义上是"退化" 抽象工厂与一个家庭的家庭一个.

它们通常在实施方面有所不同,因为工厂方法比抽象工厂.

它们也与实施有关.如GOF书中所述,

AbstractFactory仅声明用于创建产品的接口.由混凝土子类取决于实际创建它们.这样做的最常见方法是为每种产品定义一种工厂方法.

这个 c2 wiki 也有一些有趣的讨论.

其他推荐答案

似乎忽略了OP的(出色)问题列表.当前的答案仅提供重新定义.因此,我将尝试简单地解决原始问题.

  1. 如果抽象工厂只有一个创建者和一个产品,那么它仍然是抽象工厂模式吗? (一个接口 创建家庭)

no .一个抽象的工厂必须创建多种产品来制造"相关产品家族".规范GOF示例创建ScrollBar()和Window().优势(和目的)是抽象工厂可以在其多个产品中执行一个共同的主题.

  1. 工厂方法可以从接口创建混凝土创建者,还是必须来自类? (课程推迟 子类实例化)

首先,我们必须注意,当GoF写下他们的书时,Java和C#都不存在.术语接口的GOF使用与特定语言引入的接口类型无关.因此,可以从任何API创建混凝土创建者.模式中的重要一点是API消耗了自己的工厂方法,因此,只有一种方法的接口不能比它是抽象的工厂更多.

  1. 如果抽象工厂只能拥有一个创建者和一个产品,则是抽象工厂和 工厂方法前者的创建者是接口,后者的创建者是类?

遵循上述答案,这个问题不再有效.但是,如果您认为抽象出厂方法和工厂方法之间的唯一区别是创建的产品数量,请考虑客户如何消耗这些模式.通常将抽象工厂注入其客户端,并通过构成/委托进行调用.必须继承工厂方法.因此,这一切都回到了旧构图与继承辩论.

但是这些答案提出了第四个问题!

  1. 因为,一个只有一种方法的接口不能是工厂方法比抽象出厂,我们怎么样致电 仅使用一种方法的创建接口?

如果该方法是静态的,则通常称为A 静态工厂.如果该方法是非静态的,则通常称为A 简单工厂.这些都不是GOF模式,但实际上它们更常用!

本文地址:https://www.itbaoku.cn/post/627486.html

问题描述

Note: Questions are at the end of the post.

I have read the other stackoverflow threads regarding Abstract Factory vs Factory Method. I understand the intent of each pattern. However, I am not clear on the definition.

Factory Method defines an interface for creating an object, but lets subclasses decide which of those to instantiate. A factory method lets classes defer instantiation to subclasses.

By contrast, an Abstract Factory provides an interface for creating families of related or dependent objects without specifying their concrete classes.

-John Feminella

The Abstract Factory looks very similar to the Factory Method. I have drawn a few UML classes to illustrate my point.

Note:

  • The diagram are from www.yuml.com so they are not perfectly oriented. But its a free service :).
  • The diagrams may not be perfect. I am still learning the GoF design patterns.

Factory Method:

Factory Method

Abstract Factory (only 1 member):

Abstract Factory (only 1 member)

Abstract Factory (more members):

alt text

Questions:

  1. If the Abstract Factory has only one creator and one product, is it still the Abstract Factory pattern? (an interface for creating familes)
  2. Can the Factory Method concrete creator be created from an Interface or does it have to be from a class? (classes defer instantiations to subclasses)
  3. If the Abstract Factory can have only one creator and one product, is the only difference between the Abstract Factory and the Factory Method that the creator for the former is an Interface and the creator for the latter is a Class?

推荐答案

Hope this helps. It describes the various types of factories. I used the Head First Design Patterns book as my reference. I used yuml.me to diagram.

Static Factory

Is a class with a Static Method to product various sub types of Product.

Static Factory

Simple Factory

Is a class that can produce various sub types of Product. (It is better than the Static Factory. When new types are added the base Product class does not need to be changed only the Simple Factory Class)

Simple Factoryt

Factory Method

Contains one method to produce one type of product related to its type. (It is better than a Simple Factory because the type is deferred to a sub-class.)

Factory Method

Abstract Factory

Produces a Family of Types that are related. It is noticeably different than a Factory Method as it has more than one method of types it produces. (This is complicated refer to next diagram for better real-life example).

Abstract Factory

Example From The .NET Framework

DbFactoriesProvider is a Simple Factory as it has no sub-types. The DbFactoryProvider is an abstract factory as it can create various related database objects such as connection and command objects.

Abstract Factory From .NET Framework ​​​

其他推荐答案

The two patterns are certainly related!

The difference between patterns is generally in intent.

The intent of Factory Method is "Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses."

The intent of Abstract Factory is "Provide an interface for creating families of related or dependent objects without specifying their concrete classes."

Based purely on these intent statements (quoted from GoF), I would say that indeed Factory Method is in some sense a "degenerate" Abstract Factory with a family of one.

They generally tend to differ in implementation, as Factory Method is a good deal simpler than Abstract Factory.

They are related also in implementation however. As noted in the GoF book,

AbstractFactory only declares an interface for creating products. It's up to ConcreteProduct subclasses to actually create them. The most common way to do this is to define a factory method for each product.

This c2 wiki also has some interesting discussion on this topic.

其他推荐答案

It seems that the OP's list of (excellent) questions has been ignored. Current answers merely offer rehashed definitions. So I will attempt to address the original questions concisely.

  1. If the Abstract Factory has only one creator and one product, is it still the Abstract Factory pattern? (an interface for creating familes)

No. An Abstract Factory must create more than one product to make a "family of related products". The canonical GoF example creates ScrollBar() and Window(). The advantage (and purpose) is that the Abstract Factory can enforce a common theme across its multiple products.

  1. Can the Factory Method concrete creator be created from an Interface or does it have to be from a class? (classes defer instantiations to subclasses)

First, we must note that neither Java nor C# existed when the GoF wrote their book. The GoF use of the term interface is unrelated to the interface types introduced by particular languages. Therefore, the concrete creator can be created from any API. The important point in the pattern is that the API consumes its own Factory Method, so an interface with only one method cannot be a Factory Method any more than it can be an Abstract Factory.

  1. If the Abstract Factory can have only one creator and one product, is the only difference between the Abstract Factory and the Factory Method that the creator for the former is an Interface and the creator for the latter is a Class?

This question is no longer valid, following the answers above; however, if you are left thinking that the only difference between Abstract Factory and Factory Method is the number of products created, consider how a client consumes each of these patterns. An Abstract Factory is typically injected into its client and invoked via composition/delegation. A Factory Method must be inherited. So it all comes back to the old composition vs. inheritance debate.

But these answers have raised a fourth question!

  1. Since, an interface with only one method cannot be a Factory Method any more than it can be an Abstract Factory, what do we call a creational interface with only one method?

If the method is static, it is commonly called a Static Factory. If the method is non-static, it is commonly called a Simple Factory. Neither of these is a GoF pattern, but in practice they are far more commonly used!