[JAVA] Design pattern ~ Mediator ~

1.First of all

Here is a summary of the ** Mediator pattern ** in the GoF design pattern.

2. What is the Mediator 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 **.

3. Sample class diagram

Mediator.PNG

4. Sample program

A program that displays a login dialog and controls the enabled / disabled state of text and buttons.

4-1. Mediator interface

An interface that serves as a consultant.

Mediator.java


public interface Mediator {
	public abstract void createColleagues();
	public abstract void colleagueChanged();
}

4-2. LoginFrame class

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);
	}
}

4-3. Colleague interface

An interface to become a member.

Colleague.java


public interface Colleague {
	public abstract void setMediator(Mediator mediator);
	public abstract void setColleagueEnabled(boolean enabled);
}

4-4. ColleagueButton class

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);
	}
}

4-5. ColleagueCheckbox class

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();
	}
}

4-6. ColleagueTextField class

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();
	}
}

4-7. Main class

This class performs the main processing.

Main.java


public class Main {
	static public void main(String args[]) {
		new LoginFrame("Mediator Sample");
	}
}

4-8. Execution result

f.PNG

5. Benefits

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.

  1. GitHub

7. List of design patterns

-** GoF design pattern summary **

8. Reference

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

Design pattern ~ Mediator ~
Mediator pattern
Mediator Pattern
Design pattern ~ Builder ~
Design pattern ~ Visitor ~
Java design pattern
Design pattern ~ Proxy ~
Design pattern ~ State ~
Design pattern ~ Strategy ~
Design pattern ~ Composite ~
Design pattern (1): AbstractFactory
Design pattern ~ Iterator ~
Design pattern ~ Facade ~
Design pattern ~ Bridge ~
Design pattern ~ Decorator ~
Design pattern ~ Interpreter ~
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
Design pattern ~ Memento ~
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
C ++ design pattern (TemplateMethod pattern)
Design pattern ~ Factory Method ~
Design pattern ~ Abstract Factory ~
GoF design pattern summary
Design pattern ~ Template Method ~
Java design pattern summary
Design pattern ~ Chain of Responsibility ~
[Design pattern] Java core library
Ruby design pattern template method pattern memo
C # chewed design pattern: Template Method
Application example of design pattern (No. 1)
Java beginner design pattern (Factory Method pattern)
Prototype pattern
Memento Pattern
Iterator pattern
Composite pattern
Observer Pattern
Builder pattern
Bridge Pattern
Command Pattern
Builder Pattern
Strategy pattern
Iterator Pattern
Visitor pattern
Adapter Pattern
Proxy Pattern
Strategy Pattern
Composite Pattern
Singleton pattern
Facade Pattern
Decorator pattern
Flyweight Pattern
Decorator Pattern
Facade pattern
Visitor Pattern
Bridge pattern
PrintObserver "Observer Design Pattern: Description and Implementation Example"
Java8 Lambda expression & Stream design pattern reconsideration --Command pattern -