Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract Factory Pattern and Factory Method Pattern

 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • 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?
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • 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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic