1

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

enter image description here

"Abstract Factory" has the following structure:

enter image description here

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

enter image description here

?

Tim
  • 1
  • 141
  • 372
  • 590

3 Answers3

1

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. ´

BionicCode
  • 1
  • 4
  • 28
  • 44
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.

Zazaeil
  • 3,900
  • 2
  • 14
  • 31
0

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.

Nghia Bui
  • 3,694
  • 14
  • 21
  • The first half of this answer is totally correct. The second half seems to imply that every use of composition is an instance of the Strategy Pattern. – jaco0646 Jun 07 '19 at 13:22
  • No, I didnt mean that. I just mentioned composition to compare with Factory/Template Method patterns where inheritance is used. And yes, I agree that every use of composition is not necessarily Strategy pattern. – Nghia Bui Jun 07 '19 at 13:28
  • Abstract Factory uses composition; but I would not say that it follows the Strategy Pattern. Most OO patterns use composition. – jaco0646 Jun 07 '19 at 17:38
  • 1
    Maybe the word “follow” is too strong but IMO the Strategy is the clearest example of patterns using composition. – Nghia Bui Jun 08 '19 at 00:08