设计模式之美学习-行为型-中介模式(三十六)
什么是中介模式
中介模式定义了一个单独的(中介)对象,来封装一组对象之间的交互。将这组对象之间的交互委派给与中介对象交互,来避免对象之间的直接交互。
需求
假设我们有一个比较复杂的对话框,对话框中有很多控件,比如按钮、文本框、下拉框等。当我们对某个控件进行操作的时候,其他控件会做出相应的反应,比如,我们在下拉框中选择“注册”,注册相关的控件就会显示在对话框中。如果我们在下拉框中选择“登陆”,登陆相关的控件就会显示在对话框中
未使用模式
public class UIControl { private static final String LOGIN_BTN_ID = "login_btn"; private static final String REG_BTN_ID = "reg_btn"; private static final String USERNAME_INPUT_ID = "username_input"; private static final String PASSWORD_INPUT_ID = "pswd_input"; private static final String REPEATED_PASSWORD_INPUT_ID = "repeated_pswd_input"; private static final String HINT_TEXT_ID = "hint_text"; private static final String SELECTION_ID = "selection"; public static void main(String[] args) { Button loginButton = (Button)findViewById(LOGIN_BTN_ID); Button regButton = (Button)findViewById(REG_BTN_ID); Input usernameInput = (Input)findViewById(USERNAME_INPUT_ID); Input passwordInput = (Input)findViewById(PASSWORD_INPUT_ID); Input repeatedPswdInput = (Input)findViewById(REPEATED_PASSWORD_INPUT_ID); Text hintText = (Text)findViewById(HINT_TEXT_ID); Selection selection = (Selection)findViewById(SELECTION_ID); //登录按钮点击 loginButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String username = usernameInput.text(); String password = passwordInput.text(); //校验数据... //做业务处理... } }); //注册按钮点击 regButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //获取usernameInput、passwordInput、repeatedPswdInput数据... //校验数据... //做业务处理... } }); //...省略selection下拉选择框相关代码.... } }
使用中介者模式实现
//抽象的中介者 public interface Mediator { void handleEvent(Component component, String event); } //具体中介者 public class LandingPageDialog implements Mediator { private Button loginButton; private Button regButton; private Selection selection; private Input usernameInput; private Input passwordInput; private Input repeatedPswdInput; private Text hintText; @Override public void handleEvent(Component component, String event) { //点击了login if (component.equals(loginButton)) { String username = usernameInput.text(); String password = passwordInput.text(); //校验数据... //做业务处理... //点击了regButton } else if (component.equals(regButton)) { //获取usernameInput、passwordInput、repeatedPswdInput数据... //校验数据... //做业务处理... //点解了选择则 } else if (component.equals(selection)) { String selectedItem = selection.select(); if (selectedItem.equals("login")) { usernameInput.show(); passwordInput.show(); repeatedPswdInput.hide(); hintText.hide(); //...省略其他代码 } else if (selectedItem.equals("register")) { //.... } } } } public class UIControl { private static final String LOGIN_BTN_ID = "login_btn"; private static final String REG_BTN_ID = "reg_btn"; private static final String USERNAME_INPUT_ID = "username_input"; private static final String PASSWORD_INPUT_ID = "pswd_input"; private static final String REPEATED_PASSWORD_INPUT_ID = "repeated_pswd_input"; private static final String HINT_TEXT_ID = "hint_text"; private static final String SELECTION_ID = "selection"; public static void main(String[] args) { Button loginButton = (Button)findViewById(LOGIN_BTN_ID); Button regButton = (Button)findViewById(REG_BTN_ID); Input usernameInput = (Input)findViewById(USERNAME_INPUT_ID); Input passwordInput = (Input)findViewById(PASSWORD_INPUT_ID); Input repeatedPswdInput = (Input)findViewById(REPEATED_PASSWORD_INPUT_ID); Text hintText = (Text)findViewById(HINT_TEXT_ID); Selection selection = (Selection)findViewById(SELECTION_ID); //将按钮信息交给中介者 Mediator dialog = new LandingPageDialog(); dialog.setLoginButton(loginButton); dialog.setRegButton(regButton); dialog.setUsernameInput(usernameInput); dialog.setPasswordInput(passwordInput); dialog.setRepeatedPswdInput(repeatedPswdInput); dialog.setHintText(hintText); dialog.setSelection(selection); loginButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //交个中介者处理 dialog.handleEvent(loginButton, "click"); } }); regButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //交个中介者处理 dialog.handleEvent(regButton, "click"); } }); //.... } }