Note: Questions are at the end of the post.
I have read the other stackoverflow threads regarding Abstract Factory vs Factory Method. I understand the intent of each pattern. However, I am not clear on the definition.
Factory Method defines an interface for creating an object, but lets subclasses decide which of those to instantiate. A factory method lets classes defer instantiation to subclasses.
By contrast, an Abstract Factory provides an interface for creating families of related or dependent objects without specifying their concrete classes.
The Abstract Factory looks very similar to the Factory Method. I have drawn a few UML classes to illustrate my point.
Note:
- The diagram are from www.yuml.com so they are not perfectly oriented. But its a free service :).
- The diagrams may not be perfect. I am still learning the GoF design patterns.
Factory Method:
Abstract Factory (only 1 member):
Abstract Factory (more members):
Questions:
- If the Abstract Factory has only one creator and one product, is it still the Abstract Factory pattern? (an interface for creating familes)
- Can the Factory Method concrete creator be created from an Interface or does it have to be from a class? (classes defer instantiations to subclasses)
- If the Abstract Factory can have only one creator and one product, is the only difference between the Abstract Factory and the Factory Method that the creator for the former is an Interface and the creator for the latter is a Class?