设计模式之美学习-行为型-中介模式(三十六)
什么是中介模式
中介模式定义了一个单独的(中介)对象,来封装一组对象之间的交互。将这组对象之间的交互委派给与中介对象交互,来避免对象之间的直接交互。
需求
假设我们有一个比较复杂的对话框,对话框中有很多控件,比如按钮、文本框、下拉框等。当我们对某个控件进行操作的时候,其他控件会做出相应的反应,比如,我们在下拉框中选择“注册”,注册相关的控件就会显示在对话框中。如果我们在下拉框中选择“登陆”,登陆相关的控件就会显示在对话框中
未使用模式
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"); } }); //.... } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!