Home  /  Questions  /  Question

50   50
Mar 29, 2010

Difference between Factory Method and Abstract Factory


I am not understanding the difference between Factory Method and Abstract Factory.
Can anyone please elaborate more on Abstract Factory?

Sandeep Bhujabal
Good evening, Sandeep. The first difference is that Abstract Factory is an abstract CLASS, which is used as a template for several classes which build similar objects, whereas Factory Method is an abstract METHOD of a larger abstract class or perhaps of an interface, --- John Everett  Apr 02, 2010
The Abstract Factory would be used as the base class for classes which are used only to construct or instantiate many similar classes. The Factory Method would provide a common method for various classes which may implement that method differently, and may be very different in other ways that make using a common base class not a good way to implement classes whose only similarity is in this method which returns objects of a particular type. --- John Everett  Apr 02, 2010
For instance, ships and airplanes both have locations and movement. They may be similar enough to use a common Abstract Factory, implementing several methods such as movement, acceleration, and fuel consumption. LOTS of similarity for 2 different classes. However, ships and islands are different enough that the Factory Method may be all that they share, returning an object that defines the position on the map and perhaps a graphic to draw it on a display context. --- John Everett  Apr 02, 2010
It's a matter of degree of similarity - the more similar the classes, the more applicable the Abstract Factory. If they are less similar, Factory Method works better. --- John Everett  Apr 02, 2010

85   96.4
Apr 14, 2010
Practically speaking; both are creational pattern. You can use either of them for creating the object. I would say Abstract Factory is gaining the points when you wants to create the multiple object from the same family.

Kaushal Patel