简体   繁体   English

工厂方法:Mark Grand 与 GoF 解释的“Java 中的模式”

[英]Factory Method: “Patterns in Java” by Mark Grand vs GoF interpretation

I'm learning Java design patterns by "Patterns in Java", volume 1 by Mark Grand (Factory Method specifically).我正在学习“Java 中的模式”的 Java 设计模式,Mark Grand 的第 1 卷(特别是工厂方法)。 My point is to highlight difference between closest patterns for myself.我的观点是为我自己强调最接近的模式之间的差异。 There are good answers that clarify the difference between Factory Method and Abstract Factory ( Design Patterns: Factory vs Factory method vs Abstract Factory , What is the basic difference between the Factory and Abstract Factory Design Patterns? ).有很好的答案可以阐明工厂方法和抽象工厂之间的区别( 设计模式:工厂与工厂方法与抽象工厂工厂与抽象工厂设计模式之间的基本区别是什么? )。 But I noticed that most of authors mean some other interpretation of Factory Method compared to one I have read in "Patterns in Java".但我注意到,与我在“Java 中的模式”中读到的相比,大多数作者的意思是对工厂方法的其他解释。 The interpretation from the answers is closer to Factory Method from GoF book.答案的解释更接近 GoF 书中的工厂方法。

GoF interpretation: GoF解读:

GoF工厂方法解释

Grand's interpretation:格朗的解释: Mark Grand 的工厂方法解释

To be specific I will describe what in my opinion is a key difference between Grand's and GoF interpretations.具体来说,我将描述我认为 Grand 和 GoF 解释之间的主要区别。 The source of polymorphism in GoF interpretation is inheritance: different implementations of Creator create different types of Product s. GoF解释中多态的来源是inheritance: Creator的不同实现创建不同类型的Product The source of polymorphism in Mark Grand interpretations apparently is "data-driven class determination" (example from book): Mark Grand 解释中的多态性来源显然是“数据驱动的 class 确定”(书中的示例):

Image createImage(String ext){
    if (ext.equals("gif"))
        return new GIFImage();
    if (ext.equals("jpeg"))
        return new JPEGImage();
    ...
}

CreationRequester delegates object creation to other object that encapsulates "data-driven class determination" logic to pick a right type of ProductIF by discriminator argument. CreationRequester将 object 创建委托给其他 object 封装“数据驱动的 class 确定”逻辑,以通过discriminator器参数选择正确类型的ProductIF

Can any one please explain me:谁能给我解释一下:

  1. Is the Grand's Factory Method interpretation equivalent to Factory Method by GoF? Grand 的工厂方法解释是否等同于 GoF 的工厂方法? Is it actually Factory Method at all?它实际上是工厂方法吗? May be it is some kind of generalization or special case of GoF Factory Method?可能是 GoF 工厂方法的某种概括或特殊情况? I can't see direct mapping between these interpretations taking into account UML class diagrams.考虑到 UML class 图,我看不到这些解释之间的直接映射。
  2. Let's consider GoF interpretation now.现在让我们考虑 GoF 解释。 Can one define a factory method in a separate interface (not in abstract class), like Mark Grand describes?可以像 Mark Grand 描述的那样在单独的接口(而不是抽象类)中定义工厂方法吗? Will it still be Factory Method?它仍然是工厂方法吗? Can one consider Factory Method in GoF interpretation as a special case of Template Method that returns some product?可以将 GoF 解释中的工厂方法视为返回某些产品的模板方法的特例吗?
  3. If we consider Factory Method as Grand describes, then it becomes obvious that Factory Method is simply a special case of Abstract Factory that produces a single product.如果我们按照 Grand 所描述的那样考虑工厂方法,那么很明显,工厂方法只是生产单一产品的抽象工厂的一个特例。 Am I right?我对吗?
  4. If one just encapsulates a "data-driven class determination" logic into a separate class, can this structure be called "Factory Method"?如果只是将一个“数据驱动的class判定”逻辑封装成单独的class,这个结构能不能叫“工厂方法”?

Question #1.问题 #1。

  1. No.不。
  2. No.不。
  3. Sometimes UML obfuscates a thing more than it clarifies a thing.有时,UML 混淆事物多于澄清事物。

Question #2.问题2。

  1. I only see one factory interface in the Grand picture.我在 Grand 图片中只看到一个工厂界面。 I don't see a separate interface.我没有看到单独的界面。
  2. ? ?
  3. Yes.是的。 And no.和不。

Question #3.问题 #3。 No.不。

Question #4.问题 #4。 No.不。


I'm not as up on my GoF patterns as I used to be, but I'll take a shot at this.我不像以前那样对我的 GoF 模式了如指掌,但我会试一试。 I think you are correct about the difference.我认为您对差异的看法是正确的。 The GoF pattern uses inheritance for polymorphism and Grand's example uses conditional logic for polymorphism. GoF 模式使用 inheritance 进行多态性,Grand 的示例使用条件逻辑进行多态性。

The operational difference is that to add a new type in Grand's example, I would modify the method createImage :操作上的区别在于,要在 Grand 的示例中添加新类型,我将修改方法createImage

if (ext.equals("png"))
       return new PngImage()

In the GoF version no existing code is modified.在 GoF 版本中,没有修改现有代码。 I create a new subclass called PngCreator that implements the Creator interface.我创建了一个名为PngCreator的新子类,它实现了Creator接口。 This is a good example of the open/closed principle-- the GoF factory is closed to modification, but the pattern open to extension (by adding a new implementation of the factory interface).这是开放/封闭原则的一个很好的例子——GoF 工厂对修改是封闭的,但模式对扩展是开放的(通过添加工厂接口的新实现)。

Factory vs. Template工厂与模板

Factory Method is always implemented using the Template Method pattern ( Design Patterns Smalltalk Companion by Alpert, Brown, and Woolf)工厂方法始终使用模板方法模式(Alpert、Brown 和 Woolf 的Design Patterns Smalltalk Companion )实现

Yes, factory method pattern is a kind of template method pattern.是的,工厂方法模式是一种模板方法模式。 However, a (GoF) pattern is more than its structure or diagram.但是,(GoF)模式不仅仅是其结构或图表。 Every pattern in the GoF book includes intent, motivation, applicability, participants , and so on. GoF 书中的每个模式都包括意图、动机、适用性、参与者等。 Intent and motivation are part of what makes Factory and Template different.意图和动机是使 Factory 和 Template 不同的部分原因。 For starters, one is a creation pattern and the other is a behavioral pattern.对于初学者来说,一种是创造模式,另一种是行为模式。

The intent of the Factory Method pattern is to let subclasses of the Factory decide what class to instantiate.工厂方法模式的目的是让工厂的子类决定要实例化什么 class。 In Grand's version, I see only one concrete factory, and it uses conditional logic.在 Grand 的版本中,我只看到一个具体的工厂,它使用条件逻辑。 IMHO, that is different than using multiple concrete subclasses.恕我直言,这与使用多个具体子类不同。 Then again, Grand's version does decouple creation requesters from class names.再说一次,Grand 的版本确实将创建请求者与 class 名称分离。 If you modify the factory to return an instance of Jpeg2000 instead of Jpeg , the compiler won't complain.如果您修改工厂以返回Jpeg2000而不是Jpeg的实例,编译器不会抱怨。 CreationRequester depends only on the interface Image . CreationRequester 仅依赖于接口Image This is important if I am using someone else's library and I can't modify it.如果我正在使用其他人的库并且我无法修改它,这一点很重要。 If avoiding re-compilation of the creation requester is the motivation, then Grand's version works fine.如果避免重新编译创建请求者是动机,那么 Grand 的版本可以正常工作。

Is Grand's Factory Method pattern a special case of Abstract Factory pattern? Grand 的工厂方法模式是抽象工厂模式的特例吗?

I don't think it is.我不认为它是。 I'll borrow the example from my (ancient) copy of Design Patterns Smalltalk Companion.我将从我的(古老的)Design Patterns Smalltalk Companion 副本中借用这个示例。 Let's say my application uses vehicles.假设我的应用程序使用车辆。 I might have a VehicleFactory interface with methods like 'createTruck', 'createPassengerCar', 'createMotorcyle`.我可能有一个VehicleFactory接口,其中包含“createTruck”、“createPassengerCar”、“createMotorcyle”等方法。

What the vehicle manufacturer was important to my applicaiton?汽车制造商对我的申请很重要? Enter the Abstract Factory pattern!进入抽象工厂模式! VehicleFactory defines the methods createTruck , createPassengerCar , createMotorcyle . VehicleFactory定义了方法createTruckcreatePassengerCarcreateMotorcyle I create classes named ToyotaFactory , FordFactory and HondaFactory that all implement the interface.我创建了名为ToyotaFactoryFordFactoryHondaFactory的类,它们都实现了该接口。

Here is what each concrete factory return:以下是每个具体工厂返回的内容:

ToyotaFactory.createPassengerCar -> Camry ToyotaFactory.createPassengerCar -> 凯美瑞

HondaFactory.createPassengerCar -> Accord HondaFactory.createPassengerCar -> 雅阁

FordFactory.createPassengerCar -> Taurus FordFactory.createPassengerCar -> Taurus


ToyotaFactory.createMotorcycle -> FM6 ToyotaFactory.createMotorcycle -> FM6

HondaFactory.createMotorcycle -> GoldWing HondaFactory.createMotorcycle -> 金翼

FordFactory.createPassengerCar -> return null or throw exception, or... FordFactory.createPassengerCar -> 返回 null 或抛出异常,或...

On the next day, I add a FiatFactory .第二天,我添加了一个FiatFactory Because the client classes depend on the VehicleFactory interface, I don't have to change any old code or recompile anything.因为客户端类依赖于VehicleFactory接口,所以我不必更改任何旧代码或重新编译任何东西。

  1. The GoF Factory Method Pattern defines three varieties: What's the difference between Factory Method implementations? GoF 工厂方法模式定义了三种类型:工厂方法实现之间有什么区别? Grand's discriminator is indicative of what the GoF refer to as a parameterized factory method . Grand 的鉴别器表示 GoF 所称的参数化工厂方法
  2. No: Design Patterns: Abstract Factory vs Factory Method否:设计模式:抽象工厂与工厂方法
  3. No: without inheritance and without a template method, it would be what Head First Design Patterns calls a Simple Factory.不:没有 inheritance 和模板方法,这就是 Head First Design Patterns 所说的简单工厂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM