dcsimg
Abstract Factory versus FactoryMethod pattern
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Monday, September 4, 2006 12:06 AM

I am trying to figure out how AbstractFactory pattern is different from Factory Method pattern. I am having some doubts. As per GOF Factory Method pattern -> Define a interface to create a object but let subclasses decide which class to instantiate. Abstract Factory pattern -> Define a interface to create family of products without specifying their concrete classes. One point of difference i got from google search is as follows: FactoryMethod pattern uses inheritance and relies on subclass to handle object instantiation. In AbstractFactory pattern, a class delegates the responsibility of object instantiation to another objec   More>>

I am trying to figure out how AbstractFactory pattern is different from Factory Method pattern.

I am having some doubts.


As per GOF


Factory Method pattern -> Define a interface to create a object but let subclasses decide
which class to instantiate.


Abstract Factory pattern -> Define a interface to create family of products without specifying their
concrete classes.


One point of difference i got from google search is as follows:


FactoryMethod pattern uses inheritance and relies on subclass to handle object instantiation.


In AbstractFactory pattern, a class delegates the responsibility of object instantiation to another object
via composition.


Please have a look at the following link:

http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html

In this article. DAO strategy is implemented using FactoryMethod pattern and AbstractFactory pattern.

This is how Abstract Factory is implemented

/ Abstract class DAO Factory

public abstract class DAOFactory {



// List of DAO types supported by the factory


public static final int CLOUDSCAPE = 1;

public static final int ORACLE = 2;

public static final int SYBASE = 3;


...


// There will be a method for each DAO that can be

// created. The concrete factories will have to

// implement these methods.

public abstract CustomerDAO getCustomerDAO();

public abstract AccountDAO getAccountDAO();

public abstract OrderDAO getOrderDAO();


...

public static DAOFactory getDAOFactory(

int whichFactory) {


switch (whichFactory) {

case CLOUDSCAPE:

return new CloudscapeDAOFactory();

case ORACLE :

return new OracleDAOFactory();

case SYBASE :

return new SybaseDAOFactory();

...

default :

return null;

}

}

}


In Client application the factory will be choosen as follows:

DAOFactory dao = DAOFactory.getDAOFactory(CLOUDSCAPE);


Doubt no 1 Here we are not using composition anywhere. So either the point of difference is invalid or the abstract factory implementation is wrong.


Doubt no 2 The only difference between AbstractFactory and FactoryMethod implementation from
this article is
that the AbstractFactory has a method to choose a factory(ie family of products).

Either this method to choose is present inside the abstract factory or it is present in some helper class
Is this the difference ??.



Another statement which i came across is that AbstractFactory uses FactoryMethod pattern. If you observe

the DAOFactory code then which factory to choose is implemented in the class itself whereas Factory pattern

speaks about defining a interface and let subclass decide which class to instantiate.
So this AbstractFactory implementation is not using FactoryMethod pattern.



Doubt no 3 FactoryMethod pattern is called class pattern and AbstractFactory is called Object pattern.
Please explain this statement.

   <<Less
About | Sitemap | Contact