Friday, February 29, 2008 09:38 AM
I got this off JavaRanch:
A Factory pattern is one that returns an instance of one of several possible classes depending on the data provided to it.
Usually all of the classes it returns have a common parent class and common methods, but each of them performs a task differently and is optimized for different kinds of data.
There are several similar variations on the factory pattern:
- The base class is abstract and the pattern must return a complete working class.
- The base class contains default methods and is only subclassed for cases where the default methods are insufficient.
- Parameters are passed to the factory telling it which of several class types to return. In this case the classes may share the same method names but may do something quite different.
Abstract Factory Pattern:
The Abstract Factory pattern is one level of abstraction higher than the factory pattern. You can use this pattern when you want to return one of several related classes of objects, each of which can return several different objects on request. In other words, the Abstract Factory is a factory object that returns one of several factories.
Note: There was a long discussion on the thread about whether a Factory Pattern was really a "Gang of Four" design pattern. But as a devloper, I really don't care. I use the Factory Design pattern AND the AbstractFactory Design pattern.. and when I talk to other developers that know Design Patterns they know what I'm talking about when I say "I used the Factory Pattern to implement this..."
Hope this helps.