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 and Factory Method 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 and Factory Method" Watch "Abstract Factory and Factory Method" New topic

Abstract Factory and Factory Method

Aruna Raghavan
Ranch Hand

Joined: May 14, 2002
Posts: 194
I am not new to patterns, I have used them over the last few years quite a bit. However, I am not sure what is the real difference between the Abstract Factory and Factory method is. It sounds like Abstract Factory could have a factory method. The Concrete Factory can implement the factory method. I guess when the Abstract Factory has a factory method,it may also have additional functionality, so it may not be just an Abstract Factory. Am I understanding this correctly?

Aruna A. Raghavan<br />SCJP, SCJD, SCWCD
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 862
The factory method is defined in a base class or interface and the subclass returns an instance of the Object. The abstract factory returns a FACTORY that can return a FAMILY of objects. Often patterns get mixed together and it becomes difficult to distinguish between them. I find this particularly true of the FactoryPatterns as often one factory may have several of the patterns in it.
Example of Factory Method: Note only a Button is returned (i.e not a Factory)

Example of an Abstract Factory: Note a only a Button is returned. Note that GUIFactory is returned with a Factory method! Note the other methods like createButton() are not factory method in this case as they are not being created by the Button class themselves.

steve - - a fast, free performance tuning API with lots of examples of design patterns - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 862
Let me try this again. This time trying to remove stupid mistakes.
Example of an Abstract Factory: Note a whole Factory is returned via createInstance(). In the factory method design pattern an Object is returned (a button in the example above), not a Factory.
It gets a little confusing though in that GUIFactory is returned via createInstance() which is a Factory method! Note createButton() is not a factory method as the returned Button is not being created by the Button subclasses, but by the Factory.

steve -
Ranch Hand

Joined: Dec 05, 2004
Posts: 48

Your interpretation sounds wrong.
The difference is sutble. Factory method is just responsible to create an object. Just that object.

But Abs.Factory defines an interface to create families of related or dependent objects. So Abs.Fact defines several Factory methods where as the Factory method pattern just limits itself to a specifc kind of object.

Correct me If I'm worng.

Thanks,<br />Bhanu<br />SCEA Step-I<br />SCBCD for J2EE 1.3<br />SCJP1.4
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

I am posting this again (post was lost because of disk crash)

Nikhil Vasaikar
Ranch Hand

Joined: Aug 18, 2004
Posts: 56
Here is an example that uses an Abstract Factory and a Factory together.
I agree. Here's the link:
subject: Abstract Factory and Factory Method
It's not a secret anymore!