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?
In my opinion, the slight difference between the two patterns resides in the applicability, and so, as already said, in the Intent.
Let's recap the definitions (both from Wikipedia).
Abstract Factory
Factory Method
Both patterns allow to decouple the user objects from creating needed instances (run-time decoupling), and this is the common aspect. Both patterns allow to create a hierarchy of factories according to any specific needs, and this is another common aspect.
Abstract Factory allows to create several different type of instances in one sub-class, and to particularize the creations behavior in its different sub-classes; normally, Factory method declares the creation of only one type of object that can be particularized according to the sub-classing mechanism. That's the difference.
By summarizing. Let's say that Product defines the super-class of the creating objects, and that ProductA and ProductB are two different sub-classes. Therefore, the Abstract Factory method will have two methods, createProductA() and createProductB() which will be particularized (in terms of creation steps) in its specific sub-classes: the factory sub-classes particularize the creation steps for the two defined classes of objects under creation.
According to the above example, the Factory Method will be implemented differently, abstracting the creation of ProductA and ProductB in as many factories (one method per Factory), and the further specialization of the creation steps will be delegated to the hierarchy as it is built.
As far as I understand the meaning o Abstract factory and Factory method definitions the first one is implemented in static context and provides object based on input parameters.
Second one uses already created object (the family) which implements factory method interface. The factory method then creates specific instance related to the original object no matter which one it is.
So this usually leads to using both patterns together where in first step you create some general object which describes family of related objects. It is called by static method getInstance("my family name") method. Implementation of such getInstance method decides which family object will be created.
Then I call createProduct() method on newly created family object and depending family object the new product will be returned.
It seems that these patterns cooperate to each one.
In another words Abstract Factory is focused on "WHAT" will be created and Factory method "HOW" it will be created.
Although, it's been many years since people from StackOverflow questioned about this issue similarly in other posts (oldest goes to 2009), I still could not find the answer I wanted.
So I did a few hours of researching through web, reviewing the examples, and came to this conclusion, the major differences of Abstract Factory from Factory Method are
The counter examples would be
Therefore, when a final object group should have the same style without an exception of a object and you want to hide this "keeping the same style" detail, then we should use Abstract Factory.
Factory method pattern is a creational design pattern which deals with creating objects without showing the exact class of object that is being created. This design pattern basically allows a class to defers the instantiation to sub-classes.
The Abstract Factory pattern serves encapsulation to a group of individual factories without exposing the concrete classes. In this model, a generic interface of an abstract factory class is used to create the required concrete object separating the details of implementation of objects from their usage and composition. This design pattern is widely used in GUI applications where similar kind of GUI components needs to be created.
while searching on google i came up following blog which explained both design pattern brilliantly. have a look on these
http://simpletechtalks.com/factory-design-pattern/
http://simpletechtalks.com/abstract-factory-design-pattern/
If I created an abstracted (referenced via an interface or abstract base class) Factory Class that creates objects that has only one method to create objects, then it would be a Factory Method.
If the abstracted Factory had more than 1 method to create objects, then it would be an Abstract Factory.
Let's say I make a Manager that will handle the needs of action methods for an MVC controller. If it had one method, say to create the engine objects that will be used to create view models, then it would be an factory method pattern. On the other hand if it had two methods: one to create view model engines, and another to create action model engines (or whatever you want to call the model that the action method contains consumers), then it would be an abstract factory.