Perhaps we should agree sth and put it in FAQ, cause it seems like one of the most common questions...
So first of all I think that when we have only one of these patterns as possible answer for the question we should always check this answer concerning Abstract Factory or Factory Method.
It's hared when we have to choose between Factory Method or Abstract Factory. I've read that some of You call this issue a "religious question", thus we can't get answer, but as this forum is aimed for passing Sun Microsystems exam some of You have any info how it looks like according to Sun.
Thanks in advance for Your discussion and may the best Factory win.
[ October 29, 2006: Message edited by: Piotr Uryga ]
The Abstract Factory according to the GOF book, is used to
Provide an interface for creating FAMILIES of related or dependent objects without specifying their concrete classes.
Where as the Factory Method
Define an interface for creating AN object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
So the question I ask you is this - when you call create on a home inteface are you creating a family of related objects or are you creating one object??
Think about it this way, are you creating a concrete implementation of an interface and then to get a concrete object that does something specific do you need to call createConcreteObject which is responsible for creating a concrete object that matches to the environment you are running on?? The GOF book UML diagram shows the interface which has the CreateScrollBar and CreateWindow methods - these methods create the ScrollBar and Window that is appropriate for the environment. To me this does not match what is happening at all.
What happens is that you call create on the Home Interface and that creates ONE concrete implementation (EJBObject) regardless of what platform or server or whatever, you will not get one object created if you are running on windows and a different one if you are running on Solaris.
The Factory Method creates ONE concrete subclass, it does not provide a concrete subclass that just offers a way to create the family of objects appropriate for the platform, by forcing the developer to call createObjectX and createObjectY.
So for my money the pattern is quite obviously the Factory Method.
The main difference between the Factory method pattern and the Abstract Factory, is that the Abstract Factory is a factory FOR FACTORIES. If you need to create very distint factories for an specific problem, Abstract Factory can help you.
The Factory method pattern instead, is a factory for specific objects. For example:
Every J2EE application uses the concept of DAOFactory to define a central point where DAO objects are created. An generic DAOFactory could be like this:
The dao factory responsability is to create specific objects for a given problem. An example of usage this pattern could be:
Instead, the Abstract Factory pattern inttends to create a familie of related objects, which means that it is going to create a very Factory Methods (Yes, you can see an AF as a factory to Factory Methods). For example: