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

Abstract Factory and Factory Method

Aruna Raghavan
Ranch Hand

Joined: May 14, 2002
Posts: 194
Hi,
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?
Thanks,
Aruna


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

Joined: Jun 26, 2002
Posts: 861
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 - http://www.jamonapi.com - a fast, free performance tuning API with lots of examples of design patterns


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 861
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 - http://www.jamonapi.com
Bhanu
Ranch Hand

Joined: Dec 05, 2004
Posts: 48
Steve,

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: 8919

I am posting this again (post was lost because of disk crash)
http://c2.com/cgi/wiki?AbstractFactoryVsFactoryMethod


Groovy
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: http://aspose.com/file-tools
 
subject: Abstract Factory and Factory Method