简体   繁体   中英

Creating a default DAO interface

I'm wondering if it's a good practise to create default DAO interface instead of creating own interface for each class.

public interface DAO {

    public void addItem();
    public void updateItem();
    public void removeItem();
    public Object getItem(int id);
    public Object[] getAll();
}

Now we can implement this interface by multiple classes. Of course this sollution has it's cons like downcasting during retrieving data but I think it's still more efficient and code clear. Is this a good way to go?

It is a good way, but at least one improvement can be done using generics:

public interface DAO<T> {
    public void addItem(T item);
    public void updateItem(T item);
    public void removeItem(T item);
    public T getItem(int id);
    public List<T> getAll();
}

So now you will not need any casting. But anyway, you must make sure that all the DAOs will at least have those methods. Otherwise that will lead to more complexities. Moreover, if there are some DAOs that will have just exactly those methods, you will end up with pretty much compact clean code, eg:

public interface UserDAO extends DAO<User> {
    // And that is basically it :)
}

public class UserDAOImpl implements UserDAO {
    // Your implementations here
    // ...
}

Note : I have replaced the Object[] with List<T> and not T[] as you cannot do that in case of generics. But that is not a cons, it is better to use built-in containers.

No. Following the below structure will be clean.

interface DAO {
   void insert();
   void update();
   Object read(Integer id);
   void delete();
}

class DAOImpl implements DAO {
   void insert(){}
   void update(){}
   Object read(Integer id){}
   void delete(){}
}

class ItemDAO extends DAOImpl{
   public void addItem(Item i){
     insert(i);
   }
   public void updateItem(Item i){
     update(i);
   }
   public void removeItem(Item i){
     delete(i);
   }
   public Object getItem(int id){
     read(id);
   }
}

I wouldn't do it. What if you have a table that you only read from (maybe some other module/program/etc updates it? or if it's just some settings that you want to set once manually and then forget?). Or what if you want to be able to get multiple items from the same table (maybe you only need the ids for some things and the full object for something else)? While it might be easier to write, I think the interfaced DAOs are harder to use.

I also don't really think readability is any better. CarDAO.getItem(123) tells you a lot less than CarDAO.getCarById(123) . And you don't have to overload it if you want to get Cars by something else (yes, you could just make something that isn't in the interface, but then the benefit of the interface is even smaller).

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