仲裁者模式和中介者模式类似,但作用又有不同之处,仲裁者模式是对多个对象的管理协调,而中介者一般是一代一。
仲裁者有两个角色,仲裁者和被仲裁者,使用场景比如winform模式,form就是仲裁者,各种控件就是被仲裁者,仲裁者负责协调管理。
代码示例
package designMode.mediator;
public interface Mediator {
public abstract void createColleagues();
public abstract void ColleagueChanged();
}
package designMode.mediator;
public interface Colleague {
public abstract void setMediator(Mediator mediator);
public abstract void setColleagueEnable(boolean enable);
}
package designMode.mediator;
import java.awt.*;
public class ColleagueButton extends Button implements Colleague {
private static final long serialVersionUID = 1309335015058337931L;
public ColleagueButton(String caption){
super(caption);
}
private Mediator mediator;
@Override
public void setMediator(Mediator mediator) {
this.mediator=mediator;
}
@Override
public void setColleagueEnable(boolean enable) {
setEnabled(enable);
}
}
package designMode.mediator;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class ColleagueCheckBox extends Checkbox implements ItemListener,Colleague {
private static final long serialVersionUID = -628547709974079324L;
public ColleagueCheckBox(String caption,CheckboxGroup g,boolean b){
super(caption,g,b);
}
private Mediator mediator;
@Override
public void setMediator(Mediator mediator) {
this.mediator=mediator;
}
@Override
public void setColleagueEnable(boolean enable) {
setEnabled(enable);
}
@Override
public void itemStateChanged(ItemEvent e) {
mediator.ColleagueChanged();
}
}
package designMode.mediator;
import java.awt.*;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
public class ColleagueTextField extends TextField implements TextListener,Colleague {
private static final long serialVersionUID = 8539124564669846422L;
public ColleagueTextField(String text,int columns){
super(text,columns);
}
private Mediator mediator;
@Override
public void setMediator(Mediator mediator) {
this.mediator=mediator;
}
@Override
public void setColleagueEnable(boolean enable) {
setEnabled(enable);
setBackground( enable? Color.WHITE : Color.BLACK );
}
@Override
public void textValueChanged(TextEvent e) {
mediator.ColleagueChanged();
}
}
package designMode.mediator;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginFrame extends Frame implements ActionListener, Mediator {
private static final long serialVersionUID = -509490729876024682L;
private ColleagueButton buttonOK;
private ColleagueButton buttonCancel;
private ColleagueCheckBox chkGuest;
private ColleagueCheckBox chkUser;
private ColleagueTextField tfUser;
private ColleagueTextField tfPass;
public LoginFrame(String title){
super(title);
setBackground(Color.lightGray);
setLayout(new GridLayout(4,2));
createColleagues();
add(chkGuest);
add(chkUser);
add(new Label("用户名:"));
add(tfUser);
add(new Label("密码:"));
add(tfPass);
add(buttonOK);
add(buttonCancel);
ColleagueChanged();
pack();
show();
}
public void createColleagues() {
CheckboxGroup chk=new CheckboxGroup();
chkGuest=new ColleagueCheckBox("Guest",chk,true);
chkUser=new ColleagueCheckBox("User",chk,false);
buttonOK=new ColleagueButton("OK");
buttonCancel=new ColleagueButton("Cancel");
tfUser=new ColleagueTextField("",10);
tfPass=new ColleagueTextField("",10);
tfPass.setEchoChar('#');
chkGuest.setMediator(this);
chkUser.setMediator(this);
buttonOK.setMediator(this);
buttonCancel.setMediator(this);
tfUser.setMediator(this);
tfPass.setMediator(this);
chkGuest.addItemListener(chkGuest);
chkUser.addItemListener(chkUser);
buttonOK.addActionListener(this);
buttonCancel.addActionListener(this);
tfUser.addTextListener(tfUser);
tfPass.addTextListener(tfPass);
}
public void ColleagueChanged() {
if(chkGuest.getState()){
tfUser.setColleagueEnable(false);
tfPass.setColleagueEnable(false);
buttonOK.setColleagueEnable(true);
}else{
tfUser.setColleagueEnable(true);
userPassChanged();
}
}
private void userPassChanged() {
if(tfUser.getText().length()>0){
tfPass.setColleagueEnable(true);
if(tfPass.getText().length()>0){
buttonOK.setColleagueEnable(true);
}else{
buttonOK.setColleagueEnable(false);
}
}else{
tfPass.setColleagueEnable(false);
buttonOK.setColleagueEnable(false);
}
}
public void actionPerformed(ActionEvent e) {
System.out.println(e.toString());
System.exit(0);
}
}
package designMode.mediator;
public class Main {
public static void main(String[] args) {
LoginFrame login = new LoginFrame("仲裁者模式");
}
}
标签:仲裁者,mediator,void,private,模式,public,setColleagueEnable From: https://www.cnblogs.com/seeksimple/p/17910360.html