我必须完成的任务已经完成,但是我仍然想着一个问题。
我定义了以下接口:
package dao; import java.sql.SQLException; /** * Get / save / delete a single instance from the db in RESTful fashion on the object * @author kimg * * @param*/ public interface IDao { 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:
Listlist = Person.fetchAll(); List 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.
抓住这个陷阱的最优雅的解决方案是什么?