中介者模式 一、什么是中介者模式? 中介者模式(Mediator Design Pattern):定义一个单独的(中介)对象,来封装一系列对象的交互。将这组对象之间的交互都委派给中介对象,来避免对象之间的直接交互。
英文原文:
Mediator pattern defines a separate (mediator) object that encapsulates the interaction between a set of objects and the objects delegate their interaction to a mediator object instead of interacting with each other directly.
它有几个特点:
一组对象之间存在相互交流的情况,也就是一种网状关系
定义一个上帝(中介)对象,负责中转对象之间的交互,来避免对象之间的直接联系
相当于加了个中间层转换,把网状关系变成了星型关系
中介者模式,也称为调停模式,负责多个对象之间的互相沟通。
二、为什么要用中介者模式? 中介者模式的应用场景比较少,一般用在下面的场景中:
通过使用中介者模式,可以避免复杂的网状关系,降低对象之间的耦合度。
原来1个对象需要和n个对象交互,使用中介者模式后,1个对象只需要和中介者打交道就可以了。
三、怎么用中介者模式? 中介者模式中的角色:
中介者(Mediator):定义决策的接口
具体中介者(Concrete Mediator):实现了中介者接口的具体角色
同事(Colleague):定义了与中介者交互的接口
具体同事(Concrete Colleague):实现了同事接口的具体角色
中介者模式结构:
示例程序结构:
中介者(Mediator):
1 2 3 4 public interface Mediator { void createColleagues () ; void colleagueChanged () ; }
同事(Colleague):
1 2 3 4 public interface Colleague { void setMediator (Mediator mediator) ; void setColleagueEnabled (boolean enabled) ; }
具体同事(Concrete Colleague):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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(); } }
具体中介者(Concrete Mediator)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 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, true ); 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 ); buttonOk.setColleagueEnabled(textPass.getText().length() > 0 ); } else { textPass.setColleagueEnabled(false ); buttonOk.setColleagueEnabled(false ); } } @Override public void actionPerformed (ActionEvent e) { System.out.println(e.toString()); System.exit(0 ); } }
四、中介者模式有什么优缺点? 优点:
从网状关系,变成了星型关系,复杂度变低了
对象之间的交互变简单了,更容易扩展和维护
缺点: