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.

    中介模式的英文翻译是Mediator Design Pattern。在GoF中的《设计模式》一书的定义翻译成中文就是:中介模式定义了一个单独的(中介)对象,来封装一组对象之间的交互。将这组对象之间的交互委派给与中介对象交互,来避免对象之间的直接交互。

    image.png

    提到中介模式,有一个比较经典的例子不得不说,那就是航空管制。

    为了让飞机在飞行的时候互不干扰,每架飞机都需要知道其他飞机每时每刻的位置,这就需要时刻跟其他飞机通信。飞机通信形成的通信网络就会无比复杂。这个时候,我们通过引入“塔台”这样一个中介,让每架飞机只跟塔台来通信,发送自己的位置给塔台,由塔台来负责每架飞机的航线调度。这样就大大简化了通信网络。

    刚刚举的是生活中的例子,我们再举一个跟编程开发相关的例子。这个例子与UI控件有关,算是中介模式比较经典的应用,很多书籍在讲到中介模式的时候,都会拿它来举例。

    假设我们有一个比较复杂的对话框,对话框中有很多控件,比如按钮、文本框、下拉框等。当我们对某个控件进行操作的时候,其他控件会做出相应的反应,比如,我们在下拉框中选择“注册”,注册相关的控件就会显示在对话框中。如果我们在下拉框中选择“登陆”,登陆相关的控件就会显示在对话框中。

    按照通常我们习惯的UI界面的开发方式,我们将刚刚的需求用代码实现出来,就是下面这个样子。在这种实现方式中,控件和控件之间互相操作、互相依赖。

    1. public class UIControl {
    2. private static final String LOGIN_BTN_ID = "login_btn";
    3. private static final String REG_BTN_ID = "reg_btn";
    4. private static final String USERNAME_INPUT_ID = "username_input";
    5. private static final String PASSWORD_INPUT_ID = "pswd_input";
    6. private static final String REPEATED_PASSWORD_INPUT_ID = "repeated_pswd_input";
    7. private static final String HINT_TEXT_ID = "hint_text";
    8. private static final String SELECTION_ID = "selection";
    9. public static void main(String[] args) {
    10. Button loginButton = (Button)findViewById(LOGIN_BTN_ID);
    11. Button regButton = (Button)findViewById(REG_BTN_ID);
    12. Input usernameInput = (Input)findViewById(USERNAME_INPUT_ID);
    13. Input passwordInput = (Input)findViewById(PASSWORD_INPUT_ID);
    14. Input repeatedPswdInput = (Input)findViewById(REPEATED_PASSWORD_INPUT_ID);
    15. Text hintText = (Text)findViewById(HINT_TEXT_ID);
    16. Selection selection = (Selection)findViewById(SELECTION_ID);
    17. loginButton.setOnClickListener(new OnClickListener() {
    18. @Override
    19. public void onClick(View v) {
    20. String username = usernameInput.text();
    21. String password = passwordInput.text();
    22. //校验数据...
    23. //做业务处理...
    24. }
    25. });
    26. regButton.setOnClickListener(new OnClickListener() {
    27. @Override
    28. public void onClick(View v) {
    29. //获取usernameInput、passwordInput、repeatedPswdInput数据...
    30. //校验数据...
    31. //做业务处理...
    32. }
    33. });
    34. //...省略selection下拉选择框相关代码....
    35. }
    36. }

    我们再按照中介模式,将上面的代码重新实现一下。在新的代码实现中,各个控件只跟中介对象交互,中介对象负责所有业务逻辑的处理。

    1. public interface Mediator {
    2. void handleEvent(Component component, String event);
    3. }
    4. public class LandingPageDialog implements Mediator {
    5. private Button loginButton;
    6. private Button regButton;
    7. private Selection selection;
    8. private Input usernameInput;
    9. private Input passwordInput;
    10. private Input repeatedPswdInput;
    11. private Text hintText;
    12. @Override
    13. public void handleEvent(Component component, String event) {
    14. if (component.equals(loginButton)) {
    15. String username = usernameInput.text();
    16. String password = passwordInput.text();
    17. //校验数据...
    18. //做业务处理...
    19. } else if (component.equals(regButton)) {
    20. //获取usernameInput、passwordInput、repeatedPswdInput数据...
    21. //校验数据...
    22. //做业务处理...
    23. } else if (component.equals(selection)) {
    24. String selectedItem = selection.select();
    25. if (selectedItem.equals("login")) {
    26. usernameInput.show();
    27. passwordInput.show();
    28. repeatedPswdInput.hide();
    29. hintText.hide();
    30. //...省略其他代码
    31. } else if (selectedItem.equals("register")) {
    32. //....
    33. }
    34. }
    35. }
    36. }
    37. public class UIControl {
    38. private static final String LOGIN_BTN_ID = "login_btn";
    39. private static final String REG_BTN_ID = "reg_btn";
    40. private static final String USERNAME_INPUT_ID = "username_input";
    41. private static final String PASSWORD_INPUT_ID = "pswd_input";
    42. private static final String REPEATED_PASSWORD_INPUT_ID = "repeated_pswd_input";
    43. private static final String HINT_TEXT_ID = "hint_text";
    44. private static final String SELECTION_ID = "selection";
    45. public static void main(String[] args) {
    46. Button loginButton = (Button)findViewById(LOGIN_BTN_ID);
    47. Button regButton = (Button)findViewById(REG_BTN_ID);
    48. Input usernameInput = (Input)findViewById(USERNAME_INPUT_ID);
    49. Input passwordInput = (Input)findViewById(PASSWORD_INPUT_ID);
    50. Input repeatedPswdInput = (Input)findViewById(REPEATED_PASSWORD_INPUT_ID);
    51. Text hintText = (Text)findViewById(HINT_TEXT_ID);
    52. Selection selection = (Selection)findViewById(SELECTION_ID);
    53. Mediator dialog = new LandingPageDialog();
    54. dialog.setLoginButton(loginButton);
    55. dialog.setRegButton(regButton);
    56. dialog.setUsernameInput(usernameInput);
    57. dialog.setPasswordInput(passwordInput);
    58. dialog.setRepeatedPswdInput(repeatedPswdInput);
    59. dialog.setHintText(hintText);
    60. dialog.setSelection(selection);
    61. loginButton.setOnClickListener(new OnClickListener() {
    62. @Override
    63. public void onClick(View v) {
    64. dialog.handleEvent(loginButton, "click");
    65. }
    66. });
    67. regButton.setOnClickListener(new OnClickListener() {
    68. @Override
    69. public void onClick(View v) {
    70. dialog.handleEvent(regButton, "click");
    71. }
    72. });
    73. //....
    74. }
    75. }

    从代码中我们可以看出,原本业务逻辑会分散在各个控件中,现在都集中到了中介类中。实际上,这样做既有好处,也有坏处。好处是简化了控件之间的交互,坏处是中介类有可能会变成大而复杂的“上帝类”(God Class)。所以,在使用中介模式的时候,我们要根据实际的情况,平衡对象之间交互的复杂度和中介类本身的复杂度。