在以下示例中使用抽象工厂而不是工厂方法有什么好处? [英] What are the real benefits of using the Abstract Factory in the following example, instead of the factory method?

查看:154
本文介绍了在以下示例中使用抽象工厂而不是工厂方法有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在写这个问题之前,我读了以下参考文献:

Before writing the question I read the following references:


  1. 工厂方法与抽象工厂

  2. 抽象工厂与工厂方法(范围)

  3. 抽象工厂,工厂方法,构建器

  4. 工厂,抽象工厂和工厂方法

  5. 抽象工厂模式和工厂方法之间的差异

  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

我看到像我这样的很多人很难抓住具体的难度抽象工厂与工厂模式之间的差异。
我不熟悉设计模式,我遇到了这个例子 http://www.oracle.com/technetwork/java/dataaccessobject-138824.html 我正在努力深化这个话题。

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比较,我看到3 DTO我们有:

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

1)抽象工厂


  • 1个抽象类(有3个抽象方法和3个switch-cases);

  • 3个持久化类型的工厂类(每个有3个获取DTO DAO的方法)

  • 3个接口和9个DAO。

2)工厂方法:


  • 3个工厂类,每个接口一个(每个都有3个switch-case);

  • 可能我可以创建3个超类来扩展DAO类不复制代码,例如连接数据库的代码;

  • 3个接口和9个DAO。

来自代码数量的观点我没有看到任何实质性的差异。
如果您需要添加新的持久性支持或新的界面/ DTO,差异很小(和互补)。

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)抽象工厂:

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)工厂方法:

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);

使用DAOFactory有关持久性类型是否有优势,并返回与该支持相关的所有DAO为每个DTO使用多个DAOFactory来获取所用持久性类型的DAO?

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??

推荐答案

以前answear的一个注释您可以在Efecrive Java第二版中阅读工厂方法。
但是对于图案之间的现实世界的想象差异,请参阅:
例如

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

工厂

想象一下,你正在建造一所房子,然后你会接近木匠的窗户。你提出要求,他将构建一个窗口。在这种情况下,木匠是一个窗户工厂。您的规格是工厂的输入,窗口是工厂的输出。

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.

抽象工厂

现在,考虑窗口的相同示例。你可以去木匠,或者你可以去窗口店或PVC商店。他们都是窗户工厂。根据具体情况,您可以决定需要接近哪种工厂。

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 .

这篇关于在以下示例中使用抽象工厂而不是工厂方法有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆