0

Let's assume I have a User class and two subclasses Employee and Customer. I implemented this hierarchy as a table-per-hierarchy in DB with a column for specifying the type of user. I need to return the right type of object when querying this table.

Do I need separate DAOs for each type of object like CustomerDAO or EmployeeDAO, so each return their respective Customer and Employee objects. If so how to get them from DAOFactory without using:

if(type.equlas('customer'))
    return customerDao;
else
    retrun employeeDao;

Because the types implementing User could change and I don't want to change conditions every time.

Or is there any other way? Any idea would be appreciated.

Note: I'm not using any ORM framework and not planning to use one.

doctrey
  • 1,227
  • 1
  • 12
  • 25
  • Can you say a little more about your class hierarchy? Is Employee a subclass of User, or is there some linkage between the two entities? Also, what reference type(s) do you expect back from your DAO(s)? – jtoberon Jul 08 '11 at 20:31
  • @jtoberon Yes Employee and Customer are types of User. All I have is user IDs. Based on these IDs I have to query the same table and get `Customer` if user is of type customer and `Employee` if the type is employee. – doctrey Jul 08 '11 at 21:13

1 Answers1

0

If your persistence code for each type is the same, then you could have 1 generic DAO.

So your user dao, could be something like:

interface DAO<T, ID> {
  T create(T t);
  T read(ID id);
  T update(T t);
  void delete(T t);  
}

class UserDAO<T extends User> implements DAO<T> {
    // Your methods for crud operations will be limited to types of User.
}

Then your factory class could instantiate the correct DAO simply by specifying the correct type.

class DAOFactory {
  public UserDAO<Employee> getEmployeeDAO() {
    return new UserDAO<Employee> ();
  }
}

Regards
Yusuf

Yusuf Jakoet
  • 136
  • 3
  • No the code for each DAO is not the same. Each need to fill their respective objects data. I think this way I still need to check for the types of object in an `if ... else` to get the required DAO. – doctrey Jul 09 '11 at 07:16
  • I don't see the need for the check if your factory has a method for each type...? Your if statement implies that your DAO that is returned is generic. Is that the case? – Yusuf Jakoet Jul 09 '11 at 13:24
  • Maybe I'm missing something here but don't I need to know what type of DAO I want, to call the right method on DAO factory. Anyway thanks for your response. I tried to solve the problem in another way to eliminate the need for multiple DAOs. Next time I face similar problem I think I'm gonna try Reflection. – doctrey Jul 11 '11 at 17:05
  • Only when you know what data you're working with, will it make sense to return the correct DAO from the factory. So when you want to query the table without any knowledge of type (which is what you want), you must use the base classes' DAO which will return User references. – Yusuf Jakoet Jul 11 '11 at 21:03
  • That's what I want to know. User object won't work for me. I need concrete subclasses like Customer and Employee. – doctrey Jul 12 '11 at 05:39