Kim Gysen February 2016

Java static methods required implementation

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?

Answers


Andreas February 2016

Instead of having these methods on the Entity, try placing them on a repo/Dao:

public interface IDao<T> {
    void fetch(int id); 
    void save() throws SQLException; 
    void delete() throws SQLException; 
    List<T> list();
    List<T> list(offset, limit); 
    int count();
} 
public class Person {
    //...getters/setters/fields...
}
public class PersonDao implements IDao<Person> {//todo make me a singleton
    //implement the interface here
}


John Bollinger February 2016

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.

If the methods are so beneficial then wouldn't developers want to implement them? At least if they were aware of the desired patterns and the usefulness of implementing them?

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)

That's irrelevant, because static methods are not virtual. Users do know for sure whether the type whose static method they would like to invoke in fact does have that method. If they forget then the compiler will gladly tell them.

any typo in the method name, omitting any method can cause the smooth app workflow to break.

I'm not sure I follow you. If you're talking about developers failing to implement these static methods or doing so wrongly, then that's what code review is for. You could also write automated tests that reflectively verify the methods' presence. I don't really see how this is a bigger issue than the methods being implemented with the desired signature but incorrect implementation.

Again, static methods are not virtual. The user of every static method call knows at compile time exactly which method is being called, and therefore whether such a method is available. If they want a particular method and do not have it then they have the alternatives of requesting that it be added, adding it themselves, or doing without.

What would be the most elegant solution to catch this pitfall?

Document any such expectations. Have project management enforce them (and if they refuse to do, then maybe it wasn't so important after all). Employ

Post Status

Asked in February 2016
Viewed 3,252 times
Voted 8
Answered 2 times

Search




Leave an answer