A pattern to prepare a "counselor". The members who perform the work inform the counselor when an event that spreads to the whole occurs. In addition, we will obediently respond to instructions from the advisor. The counselor makes a big decision based on the news from the members. Give instructions to each member.
Communicate with the Colleague role and define an interface for making adjustments.
package mediator;
public interface Mediator {
//Generate members managed by this Mediator
public abstract void createColleagues();
//Consultation with a consultant
public abstract void collegueChanged();
}
Implement the interface that plays the role of Mediator and make the actual adjustments.
package mediator;
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);
//Create a 4x2 grid using the layout manager
setLayout(new GridLayout(4, 2));
//Generation of Colleagues
createColleagues();
//Placement
add(checkGuest);
add(checkLogin);
add(new Label("User name:"));
add(textUser);
add(new Label("Password:"));
add(textPass);
add(buttonOk);
add(buttonCancel);
//Effectiveness/Invalid initial setting
collegueChanged();
//display
pack();
show();
}
@Override
public void createColleagues() {
//Generate
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");
// Mediator(Advisor)Set of
checkGuest.setMediator(this);
checkLogin.setMediator(this);
textUser.setMediator(this);
textPass.setMediator(this);
buttonOk.setMediator(this);
buttonCancel.setMediator(this);
//Set of Listeners
checkGuest.addItemListener(checkGuest);
checkLogin.addItemListener(checkLogin);
textUser.addTextListener(textUser);
textPass.addTextListener(textPass);
buttonOk.addActionListener(this);
buttonCancel.addActionListener(this);
}
//Valid for notifications from Colleague/Judge invalid
@Override
public void collegueChanged() {
if (checkGuest.getState()) {
//Guest mode
textPass.setColleagueEnabled(true);
if (textPass.getText().length() > 0) {
buttonOk.setEnabled(true);
} else {
buttonOk.setEnabled(false);
}
} else {
//Login mode
textPass.setColleagueEnabled(false);
buttonOk.setColleagueEnabled(false);
}
}
//There was a change in textUser or textPass
//Validity of each College/Judge invalid
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e);
System.exit(0);
}
}
Define an interface to communicate with the Mediator role.
package mediator;
public interface Colleague {
public abstract void setMediator(Mediator mediator);
//Corresponds to the instructions coming from the consultant
public abstract void setColleagueEnabled(boolean enabled);
}
Implement the interface for Colleague.
package mediator;
import java.awt.Button;
/**
*LoginFrame class(Mediator interface)Coordinate with
*
*/
public class ColleagueButton extends Button implements Colleague{
private Mediator mediator;
public ColleagueButton(String caption) {
super(caption);
}
@Override
public void setMediator(Mediator mediator) {
this.mediator = mediator;
}
//Valid from Mediator/Invalidation is instructed
@Override
public void setColleagueEnabled(boolean enabled) {
setEnabled(enabled);
}
}
package mediator;
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);
}
@Override
public void setMediator(Mediator mediator) {
this.mediator = mediator;
}
@Override
public void setColleagueEnabled(boolean enabled) {
setEnabled(enabled);
setBackground(enabled ? Color.white : Color.lightGray);
}
//Notify Mediator when the string changes
public void textValueChanged(TextEvent e) {
mediator.collegueChanged();
}
}
package mediator;
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);
}
@Override
public void setMediator(Mediator mediator) {
this.mediator = mediator;
}
@Override
public void setColleagueEnabled(boolean enabled) {
setEnabled(enabled);
}
@Override
public void itemStateChanged(ItemEvent e) {
mediator.collegueChanged();
}
}
In the above program, the LoginFrame class, which is a consultant, determines whether the button is active or inactive. Each member (ColleagueButton, TextField, CheckBox) only informs the advisor, and the decision is left to the advisor.
https://github.com/aki0207/mediator
I used this as a reference. Augmented and Revised Introduction to Design Patterns Learned in Java Language
Recommended Posts