The right usage of lambdas with event listeners
Submitted on 13 April 2014There’s been much advertisement about how Java 8’s lambdas bring a big asset to Vaadin development. The following is a pre-Java 8 usage of a ClickListener
:
Java 8’s lambda make it much easier, as this snippet shows:
However, it is my opinion, that using lambdas this way leads to bad architecture and unmaintainable design, for it strongly couples GUI components and behavior.
Object-oriented design should follow SOLID principles, first among them being the Single Responsibility Principle:
In object-oriented programming, the single responsibility principle states that every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility.
Using lambdas as in the previous snippet just defeats this principle. Enforcing it requires a dedicated class for the behavior:
Not only is this snippet sound Object-Oriented design, it allows to pass parameters to the constructor and store them as attributes. Usage of this listener would be as the following:
Storing the container as an attribute in the listener is better than using lambdas directly, but introduces strong coupling from the behavior to the GUI in the form of an association. In this regard, I’ve already introduced Guava’s EventBus in a former post.
This would translate into the following code:
Usage then becomes:
Of course, this is a little verbose, and that’s when lambdas come in here handy. The previous snippet can be updated with Java 8’s lambdas as:
IMHO, this is the right usage of lambdas in regard to Vaadin event listeners.
You can find relevant sources for this article on Github.
Tags: