File tree Expand file tree Collapse file tree 19 files changed +858
-0
lines changed
design-patterns/src/main/java/pers/huangyuhui/mediator_pattern Expand file tree Collapse file tree 19 files changed +858
-0
lines changed Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments