原理:
代码清单:
Mediator
public interface Mediator { void createColleagues(); void colleagueChanged();}
Colleague
public interface Colleague { void setMediator(Mediator mediator); void setColleagueEnabled(boolean enabled);}
ColleagueButton
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; } @Override public void setColleagueEnabled(boolean enabled) { setEnabled(enabled); }}
ColleagueCheckbox
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(); }}
ColleagueTextField
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); } @Override public void textValueChanged(TextEvent e) { mediator.colleagueChanged(); }}
LoginFrame
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(); 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:
public class Main { public static void main(String[] args){ new LoginFrame("Mediator Sample"); }}
结果:
1.Guest 不允许输入账户
2.Login 只有输入username 才可以输入password
3.Login 只有username 与 password 输全才可以 OK