aspose file tools*
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
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
Author

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
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
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
author
Sheriff

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
 
 
subject: Abstract Factory Pattern and Factory Method Pattern