File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract and abstract factory

 
shalu sharma
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

can someone explain me the diff between abstract factory and factory design pattern ? I am quite confused in the difference between the two ? some examples would be even better.

Thanks
Shalu
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract Factory is a Strategy for creating objects.

There is no common definition for what a Factory is. The most sensible definition I know of is a class with non-polymorphic creation methods.

And then there is the GoF pattern Factory Method, which is the Template Method version for creating objects.
 
shalu sharma
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:

There is no common definition for what a Factory is. The most sensible definition I know of is a class with non-polymorphic creation methods.



Ilja,

can u please elaborate a bit more on this "non-polymorphic creation methods". any examples or java code would help me understand better.


Thanks
Shalu
 
B.Sathish
Ranch Hand
Posts: 372
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the factory method design pattern, you usually have an abstract superclass with an abstract factory method. Then you will have concrete subclasses which implement the factory method inside which they create a concrete product and return it to the caller. Because FM uses inheritance, polymorphism comes into the picture. You will have a reference variable of the superclass type which will refer to an instance of one of the concrete sub-classes at runtime. When you call the create method through this reference variable, polymorphism kicks in and the method of the concrete subclass which is being referred to at run-time will be invoked.

But in the simple factory (this is not actually a design pattern, more of a programming idiom), you have a factory class with a factory method that creates a product and returns it. So you can consider it as a one-shot deal because you cannot vary the product you are creating as you can with the factory method pattern by creating different concrete subclasses. Also, you will not use inheritance, you'll use composition to compose the factory object and call the create method on the composed object. That's why I guess Ilja called it a non-polymorphic method.

Abstract Factory also uses composition. It is used for creating families of related or dependent objects as against factory method which is used to create a single object. You can refer to HFDP or the patterns catalog for more details
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The difference to Abstract Factory is that there always will only be one implementation, so the method doesn't need to be overridden (polymorphic) and can even be made static.
[ March 24, 2006: Message edited by: Ilja Preuss ]
 
Peer Reynders
Bartender
Posts: 2939
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see also Abstract Factory vs Factory pattern and the links quoted therein.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic