Understanding the DAO Template and Interfaces

I am reading effective Java J. Bloch , and he said the following:

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

So, now consider a 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 what my Dao interface looked like when it was first released. By then, I had to add some features for users to aggregate all users by registration_date or something similar. Therefore, I needed to add the appropriate method descriptions to the Dao interface and implement it.

Moreover, I cannot imagine how Dao interfaces can be more or less stable in general, because the addition of some new Dao operations is very common.

Perhaps this is my DAO design disaster, or are interfaces hardly suitable for Dao s?

+5
source share
1 answer

I think the proposal by J. Bloch is for public interfaces, not for your DAO case.

Consider creating a public API so that any programmer can use your platform. If you change this interface, you need programmers to adapt its code, so this will be unpleasant.

But if you are creating an interface for your internal application, you do not assume this risk, and you should develop your interface as necessary.

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

+6
source

Source: https://habr.com/ru/post/1234591/


All Articles