File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Abstract Factory Pattern    and    Factory Method Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Abstract Factory Pattern    and    Factory Method Pattern" Watch "Abstract Factory Pattern    and    Factory Method Pattern" New topic

Abstract Factory Pattern and Factory Method Pattern

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
can anyone advise what is the difference between "Abstract Factory Pattern" and "Factory Method Pattern"?

I know the former is for class creation and the latter is for object creation (is that it)?

Please correct me if I'm wrong: Abstract Factory Pattern is to be used when you have a lot of classes from the same family and you need to instantiate one of them
(key: families of products)

if this is the case what is the purpose of Factory Method Pattern?
Peer Reynders

Joined: Aug 19, 2005
Posts: 2921
The only problem is that your factory isn't abstract. If your DogFactory was abstract you could create A SchnauzerFactory or PoodleFactory which are concrete factories. All of them would produces "Dogs" but one would produce "small"->"Miniature Schnauzer", "regular"->"Standard Schauzer", "large"->"Giant Schauzer" while the other would produce "small" or "regular"->"Standard Poodle", "large"->"Giant Poodle" - and something would hand you the right concrete factory for your circumstances but you would only know that you had a concrete subclass of DogFactory.

In the simplest sense getDog(criteria:String) og is a Factory Method because it doesn't actually return a dog object (because Dog should be an abstract class or interface) but it returns a concrete subclass. It is a Factory Method in the GOF sense because the method is part of an interface or abstract class (DogFactory) but it's implementation is deferred to the subclasses (SchnauzerFactory, PoodleFactory).

So an Abstract Factory definitely has Factory Methods but a class with Factory methods isn't necessarily a Factory.

You may want to look at these topics:
Builder Pattern Question
abstract and abstract factory
Abstract Factory vs Factory pattern
difference between factory pattern , abstract factory pattern and command pattern
[ November 20, 2006: Message edited by: Peer Reynders ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If we have a base class with an abstract getDog() method then getDog is a "factory method." Some derived class will be required to implement the getDog method and return something compatible with Dog.

An abstract factory just has a bunch of abstract getThis, getThat and getTheOther methods for a family of related objects. It usually has nothing else unrelated to getting things. Some derived class will be required to implement the methods and return compatible objects again.

I actually just answered this as a test so Ilja can tell me if I finally got it right.

What I use most often is a creator thingy that looks a lot like your example. I call them "factories" from pre-GoF habit but it's not true to either pattern. I like to get the mapping of key to class (small -> poodle) from configuration so I never have to touch the code again.
[ November 20, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Stan James:

I actually just answered this as a test so Ilja can tell me if I finally got it right.

Well, unfortunately I can't tell you whether you are right, but I *can* tell you that that's finally my understanding, too...

I still like to think of Factory Method as a specialization of Template Method, whereas Abstract Factory is more like a special form of the Strategy pattern.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Consider Paul's rocket mass heater.
subject: Abstract Factory Pattern and Factory Method Pattern
Similar Threads
Doubt about EJBHome(design patterns)
private constructor
Help understanding Abstract Factory from a factory Example
Factory Vs Abstract Factory Pattern