当前位置:  开发笔记 > 编程语言 > 正文

Java静态方法所需的实现

如何解决《Java静态方法所需的实现》经验,为你挑选了0个好方法。

我必须完成的任务已经完成,但是我仍然想着一个问题。

我定义了以下接口:

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:

List list = 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.

抓住这个陷阱的最优雅的解决方案是什么?

推荐阅读
郑小蒜9299_941611_G
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有