Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding DAO-pattern and interfaces

I'm reading J. Bloch's effective Java and he said the following:

Once an interface is released and widely implemented, it is almost impossible to change.

So, now consider the simple interface for DAO-pattern:

public interface UserDao{

    public User getById(int id);

    public Collection<User> getAll();

    public boolean delete(int userId);

    public boolean update(User u);

}

This is how my Dao interface looked when it was released firstly. By the time, I had to add some functionality to users in order to aggregate all users by its registration_date or something similar. So, I needed to add corresponding method declarations to the DAO-interface and implement it.

Moreover for now I can't imagine how DAO-interfaces may be more or less stable at all, because adding some new DAO-operations happens very often.

Maybe it's my DAO design disaster, or interfaces are hardly suitable for DAOs?

like image 471
St.Antario Avatar asked Oct 27 '15 09:10

St.Antario


1 Answers

I think that sentence of J. Bloch is meant to public interfaces, and not for your DAO case.

Think of you creating a public API to let any programmer use your platform. If you are changing that interface, your are requiring programmers to adapt his code, so this will be frustrating.

But if you are creating an interface for the purpose of your internal app, you are not assuming that risk, and it is worth to evolve your interface as you need.

Obviously you have to consider how many classes implements your interface and assume the cost of the modification.

like image 108
malaguna Avatar answered Oct 21 '22 19:10

malaguna