Does factory method pattern follow template pattern, and abstract factory pattern doesn't?

喜你入骨 提交于 2019-12-13 19:36:24

问题


In Design Pattern by Gamma et al, "Factory Method" has the following structure:

"Abstract Factory" has the following structure:

Why does Creator has a method AnOperation() to callFactoryMethod() in Factory Method pattern, while AbstractFactory doesn't have a method to call CreateProductA() and CreateProductB() in Abstract Factory pattern?

Does Creator's method AnOperation() callingFactoryMethod() follow the Template pattern, whose structure is

?


回答1:


It's simple: given "first-order factory" servers creation purpose (basically, a bunch of new statements done rightly), an "abstract factorty" - or "higher order factory" - is a special case when the created object is itself.. another factory. It's kinda similar to a list of lists.

P.S. You could also have a factory that creates a factory that creates a factory. It won't be useful in most realworld cases, though perfectly fits "higher order factory" definition.




回答2:


A factory is a class that is dedicated to create instances of defined types. This is the ONLY purpose of a factory. A factory method on the other hand can exist on any non-factory type. The factory method diagram tries to express this by adding an arbitrary method (in UML a method is called operation) to the class to show that its main purpose is not to create instances of some other types.

You are wrong when you are interpreting the diagram like AnOperation() is calling FactoryMethod(). The diagram says Creator is an abstract type that has an abstract method called FactoryMethod and a concrete (or non-abstract) method called AnOperation. ConcreteCreator derives from Creator and inherits AnOperatio() (which is not repeated in the UML class specification since it is not abstract) and the abstract FactoryMethod(). AnOperatio() and FactoryMethod() are two independent methods. ´




回答3:


Neither the Factory method nor the Abstract Factory pattern are applications of ("follow") the Template Method design pattern. Although the UML class diagram of their solution template may look similar, they actually help accomplish different goals.

The two factory patterns are concerned with allowing subclasses to customize how objects of a certain type are created. See this post for a discussion of the differences between the different factory patterns.

The Template Method design pattern is not concerned with the creation of objects, but rather in introducing variation points into an algorithm.

This being said, what all three patterns have in common is that they defer some behavior to subclasses. See this post for an alternative take on your question.




回答4:


I suppose you mean Template Method pattern.

Does factory method pattern follow template pattern?

Yes, it does. A method which is a factory method is exactly a kind of a template method. The only special thing is that a factory method creates and returns an object, while a template method can do anything.

abstract factory pattern doesn't?

No, it doesn't. The Abstract Factory pattern follows the Strategy pattern in which aggregation/composition is used instead of inheritance.



来源:https://stackoverflow.com/questions/56483736/does-factory-method-pattern-follow-template-pattern-and-abstract-factory-patter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!