• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Abstract Factory Pattern and Factory Method Pattern

 
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone advise what is the difference between "Abstract Factory Pattern" and "Factory Method Pattern"?

I know the former is for class creation and the latter is for object creation (is that it)?

Please correct me if I'm wrong: Abstract Factory Pattern is to be used when you have a lot of classes from the same family and you need to instantiate one of them
(key: families of products)



if this is the case what is the purpose of Factory Method Pattern?
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only problem is that your factory isn't abstract. If your DogFactory was abstract you could create A SchnauzerFactory or PoodleFactory which are concrete factories. All of them would produces "Dogs" but one would produce "small"->"Miniature Schnauzer", "regular"->"Standard Schauzer", "large"->"Giant Schauzer" while the other would produce "small" or "regular"->"Standard Poodle", "large"->"Giant Poodle" - and something would hand you the right concrete factory for your circumstances but you would only know that you had a concrete subclass of DogFactory.

In the simplest sense getDog(criteria:String) og is a Factory Method because it doesn't actually return a dog object (because Dog should be an abstract class or interface) but it returns a concrete subclass. It is a Factory Method in the GOF sense because the method is part of an interface or abstract class (DogFactory) but it's implementation is deferred to the subclasses (SchnauzerFactory, PoodleFactory).

So an Abstract Factory definitely has Factory Methods but a class with Factory methods isn't necessarily a Factory.

You may want to look at these topics:
Builder Pattern Question
abstract and abstract factory
Abstract Factory vs Factory pattern
difference between factory pattern , abstract factory pattern and command pattern
[ November 20, 2006: Message edited by: Peer Reynders ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we have a base class with an abstract getDog() method then getDog is a "factory method." Some derived class will be required to implement the getDog method and return something compatible with Dog.

An abstract factory just has a bunch of abstract getThis, getThat and getTheOther methods for a family of related objects. It usually has nothing else unrelated to getting things. Some derived class will be required to implement the methods and return compatible objects again.

I actually just answered this as a test so Ilja can tell me if I finally got it right.

What I use most often is a creator thingy that looks a lot like your example. I call them "factories" from pre-GoF habit but it's not true to either pattern. I like to get the mapping of key to class (small -> poodle) from configuration so I never have to touch the code again.
[ November 20, 2006: Message edited by: Stan James ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stan James:

I actually just answered this as a test so Ilja can tell me if I finally got it right.



Well, unfortunately I can't tell you whether you are right, but I *can* tell you that that's finally my understanding, too...

I still like to think of Factory Method as a specialization of Template Method, whereas Abstract Factory is more like a special form of the Strategy pattern.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic