The task I had to make has already been delivered, yet a certain question hangs around in my mind.
I defined the following interface:
package dao;
import java.sql.SQLException;
/**
* Get / save / delete a single instance from the db in RESTful fashion on the object
* @author kimg
*
* @param <T>
*/
public interface IDao<T> {
public void fetch(int id);
public void save() throws SQLException;
public void delete() throws SQLException;
}
Purpose is to have all pojo's that are represented as database table entities implement these methods, so the user of the pojo's knows how to handle instances according to a pattern. I've taken this approach from Backbone (Javascript).
However, there are other methods that I liked to impose as class methods (static) on the Pojo class itself. The most obvious methods are things like:
List<Person> list = Person.fetchAll();
List<Person> list = Person.fetchAll(offset, limit);
int i = Person.countAll();
...
I've found that having these methods defined by default offers great benefit, yet nothing forces a developer to implement them, as static methods can't be imposed by default. Also, users of the classes can't know for sure that they'll be able to use the static methods that they would otherwise expect by contract (in case of an interface); any typo in the method name, omitting any method can cause the smooth app workflow to break.
What would be the most elegant solution to catch this pitfall?