This page descibes one important difference between Factory Method
and Abstract Factory
:
http://architects.dzone.com/articles/factory-method-vs-abstract
The difference, according to this page, is that in Factory Method
pattern the Creator
(i.e. the entity creating new objects) and the Client
(i.e. the entity using the Creator
) are the same class. More precisely, this pattern only defines a method, so the rest of the class is the Client
. In Abstract Factory
however, the Creator
and the Client
are separate classes. The Creator
's only purpose is to create objects so only a separate class can be the Client
.
Is this distinction correct? If so, why can't the Creator
method in the Factory Method
be put in a separate class? Would it create any problems? Similarly, why can't the Creator
class in the Abstract Factory
be the same class as the Client
? Would this create any problems?
If the Abstract Factory pattern is used correctly the Client only knows about the abstract factory base class and doesn't know about the concrete implementations. So the Client cannot also be the Creator as that would couple the client and creation code together which is exactly what the Abstract Factory pattern is designed to prevent.
The way the Factory Method pattern is usually described, there isn't really a class playing the role of the Creator. The only "creation" code is in the factory method itself. If the class containing a factory method simply passes the created object on to a Client and does play the role of "Creator" then what you have is the Abstract Factory pattern (albeit a special case with only one family of products). You'll notice that the Abstract Factory pattern is usually implemented using a Factory Method.