[英]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解读:
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:谁能给我解释一下:
Question #1.问题 #1。
Question #2.问题2。
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
定义了方法createTruck
、 createPassengerCar
、 createMotorcyle
。 I create classes named ToyotaFactory
, FordFactory
and HondaFactory
that all implement the interface.我创建了名为
ToyotaFactory
、 FordFactory
和HondaFactory
的类,它们都实现了该接口。
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
接口,所以我不必更改任何旧代码或重新编译任何东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.