工厂方法实现之间有什么区别? [英] What's the difference between Factory Method implementations?

查看:59
本文介绍了工厂方法实现之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GoF书中指出,有两种方法可以实现Factory Method:

应用工厂方法"模式时,请考虑以下问题:

  1. 两个主要品种.当Creator类为抽象类时,Factory Method模式的两个主要变体是这种情况类,并且不提供其工厂方法的实现声明,以及造物主是具体班级的情况,以及提供了工厂方法的默认实现.这也是可能有一个定义默认值的抽象类实施,但这并不常见.第一种情况要求子类来定义实现,因为没有合理的选择默认.它避免了必须实例化的困境不可预见的类.在第二种情况下,具体的创建者使用工厂方法主要是为了灵活性.遵守规则表示在单独的操作中创建对象,以便子类可以覆盖它们的创建方式."该规则确保子类的设计者可以更改其父对象的类类在必要时实例化.
  2. 参数化工厂方法.模式的另一种变体使工厂方法可以创建多种产品.工厂方法采用一个参数来标识要创建的对象的类型.工厂方法创建的所有对象将共享该产品界面.在文档示例中,应用程序可能支持不同种类的文件.您额外传递了CreateDocument参数以指定要创建的文档的类型.

设计模式(设计模式:可重用的面向对象软件的元素)

在哪种情况下,我应该使用一种方法代替另一种方法.当我更喜欢一种方法而不是另一种方法时,优点和缺点是什么?

谢谢.

解决方案

阅读本书的荣誉.大多数人尝试#2相信 是工厂方法模式,而实际上#1声称描述了两个 major 品种.

因此,实际上我们处理的是引号文字中模式的三个略有不同的版本,尽管其中只有两个被编号.这些版本之间的差异是基于 Creator 拥有多少有关其所需 Product 实现的信息.

  1. 具有抽象工厂方法的 Creator 不了解 Product 实现,并将所有内容留给 ConcreteCreator .
  2. 具有默认工厂方法的 Creator 在大多数情况下(但并非总是)知道所需的 Product 实现.因此它允许 ConcreteCreator 覆盖默认值.
  3. 具有参数化工厂方法的 Creator 具有一个 Product 实现的菜单,并可以决定询问哪个ConcreteCreator .

因此,在每个连续的版本中, Creator 逐渐具有有关 Product 实现的更多信息,以及有关如何选择实现的更多逻辑.

在工厂方法模式中, Creator 将创建对象的责任委托给其子类,因为它"无法预期必须创建的对象的类."(第108页),基于不同的品种,我们可以看到当 Creator 可以预期有关要创建的对象类别的一些信息时,模式会如何略有变化.

选择的版本取决于您在编译时对 Product 实现的了解程度.

GoF book states that there are two ways to implement Factory Method:

Consider the following issues when applying the Factory Method pattern:

  1. Two major varieties. The two main variations of the Factory Method pattern are the case when the Creator class is an abstract class and does not provide an implementation for the factory method it declares, and the case when the Creator is a concrete class and provides a default implementation for the factory method. It’s also possible to have an abstract class that defines a default implementation, but this is less common. The first case requires subclasses to define an implementation, because there’s no reasonable default. It gets around the dilemma of having to instantiate unforeseeable classes. In the second case, the concrete Creator uses the factory method primarily for flexibility. It’s following a rule that says, "Create objects in a separate operation so that subclasses can override the way they’re created." This rule ensures that designers of subclasses can change the class of objects their parent class instantiates if necessary.
  2. Parameterized factory methods. Another variation on the pattern lets the factory method create multiple kinds of products. The factory method takes a parameter that identifies the kind of object to create. All objects the factory method creates will share the Product interface. In the Document example, Application might support different kinds of Documents. You pass CreateDocument an extra parameter to specify the kind of document to create.

Design Patterns (Design Patterns: Elements of Reusable Object-Oriented Software)

In what cases should I use one approach instead of another. What the benefits and drawbacks when I prefer one approach instead of another?

Thanks in advance.

解决方案

Kudos for reading the book. Most people attempt #2 believing that is the Factory Method pattern, when in fact #1 claims to describe the two major varieties.

So we're actually dealing with three slightly different versions of the pattern in the quoted text, though only two of them are numbered. The differences between these versions are based on how much information the Creator has about which Product implementation it wants.

  1. A Creator with an abstract Factory Method knows nothing about the Product implementation and leaves everything up to the ConcreteCreator.
  2. A Creator with a default Factory Method knows what Product implementation it wants most of the time, but not always; so it allows a ConcreteCreator to override the default.
  3. A Creator with a parameterized Factory Method has a menu of Product implementations to choose from and decides which one to ask the ConcreteCreator for.

So in each consecutive version, the Creator has progressively more information about the Product implementations and more logic concerning how the implementation is chosen.

In the Factory Method pattern, a Creator delegates responsibility for creating objects to its child classes because it, "can't anticipate the class of objects it must create." (page 108) Based on the different varieties, we can see how the pattern changes slightly when a Creator can anticipate some information about the class of objects to create.

The version you choose depends on how much you know about the Product implementations at compile time.

这篇关于工厂方法实现之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆