Here is a summary of the ** Mediator pattern ** in the GoF design pattern.
――The English word Mediator means ** mediator **, but it is easier to understand if you actually think of it as ** advisor **. ――If you have 10 members and you instruct each other, the work will be confusing. In such a case, if there is a ** counselor ** in a different position, the member should report only to the counselor, and support for the member should be received only from the counselor. ――The Mediator pattern is a method that allows you to take action through a consultant **. --The GoF design patterns are classified as ** behavioral design patterns **.

A program that displays a login dialog and controls the enabled / disabled state of text and buttons.
An interface that serves as a consultant.
Mediator.java
public interface Mediator {
	public abstract void createColleagues();
	public abstract void colleagueChanged();
}
A class that represents a login dialog. Implement the Mediator interface.
LoginFrame.java
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginFrame extends Frame implements ActionListener, Mediator {
	private ColleagueCheckbox checkGuest;
	private ColleagueCheckbox checkLogin;
	private ColleagueTextField textUser;
	private ColleagueTextField textPass;
	private ColleagueButton buttonOk;
	private ColleagueButton buttonCancel;
	public LoginFrame(String title) {
		super(title);
		setBackground(Color.lightGray);
		setLayout(new GridLayout(4, 2));
		createColleagues();
		add(checkGuest);
		add(checkLogin);
		add(new Label("Username:"));
		add(textUser);
		add(new Label("Password:"));
		add(textPass);
		add(buttonOk);
		add(buttonCancel);
		colleagueChanged();
		pack();
		setVisible(true);
	}
	public void createColleagues() {
		CheckboxGroup g = new CheckboxGroup();
		checkGuest = new ColleagueCheckbox("Guest", g, true);
		checkLogin = new ColleagueCheckbox("Login", g, false);
		textUser = new ColleagueTextField("", 10);
		textPass = new ColleagueTextField("", 10);
		textPass.setEchoChar('*');
		buttonOk = new ColleagueButton("OK");
		buttonCancel = new ColleagueButton("Cancel");
		checkGuest.setMediator(this);
		checkLogin.setMediator(this);
		textUser.setMediator(this);
		textPass.setMediator(this);
		buttonOk.setMediator(this);
		buttonCancel.setMediator(this);
		checkGuest.addItemListener(checkGuest);
		checkLogin.addItemListener(checkLogin);
		textUser.addTextListener(textUser);
		textPass.addTextListener(textPass);
		buttonOk.addActionListener(this);
		buttonCancel.addActionListener(this);
	}
	//Each College is valid by notification from the College/Judge invalidity.
	public void colleagueChanged() {
		if (checkGuest.getState()) {
			// Guest mode
			textUser.setColleagueEnabled(false);
			textPass.setColleagueEnabled(false);
			buttonOk.setColleagueEnabled(true);
		} else {
			// Login mode
			textUser.setColleagueEnabled(true);
			userpassChanged();
		}
	}
	private void userpassChanged() {
		if (textUser.getText().length() > 0) {
			textPass.setColleagueEnabled(true);
			if (textPass.getText().length() > 0) {
				buttonOk.setColleagueEnabled(true);
			} else {
				buttonOk.setColleagueEnabled(false);
			}
		} else {
			textPass.setColleagueEnabled(false);
			buttonOk.setColleagueEnabled(false);
		}
	}
	public void actionPerformed(ActionEvent e) {
		System.out.println(e.toString());
		System.exit(0);
	}
}
An interface to become a member.
Colleague.java
public interface Colleague {
	public abstract void setMediator(Mediator mediator);
	public abstract void setColleagueEnabled(boolean enabled);
}
A class that represents a button. Implements the Colleague interface.
ColleagueButton.java
import java.awt.Button;
public class ColleagueButton extends Button implements Colleague {
	private Mediator mediator;
	public ColleagueButton(String caption) {
		super(caption);
	}
	public void setMediator(Mediator mediator) {
		this.mediator = mediator;
	}
	public void setColleagueEnabled(boolean enabled) {
		//Valid from Mediator/Invalidation is instructed
		setEnabled(enabled);
	}
}
A class that represents a checkbox (here a radio button). Implements the Colleague interface.
ColleagueCheckbox.java
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class ColleagueCheckbox extends Checkbox implements ItemListener, Colleague {
	private Mediator mediator;
	public ColleagueCheckbox(String caption, CheckboxGroup group, boolean state) {
		super(caption, group, state);
	}
	public void setMediator(Mediator mediator) {
		this.mediator = mediator;
	}
	public void setColleagueEnabled(boolean enabled) {
		//Valid from Mediator/Invalidation is instructed
		setEnabled(enabled);
	}
	public void itemStateChanged(ItemEvent e) {
		//Notify Mediator when status changes
		mediator.colleagueChanged();
	}
}
A class that represents a text box. Implements the Colleague interface.
ColleagueTextField.java
import java.awt.Color;
import java.awt.TextField;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
public class ColleagueTextField extends TextField implements TextListener, Colleague {
	private Mediator mediator;
	public ColleagueTextField(String text, int columns) {
		super(text, columns);
	}
	public void setMediator(Mediator mediator) {
		this.mediator = mediator;
	}
	public void setColleagueEnabled(boolean enabled) {
		//Valid from Mediator/Invalidation is instructed
		setEnabled(enabled);
		setBackground(enabled ? Color.white : Color.lightGray);
	}
	public void textValueChanged(TextEvent e) {
		//Notify Mediator when the string changes
		mediator.colleagueChanged();
	}
}
This class performs the main processing.
Main.java
public class Main {
	static public void main(String args[]) {
		new LoginFrame("Mediator Sample");
	}
}

The logic for enabling / disabling the display is complicated, but it is mediated by the LoginFrame class. If you change the display specifications or find a bug, you only need to fix and debug the LoginFrame class. Once the logic is distributed across the CollegeButton, CollegeCheckbox, and CollegeTextField, it's hard to write, debug, and modify.
-** GoF design pattern summary **
This article and sample program were created based on the following books.
-** Introduction to design patterns learned in Java language **
It was very easy to understand and I learned a lot. Thank you. The detailed explanations of the design patterns and sample programs are written, so please take a look at the books as well.
Recommended Posts