Search code examples
javadesign-patternsdaoabstract-factoryfactory-method

What are the real benefits of using the Abstract Factory in the following example, instead of the factory method?


Before writing the question I read the following references:

  1. Factory Method Vs Abstract Factory
  2. Abstract Factory vs Factory Method (scope)
  3. Abstract Factory, Factory Method, Builder
  4. Factory, Abstract Factory and Factory Method
  5. Differences between Abstract Factory Pattern and Factory Method

I see that many like me have had difficulty "grasping" the concrete differences between Abstract Factory and Factory Pattern. I'm not familiar with the design patterns, I came across this example http://www.oracle.com/technetwork/java/dataaccessobject-138824.html and I'm trying to deepen the topic.

By comparing, I see that for 3 DTO we have:

1) Abstract Factory

  • 1 abstract class (with 3 abstract methods and 3 switch-cases);
  • 3 factory classes for persistence type (each with 3 methods for obtaining DTO DAOs)
  • 3 interfaces and 9 DAOs.

2) Factory Method:

  • 3 factory classes, one for each interface (each with 3 switch-case);
  • Possibly I can create 3 superclasses from which to extend the DAO classes to not duplicate the code, such as that for connecting to the database;
  • 3 interfaces and 9 DAOs.

From the point of view of the quantity of code I do not see any substantial differences. In cases where you need to add a new persistence support or a new interface / DTO, the differences are minimal (and complementary).

From the client's point of view:

1) Abstract Factory:

public static final int PERSISTENCE_TYPE = DAOFactory.ORACLE;

DAOFactory daoFactory = DAOFactory.getDAOFactory(PERSISTENCE_TYPE);

CustomerDAO cDAO = daoFactory.getCustomerDAO();
AccountDAO aDAO = daoFactory.getAccountDAO();
OrderDAO oDAO = daoFactory.getOrderDAO();

2) Factory Method:

public static final int PERSISTENCE_TYPE = DAOFactory.ORACLE;

CustomerDAO cDAO = CustomerDAOFactory.getCustomerDAO(PERSISTENCE_TYPE);
AccountDAO aDAO = AccountDAOFactory.getAccountDAO(PERSISTENCE_TYPE);
OrderDAO oDAO = OrderDAOFactory.getOrderDAO(PERSISTENCE_TYPE);

Is there an advantage in using a DAOFactory regarding the persistence type and returns all DAOs related to that support instead of using multiple DAOFactory for each DTO to get the DAO for the type of persistence used?

For now I see only aesthetic-conceptual difference in using the Abstract Factory, is there also a utility benefit that I can not grasp for my ignorance about software design??


Solution

  • One note to previous answear You can read factory method in Efecrive Java 2nd edition . But to imagin difference in real world between patterns please see : For example

    Factory

    Imagine you are constructing a house and you approach a carpenter for a window. You give your requirements, and he will construct a window . In this case, the carpenter is a factory of windows. Your specifications are inputs for the factory, and the window is the output from the factory.

    Abstract Factory

    Now, consider the same example of the window. You can go to a carpenter, or you can go to a window shop or a PVC shop. All of them are window factories. Based on the situation, you decide what kind of factory you need to approach.

    So conclution - It depend on problem which you solve .