简体   繁体   English

一个接口,具有所有具有多个实现类的方法,每个方法都实现该接口的方法的子集

[英]One interface with all methods with multiple implementation class each implementing a subset of the methods from the interface

I am working on refactoring the data access layer for a project where I need to provide the business logic layer developers with a single interface for all DB interactions. 我正在为一个项目重构数据访问层,在该项目中,我需要为业务逻辑层开发人员提供用于所有数据库交互的单个接口。 Currently there is just one DAOImpl class implementing this interface but the class has bloated to 15000+ lines of code. 当前,只有一个DAOImpl类实现此接口,但是该类已经膨胀到15000多行代码。 Now I wish to move the methods from this class into multiple classes based on the type of object they handle. 现在,我希望根据它们处理的对象类型将方法从此类移至多个类。 The approach I have thought of is - 我想到的方法是-

  1. Keep the DAOInterface with all methods as is 保持DAOInterface的所有方法不变
  2. Implement a DAOImpl class which implements the DAOInterface but will not have any logic in any of the methods 实现一个DAOImpl类,该类实现DAOInterface,但在任何方法中均不包含任何逻辑
  3. Implement Object specific DAOImpl classes which extend the DAOImpl and implement the DAOInterface and provide the actual DAO implementation for all object specific methods. 实现特定于对象的DAOImpl类,该类扩展了DAOImpl并实现了DAOInterface,并为所有特定于对象的方法提供了实际的DAO实现。
  4. Change the current DAOFactory class to provide instances of object specific DAOImpl based on some identifier passed from the business logic layer. 更改当前的DAOFactory类,以基于从业务逻辑层传递的某些标识符提供特定于对象的DAOImpl的实例。

I just wanted to validate my approach in this forum to see if I am doing things the right way or is there a better solution/pattern for this problem. 我只是想在此论坛中验证我的方法,以查看我是否在以正确的方式行事,或者是否有针对此问题的更好的解决方案/模式。

I would suggest to have it in Facade like style. 我建议在Facade之类的样式中使用它。 The main DAOImpl has references to all the sub DAOs delegating the calls to appropriate one. 主DAOImpl具有对所有子DAO的引用,这些子DAO将调用委派给适当的子DAO。

UPD: to illustrate the approach UPD:说明方法

interface DAO {
 void doSomethingUser();
 void doSomethingProject();
}

class DAOImpl {
  private UserDAOImpl;
  private ProjectDAOImpl;
  public void doSomethingUser() {
    UserDAOImpl.doSomethingUser();
  }
  public void doSomethingProject() {
    ProjectDAOImpl.doSomethingProject();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM