简体   繁体   中英

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. 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
  2. Implement a DAOImpl class which implements the DAOInterface but will not have any logic in any of the methods
  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.
  4. Change the current DAOFactory class to provide instances of object specific DAOImpl based on some identifier passed from the business logic layer.

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. The main DAOImpl has references to all the sub DAOs delegating the calls to appropriate one.

UPD: to illustrate the approach

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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