组员向仲裁者报告,仲裁者向组员下达指示。组员之间不再相互询问和相互指示。
示例
一个GUI应用程序,它展示了一个登录对话框,用户在其中输入正确的用户名和密码后可以登录。
- 可以选择作为游客访问(Guest)或是作为用户登录(Login)
- 作为用户登录时,需要输入正确的用户名(Username)和密码(Password)
- 点击OK按钮可以登录,点击Cancel按钮可以取消登录
类图
时序图
Mediator接口
1234 |
public interface { public abstract void createColleagues(); public abstract void colleagueChanged();} |
Colleague接口
1234 |
public interface Colleague { public abstract void setMediator(Mediator mediator); public abstract void setColleagueEnabled(boolean enabled);} |
ColleagueButton类
12345678910111213141516171819 |
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类
123456789101112131415161718192021222324252627 |
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类
1234567891011121314151617181920212223242526 |
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); } @Override public void setMediator(Mediator mediator) { this.mediator = mediator; } @Override public void setColleagueEnabled(boolean enabled) { setEnabled(enabled); } @Override public void itemStateChanged(ItemEvent e) { mediator.colleagueChanged(); }} |
LoginFrame类
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener; public class LoginFrame extends Frame implements ActionListener, { 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(); show(); } @Override 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); } @Override 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); } } @Override public void actionPerformed(ActionEvent e) { System.out.println(e.toString()); System.exit(0); }} |
Main类
123456 |
public class Main { public static void main(String[] args) { new LoginFrame("Mediator Sample"); }} |
运行结果
登场角色
Mediator(仲裁者、中介者)
Mediator角色负责定义与Colleague角色进行通信和做出决定的接口(API)。在示例程序中,由Mediator接口扮演此角色。
ConcreteMediator(具体的仲裁者、中介者)
ConcreteMediator角色负责实现Mediator角色的接口(API),负责实际做出决定。在示例程序中,由LoginFrame类扮演此角色。
Colleague(同事)
Colleague角色负责定义与Mediator角色进行通信的接口(API)。在示例程序中,由Colleague接口扮演此角色。
ConcreteColleague(具体的同事)
ConcreteColleague角色负责实现Colleague角色的接口(API)。在示例程序中,由ColleagueButton类、ColleagueTextField类和ColleagueCheckbox类扮演此角色。
类图
原文地址:https://www.cnblogs.com/chinatrump/p/11584628.html
时间: 2024-12-11 12:52:28