Using Java 8 default methods to “fix” bloated interfaces

Vaadin and some other Java frameworks tend to have some rather “verbose” interfaces. For example Container and its sub interfaces has lots of methods that you actually don’t need to implement, except with a dummy implementation that returns null or throws an UnsupportedOperationException. Similarly, View has its enter method which I seldom really need in my projects.

Java 8 introduced so called default methods that you can add to interfaces. They allow you do easily hide the noise this kind of methods cause to your code. Here is a View extension I used in my recent hobby project:

import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;

public interface MyView extends View {

@Override
default void enter(ViewChangeListener.ViewChangeEvent event) {
}

}

In my actual views, where I inherit MyView instead of View, I don’t need to implement the empty enter method any more and still those classes can still inherit from different super classes. Pretty handy.

BTW. Instead of fixing Container in the same way, I suggest to forget that interfeace altogether and use Viritin and its built in support for java.util.List/Collection 😉

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.