demo-decorator-java-8 icon indicating copy to clipboard operation
demo-decorator-java-8 copied to clipboard

Demonstrates how the decorator pattern could be implemented using the features of Java 8.

Decorator Pattern With Java 8

This project provides the code samples for the CodeFX post about the decorator pattern in Java 8. It uses static and default interface methods to create a fluent API for decorations:

HyperlinkListener listener = this::changeHtmlViewBackgroundColor;
listener = DecoratingHyperlinkListener.from(listener)
	.onHoverMakeVisible(urlLabel)
	.onHoverSetUrlOn(urlLabel)
	.logEvents()
	.decorate(l -> new OnActivateHighlightComponent(l, urlLabel))
	.decorate(OnEnterLogUrl::new);

Swing?

The code uses Swing's HyperlinkListener for its examples. The main reason is to continue on a previous post but it turns out to be a good interface for the demonstrations as it is not generic and has only one method with one argument. All of this reduces the necessary clutter to a bare minimum.

The demo is executable and creates a small Swing frame which contains two links and reacts to events on them.

Code

The code is split into the package which defines the decoration API (org.codefx.lab.decorator.def) and the using code in Demo.java.

The latter stands for all the places in a project where the decorations are used but new decorators are so specific that they should not be added to the decoration API. This is represented by the private listener classes in Demo. It contains the above code sample in createHyperlinkListener().