java - 如何正确地将工厂方法应用到DAO工厂中?

标签 java dao factory

the documentation about DAO 说:

When the underlying storage is not subject to change from one implementation to another, this strategy can be implemented using the Factory Method pattern to produce a number of DAOs needed by the application. The class diagram for this case is shown in Figure 9.3.

图 9.3 本身:

enter image description here

导致误解的原因是我们如何应用工厂方法来生成数量DAO?据我了解,在这种情况下,DAO 可能看起来像

public interface DAOFactory{

    public DAO1 getDAO1();
    public DAO2 getDAO2();

}

但它不是工厂方法。它将是一个抽象工厂,因为我们正在生产一系列对象而不是单个对象。你不能解释一下他们所说的意思

this strategy can be implemented using the Factory Method pattern to produce a number of DAOs needed by the application. The class diagram for this case is shown in Figure 9.3.

最佳答案

你是对的。这是误导性的。 描述使用工厂方法的图像无疑是抽象工厂设计模式的一个示例。工厂只有一种实现。

这里有关于工厂方法和抽象工厂之间区别的很好的解释:Differences between Abstract Factory Pattern and Factory Method

文档中显示的图表描述了抽象工厂模式。

我可以想象以下使用DAO工厂方法

// the Service contains the business logic and uses DAOs to access data
class BaseService {

   public Object doSomething() {
       return getDao().retrieveObject("identifier");
   }

   // Factory method producing DAOs
   public IDao getDao() {
       return new BaseDao();
   }

}

// The DAO interface
interface IDao {
    Object retrieveObject(String identifier);
}


// Another Service that overwrite the type of produced DAOs 
class SmartService extends Service {

   // overwrite the factory method
   public IDao getDao() {
       return new SmartDao();
   }
}

关于java - 如何正确地将工厂方法应用到DAO工厂中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27959770/

相关文章:

java - XML DAO 示例

generics - Kotlin 通用工厂动态转换

c# - 工厂模式可以这样使用吗?

java - 哪种方法更好 String.equalIgnoreCase 或 StringUtils.equalIgnoreCase

Java DAO 工厂动态返回的对象类型

java - hibernate 随机 "Session is closed error"与 2 个数据库

javascript - Controller 测试模拟工厂

java - 我正在尝试在 Eclipse 中导出一个可运行的 jar,但我需要它包含一些运行我的程序所需的其他文件

java - Azure 构建管道,docker compose - 设置环境变量

java - 如何管理和操作极大的二进制值