• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

abstract and abstract factory

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please see also Abstract Factory vs Factory pattern and the links quoted therein.
 
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic