Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract Factory and Factory Method

 
Aruna Raghavan
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am posting this again (post was lost because of disk crash)
http://c2.com/cgi/wiki?AbstractFactoryVsFactoryMethod
 
Nikhil Vasaikar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example that uses an Abstract Factory and a Factory together.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic