Reputation: 735
As I anderstand:
The abstract factory pattern provides an interface for creating a family of objects whereas factory method provides an interface for creating one object.
If this is only difference between these patterns why they are being considered separately?
Upvotes: 1
Views: 1267
Reputation: 896
Having a look back at the definition of these two patterns tells us that
Factory Method is used to define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses."
Abstract Factory is used to provide an interface for creating families of related or dependent objects without specifying their concrete classes.
So what you said is correct. Factory Method concerns with creating (usually) one object, whereas Abstract Factory concerns with several related objects.
But that is NOT all there is. If you look at the second word that I bolded in each intent, you will see there's another difference, in terms of the mechanism each pattern works.
Factory Method uses SUBCLASSES and inheritance to delegate the instantiation to the concrete implementation. It means that to create new products, you have to inherit the "creator" class and override the factory method. This factory method, in turn, returns the needed product.
On the other hand, Abstract Factory uses OBJECT composition (i.e. the factory) to delegate. You have to make code change in the "product" class, not in the creator class. Specifically, inside the product class, you define several related products (lets say ingredients of the product), each of which can be created using the factory OBJECT (composed in the product class, and passed in runtime). The creator class in this case only creates the abstract product and passes into that product a factory that the product will be using.
To make things a bit confusing, however, the central abstract factory in Abstract Factory usually implements Factory Method. This central factory often defines a series of factory methods for all the ingredients, and delegate the creation of each ingredient to the concrete factories.
Hope it helps. Remember that many design patterns are really similar and in fact they are related, e.g. look at the Decorator pattern and Adapter pattern. Most of the times, the difference between two design patterns lies in their respective intents. A good book about Design Patterns, by the way, that I really love is Head First Design Pattern. And there is a similar question posted here.
Upvotes: 4
Reputation: 267
Abstract factory pattern means having a factory for factories (concrete implementations).
Upvotes: 0
Reputation: 597106
The factory method is fixed - you can't change it at runtime.
Abstract factory allows you to create objects with a different factory, which can be selected at runtime, depending on some criteria.
Button button = WinButtonFactory.create(); //will always be a "windows button"
Button button = buttonFactory.create();
on the 2nd like this can be WinButtonFactory extends ButtonFactory
or MacOSXButtonFactory extends ButtonFactory
. You can pass one or the other depending on the current OS.
Upvotes: 3