在 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 本身:
导致误解的原因是我们如何应用工厂方法来生成数量的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/