Skip to content
Demonstrates how the decorator pattern could be implemented using the features of Java 8.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

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)
	.decorate(l -> new OnActivateHighlightComponent(l, urlLabel))


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.


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

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().

You can’t perform that action at this time.