只有一个仲裁者
Mediator 模式
组员向仲裁者报告,仲裁者向组员下达指示,组员之间不在相互询问和相互指示。
要调整多个对象之间的关系时,就需要用到 Mediator 模式。将逻辑处理交给仲裁者执行。
示例程序
名字 | 说明 | 角色 |
---|---|---|
Mediator | 定义“仲裁者”的接口的接口 | Mediator |
Colleague | 定义“组员”的接口的接口 | Colleague |
ColleagueButton | 表示按钮的类。它实现了 Colleague 接口 | ConcreteColleague |
ColleagueTextField | 表示文本输入框的类。它实现了 Colleague 接口 | ConcreteColleague |
ColleagueCheckbox | 表示单选框的类。它实现了 Colleague 接口 | ConcreteColleague |
LoginFrame | 表示登录对话框的类。它实现了 Mediator 接口 | ConcreteMediator |
Main | 测试程序行为的类 |
Mediator.java
package mediator;
public interface Mediator {
public abstract void createColleague();
public abstract void colleagueChanged();
}
Colleague.java
package mediator;
public interface Colleague {
public abstract void setMediator(Mediator mediator);
public abstract void setColleagueEnabled(boolean enabled);
}
ColleagueButton.java
package mediator;
import java.awt.*;
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) {
setEnabled(enabled);
}
}
ColleagueTextField.java
package mediator;
import java.awt.*;
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) {
setEnabled(enabled);
setBackground(enabled ? Color.WHITE : Color.lightGray);
}
public void textValueChanged(TextEvent e) {
mediator.colleagueChanged();
}
}
ColleagueCheckbox.java
package mediator;
import java.awt.*;
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) {
setEnabled(enabled);
setBackground(enabled ? Color.WHITE : Color.lightGray);
}
public void itemStateChanged(ItemEvent e) {
mediator.colleagueChanged();
}
}
LoginFrame.java
package mediator;
import java.awt.*;
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;
// 构造函数
// 生成并配置各个Colleague后,显示对话框
public LoginFrame(String title) {
super(title);
setBackground(Color.lightGray);
setLayout(new GridLayout(4, 2));
createColleague();
add(checkGuest);
add(checkLogin);
add(new Label("Username:"));
add(textUser);
add(new Label("Password:"));
add(textPass);
add(buttonOk);
add(buttonCancel);
colleagueChanged();
pack();
show();
}
public void createColleague() {
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);
}
public void colleagueChanged() {
if (checkGuest.getState()) {
textUser.setColleagueEnabled(false);
textPass.setColleagueEnabled(false);
buttonOk.setColleagueEnabled(true);
} else {
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);
}
}
Main.java
package mediator;
public class Main {
static public void main(String args[]) {
new LoginFrame("Mediator Sample");
}
}
Mediator 模式中的角色
-
Mediator(仲裁者、中介者)
Mediator 角色负责定义与 Colleague 角色进行通信和做出决定的接口。
-
ConcreteMediator(具体的仲裁者、中介者)
ConcreteMediator 角色负责实现 Mediator 角色的接口,负责实际做出决定。
-
Colleague(同事)
Colleague 角色负责定义与 Mediator 角色进行通信的接口。
-
ConcreteColleague(具体的同事)
ConcreteColleague 角色负责实现 Colleague 角色的接口。
拓展思路
一、Bug容易定位,因为没有别的控制控件做逻辑控制处理。
二、ConcreteColleague 角色可以复用,ConcreteMediator 角色难以复用。
网友评论