设计模式之中介者模式
时间:2022-07-22
本文章向大家介绍设计模式之中介者模式,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
设计模式之中介者模式
结构
说明
Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
包装了一系列对象相互作用的方式,使得这些对象不必相互明显作用,从而使它们可以松散偶合。当某些对象之间的作用发生改变时,不会立即影响其他的一些对象之间的作用,保证这些作用可以彼此独立的变化。
适用条件
一组对象以定义良好但是复杂的方式进行通信, 之间产生的相互依赖关系结构混乱且难以理解, 一个对象引用其他很多对象, 并且直接与这些对象通信, 导致难以复用该对象。
实现
public interface IColleague {
void Execute();
}
public interface IMediator {
void Book();
void View();
void Search();
void RegisterView(ViewButton viewButton);
void RegisterSearch(SearchButton searchButton);
void RegisterBook(BookButton bookButton);
void RegisterDisplay(DisplayLabel displayLabel);
}
class Mediator : IMediator {
private ViewButton _viewButton;
private SearchButton _searchButton;
private BookButton _bookButton;
private DisplayLabel _displayLabel;
public void Book() {
this._bookButton.IsEnabled = false;
this._viewButton.IsEnabled = true;
this._searchButton.IsEnabled = true;
this._displayLabel.Content = "Booking ...";
}
public void View() {
this._bookButton.IsEnabled = true;
this._viewButton.IsEnabled = false;
this._searchButton.IsEnabled = true;
this._displayLabel.Content = "Viewing ...";
}
public void Search() {
this._bookButton.IsEnabled = true;
this._viewButton.IsEnabled = true;
this._searchButton.IsEnabled = false;
this._displayLabel.Content = "Searching ...";
}
public void RegisterView(ViewButton viewButton) {
this._viewButton = viewButton;
}
public void RegisterSearch(SearchButton searchButton) {
this._searchButton = searchButton;
}
public void RegisterBook(BookButton bookButton) {
this._bookButton = bookButton;
}
public void RegisterDisplay(DisplayLabel displayLabel) {
this._displayLabel = displayLabel;
}
}
public class BookButton : Button, IColleague {
private readonly IMediator _mediator;
public BookButton(IMediator mediator) {
this.Content = "Book";
this._mediator = mediator;
this._mediator.RegisterBook(this);
}
public void Execute() {
this._mediator.Book();
}
}
public class SearchButton : Button, IColleague {
private readonly IMediator _mediator;
public SearchButton(IMediator mediator) {
this.Content = "Search";
this._mediator = mediator;
this._mediator.RegisterSearch(this);
}
public void Execute() {
this._mediator.Search();
}
}
public class ViewButton : Button, IColleague {
private readonly IMediator _mediator;
public ViewButton(IMediator mediator) {
this.Content = "View";
this._mediator = mediator;
this._mediator.RegisterView(this);
}
public void Execute() {
this._mediator.View();
}
}
public class DisplayLabel : Label {
private readonly IMediator _mediator;
public DisplayLabel(IMediator mediator) {
this.Content = "Starting ...";
this.FontSize = 24;
//this.FontWeight = new FontWeight();
this._mediator = mediator;
this._mediator.RegisterDisplay(this);
}
}
class MediatorDemo : Window {
private readonly IMediator _mediator = new Mediator();
public MediatorDemo() {
var rootView = new Grid();
rootView.RowDefinitions.Add(new RowDefinition());
rootView.RowDefinitions.Add(new RowDefinition());
rootView.ColumnDefinitions.Add(new ColumnDefinition());
rootView.ColumnDefinitions.Add(new ColumnDefinition());
rootView.ColumnDefinitions.Add(new ColumnDefinition());
var viewButton = new ViewButton(this._mediator);
viewButton.Click += this.OnViewClick;
Grid.SetColumn(viewButton, 0);
Grid.SetRow(viewButton, 0);
rootView.Children.Add(viewButton);
var bookButton = new BookButton(this._mediator);
bookButton.Click += this.OnViewClick;
Grid.SetColumn(bookButton, 1);
Grid.SetRow(bookButton, 0);
rootView.Children.Add(bookButton);
var searchButton = new SearchButton(this._mediator);
searchButton.Click += this.OnViewClick;
Grid.SetColumn(searchButton, 2);
Grid.SetRow(searchButton, 0);
rootView.Children.Add(searchButton);
var displayLabel = new DisplayLabel(this._mediator);
Grid.SetColumn(displayLabel, 0);
Grid.SetRow(displayLabel, 1);
Grid.SetColumnSpan(displayLabel, 3);
rootView.Children.Add(displayLabel);
this.Content = rootView;
this.Width = 400;
this.Height = 200;
this.Title = "Mediator Demo";
}
private void OnViewClick(object sender, RoutedEventArgs routedEventArgs) {
var colleague = (IColleague)sender;
colleague.Execute();
}
[STAThread]
static void Main(string[] args) {
var app = new Application();
app.Startup += AppOnStartup;
app.Run();
}
private static void AppOnStartup(object sender, StartupEventArgs startupEventArgs) {
Application.Current.MainWindow = new MediatorDemo();
Application.Current.MainWindow.Show();
}
}
中介者模式也是最为常见的设计模式之一, 不管是 WinForm, WebForm 还是 WPF , 均可以考虑采用中介者模式。
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- R语言几行代码拼接pdf文件
- 一次 Redis 分布式锁事故,整个项目组被扣绩效了。。。
- 小程序访问https显示网络错误,微信打开https空白、浏览器访问正常 解决方案
- Python GUI项目实战(七)学生信息的修改、删除和保存
- Python | 使用argparse解析命令行参数
- LeetCode 98 | 判断二叉搜索树是否合法
- LeetCode 96,n个数构建BST的方法有多少种?
- Go语言 | goroutine不只有基础的用法,还有这些你不知道的操作
- 每日一题 | 二进制操作问题
- Pandas | Dataframe的merge操作,像数据库一样尽情join
- 每日一题 | 灾后重建问题
- 树形结构已知子节点找父节点
- 解析 hashMap 源码之基本操作 get
- RCE(远程命令/代码执行漏洞)原理及复现
- 我的Vue不小心跨域了o(╥﹏╥)o 干它