1

I am just going through the wiki pages of Factory Method Pattern and Abstract Factory method pattern.

From Head First Design Patterns I got this

Factory method Pattern uses inheritance, Abstract Factory Pattern uses composition. Abstract Factory Pattern also does the same like Factory Method Pattern which keeps the Clients decoupled from concrete Types but in a different way.

After analyzing the wiki examples, I couldn't actually find the composition in the examples. Both of the Patterns uses inheritance.

Factory Method Pattern

enter image description here

Abstract Factory Pattern

enter image description here

Here are my questions.

  1. Does Factory Method Pattern always have just only one Abstract Product inheritance tree unlike Abstract Factory Method Pattern?
  2. Where could be composition utilized or programmed in a way? If it's just the Client with main method, then Factory Method Pattern also uses Composition. A code snippet example would clear the concept.
  3. Is my interpretation of these patterns as in sample sketch correct?
srk
  • 4,857
  • 12
  • 65
  • 109
  • Possible duplicate of [Differences between Abstract Factory Pattern and Factory Method](https://stackoverflow.com/questions/5739611/differences-between-abstract-factory-pattern-and-factory-method) – Fuhrmanator Jul 24 '17 at 23:33

1 Answers1

1

Yes as pointed out by "Fuhrmanator" it looks duplicate. The referred question-answer provides good explanation about these two patterns. Just in short, GOF Design pattern catalog talks about three patterns with 'Factory' term in it. All three are creational patterns. Most of the people use Static factory implementation when they use term "factory Pattern".

Static Factory is convenient way of enclosing creation of concrete classes in one method of nature

AbstractProduct getProduct(key){
    if (key == ...) return new ConcreteProduct1();
    ... so on.
} 

Factory method is a single abstract method maintained as a hook to delay the instantiation of concrete product. This is very useful when we are writing frameworks where we write business logic but we do not want to assume anything about concrete classes (to which this logic applies) which client developer will write at later date. (not repeating UML diagram of pattern here)

Abstract Factory pattern is more general case of Factory method, where we need to restrict the types of products which can be instantiated and used but the complete flavor changes (where we will not keep toggling between flavors through out application life).

e.g. An abstract ShapeFactory might provide interface for creating 3 shapes like circle, square and rectangle, While as concrete factories FilledShapeFactory and HollowShapeFactory will be bound to provide filled and hollow versions of these three shapes (concrete factories are not free to decide about new shapes or drop any shape).

This pattern can not be used when variety of shapes (products) changes as it enforces change in interface and impacts clients.

The reason for flavor change might be due to parent environment, OS or any other grossly impacting parameter.

Kedar Tokekar
  • 418
  • 3
  • 10