wood burning stoves 2.0*
The moose likes OO, Patterns, UML and Refactoring and the fly likes abstract and abstract factory 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 and abstract factory" Watch "abstract and abstract factory" New topic
Author

abstract and abstract factory

shalu sharma
Ranch Hand

Joined: Sep 09, 2002
Posts: 51
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

Joined: Jul 11, 2001
Posts: 14112
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.


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
shalu sharma
Ranch Hand

Joined: Sep 09, 2002
Posts: 51
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

Joined: Aug 18, 2005
Posts: 372
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

Joined: Jul 11, 2001
Posts: 14112


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

Joined: Aug 19, 2005
Posts: 2922
    
    5
Please see also Abstract Factory vs Factory pattern and the links quoted therein.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abstract and abstract factory
 
Similar Threads
design pattern for Strings to Objects
Difference between Abstract Factory and Builder Pattern
Q on method types: what is a factory method?
factory and abstract factory
Abstract Factory pattern