Java

Usage examples: The most popular usage of the Mediator pattern in Java code is facilitating communications between GUI components of an app. The synonym of the Mediator is the Controller part of MVC pattern.

Here are some examples of the pattern in core Java libraries:

Notes app

This example shows how to organize lots of GUI elements so that they cooperate with the help of a mediator but don’t depend on each other.

components: Colleague classes

components/Component.java

package refactoring_guru.mediator.example.components;

import refactoring_guru.mediator.example.mediator.Mediator;

/**
 * Common component interface.
 */
public interface Component {
    void setMediator(Mediator mediator);
    String getName();
}

components/AddButton.java

components/DeleteButton.java

components/Filter.java

components/List.java

components/SaveButton.java

components/TextBox.java

components/Title.java

mediator

mediator/Mediator.java: Defines common mediator interface

mediator/Editor.java: Concrete mediator

mediator/Note.java: A note’s class

Demo.java: Initialization code

OutputDemo.png: Execution result

Last updated