Skip to content

Commit 8aabdca

Browse files
committed
🎉 mediator pattern
1 parent 2e5a719 commit 8aabdca

File tree

19 files changed

+858
-0
lines changed

19 files changed

+858
-0
lines changed

design-patterns/src/main/java/pers/huangyuhui/mediator_pattern/README.md

Lines changed: 408 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package pers.huangyuhui.mediator_pattern.mediator_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 按钮类, 充当具体同事类
6+
* @author: 黄宇辉
7+
* @date: 10/10/2019-12:51 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class Button extends Component {
12+
public void update() {
13+
//System.out.println("点击添加信息按钮");
14+
}
15+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package pers.huangyuhui.mediator_pattern.mediator_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 客户端测试类
6+
* @author: 黄宇辉
7+
* @date: 10/10/2019-12:58 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class Client {
12+
13+
public static void main(String[] args) {
14+
//定义中介者对象
15+
ConcreteMediator mediator = new ConcreteMediator();
16+
17+
//定义同事对象
18+
List list = new List();
19+
Button addButton = new Button();
20+
ComboBox comboBox = new ComboBox();
21+
TextBox userNameTextBox = new TextBox();
22+
23+
//关联中介者
24+
list.setMediator(mediator);
25+
comboBox.setMediator(mediator);
26+
addButton.setMediator(mediator);
27+
userNameTextBox.setMediator(mediator);
28+
29+
//初始化具体同事类
30+
mediator.list = list;
31+
mediator.comboBox = comboBox;
32+
mediator.addButton = addButton;
33+
mediator.userNameTextBox = userNameTextBox;
34+
35+
//通过中介者进行通信
36+
addButton.changed(); //点击添加客户信息按钮
37+
list.changed(); //从列表框中选择客户
38+
comboBox.changed(); //从组合框中选择客户
39+
}
40+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package pers.huangyuhui.mediator_pattern.mediator_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 组合框类, 充当具体同事类
6+
* @author: 黄宇辉
7+
* @date: 10/10/2019-12:55 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class ComboBox extends Component {
12+
13+
public void update() {
14+
System.out.println("组合框增加一项: 黄宇辉");
15+
}
16+
17+
public void select() {
18+
System.out.println("组合框选中项: 黄浩然");
19+
}
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package pers.huangyuhui.mediator_pattern.mediator_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 抽象组件类, 充当抽象同事类
6+
* @author: 黄宇辉
7+
* @date: 10/10/2019-12:51 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public abstract class Component {
12+
13+
protected Mediator mediator;
14+
15+
public void setMediator(Mediator mediator) {
16+
this.mediator = mediator;
17+
}
18+
19+
//转发调用
20+
public void changed() {
21+
mediator.componentChanged(this);
22+
}
23+
24+
public abstract void update();
25+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package pers.huangyuhui.mediator_pattern.mediator_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 具体中介者类
6+
* @author: 黄宇辉
7+
* @date: 10/10/2019-12:50 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class ConcreteMediator extends Mediator {
12+
13+
//维持对各个同事对象的引用
14+
public List list;
15+
public Button addButton;
16+
public TextBox userNameTextBox;
17+
public ComboBox comboBox;
18+
19+
//封装同事对象之间的交互
20+
@Override
21+
public void componentChanged(Component component) {
22+
//单击按钮
23+
if (component == addButton) {
24+
System.out.println("--- 点击增加信息按钮 ---");
25+
list.update();
26+
comboBox.update();
27+
userNameTextBox.update();
28+
}
29+
//从列表框选择客户
30+
else if (component == list) {
31+
System.out.println("\n--- 从列表框选择客户 ---");
32+
list.select();
33+
userNameTextBox.setText();
34+
}
35+
//从组合框选择客户
36+
else if (component == comboBox) {
37+
System.out.println("\n--- 从组合框选择客户 ---");
38+
comboBox.select();
39+
userNameTextBox.setText();
40+
}
41+
}
42+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package pers.huangyuhui.mediator_pattern.mediator_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 列表框类, 充当具体同事类
6+
* @author: 黄宇辉
7+
* @date: 10/10/2019-12:52 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class List extends Component {
12+
13+
public void update() {
14+
System.out.println("列表框增加一项: 黄宇辉");
15+
}
16+
17+
public void select() {
18+
System.out.println("列表框选中项: 黄浩然");
19+
}
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package pers.huangyuhui.mediator_pattern.mediator_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 抽象中介者类
6+
* @author: 黄宇辉
7+
* @date: 10/10/2019-12:49 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public abstract class Mediator {
12+
public abstract void componentChanged(Component component);
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package pers.huangyuhui.mediator_pattern.mediator_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 文本框, 充当具体同事类
6+
* @author: 黄宇辉
7+
* @date: 10/10/2019-12:56 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class TextBox extends Component {
12+
13+
public void update() {
14+
System.out.println("客户信息增加成功后文本框被清空");
15+
}
16+
17+
public void setText() {
18+
System.out.println("文本框显示: 黄浩然");
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package pers.huangyuhui.mediator_pattern.mediator_pattern_2;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 按钮类, 充当具体同事类
6+
* @author: 黄宇辉
7+
* @date: 10/10/2019-12:51 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class Button extends Component {
12+
public void update() {
13+
//System.out.println("点击增加信息按钮");
14+
}
15+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy