samedi 23 septembre 2017

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: Factory Method Vs Abstract Factory Abstract Factory vs Factory Method (scope) Abstract Factory, Factory Method, Builder Factory, Abstract Factory 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://ift.tt/1j9o9jW 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??

Aucun commentaire:

Enregistrer un commentaire