File tree 5 files changed +97
-2
lines changed
5 files changed +97
-2
lines changed Original file line number Diff line number Diff line change 1
1
# 中介者模式
2
2
3
+ 行为型 模式
4
+
3
5
加入中介,改变结构,提高动态服务的能力。
4
6
7
+ https://time.geekbang.org/column/article/226710
8
+
5
9
## intro
6
10
7
11
在现实生活中,常常会出现好多对象之间存在复杂的交互关系,这种交互关系常常是“网状结构”,
33
37
34
38
## more
35
39
36
- http://c.biancheng.net/view/1393.html
37
-
38
40
中介者模式封装对象之间互交,使依赖变的简单,并且使复杂互交简单化,封装在中介者中。
39
41
40
42
例子中的中介者使用单例模式生成中介者。
41
43
42
44
中介者的change使用switch判断类型。
45
+
46
+ 从代码中我们可以看出,原本业务逻辑会分散在各个控件中,现在都集中到了中介类中。实际上,这样做既有好处,也有坏处。好处是简化了控件之间的交互,坏处是中介类有可能会变成大而复杂的“上帝类”(God Class)。所以,在使用中介模式的时候,我们要根据实际的情况,平衡对象之间交互的复杂度和中介类本身的复杂度。
47
+
48
+ 而中介模式正好相反。只有当参与者之间的交互关系错综复杂,维护成本很高的时候,我们才考虑使用中介模式
49
+
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ func (c *CDDriver) ReadData() {
14
14
c .Data = "music,image"
15
15
16
16
fmt .Printf ("CDDriver: reading data %s\n " , c .Data )
17
+
17
18
// mediator
18
19
GetMediatorInstance ().changed (c )
19
20
}
Original file line number Diff line number Diff line change
1
+ package alternative
2
+
3
+ import "fmt"
4
+
5
+ // https://refactoring.guru/design-patterns/command/go/example#example-0
6
+
7
+ // [1] Invoker
8
+ type Button struct {
9
+ command Command
10
+ }
11
+
12
+ func (b * Button ) press () {
13
+ b .command .execute ()
14
+ }
15
+
16
+ // intf
17
+ type Command interface {
18
+ execute ()
19
+ }
20
+
21
+ // OnCommand - concrete cmd
22
+ type OnCommand struct {
23
+ device Device
24
+ }
25
+
26
+ func (c * OnCommand ) execute () {
27
+ c .device .on ()
28
+ }
29
+
30
+ // OffCommand - concrete cmd
31
+ type OffCommand struct {
32
+ device Device
33
+ }
34
+
35
+ func (c * OffCommand ) execute () {
36
+ c .device .off ()
37
+ }
38
+
39
+ // [2] Receiver intf
40
+ type Device interface {
41
+ on ()
42
+ off ()
43
+ }
44
+
45
+ // Concrete receiver
46
+ type Tv struct {
47
+ isRunning bool
48
+ }
49
+
50
+ func (t * Tv ) on () {
51
+ t .isRunning = true
52
+ fmt .Println ("Turning tv on" )
53
+ }
54
+
55
+ func (t * Tv ) off () {
56
+ t .isRunning = false
57
+ fmt .Println ("Turning tv off" )
58
+ }
Original file line number Diff line number Diff line change 1
1
package alternative
2
+
3
+ import "testing"
4
+
5
+ func TestAlternativeCmdPattern (t * testing.T ) {
6
+ tv := & Tv {}
7
+
8
+ // command includes device
9
+ onCommand := & OnCommand {
10
+ device : tv ,
11
+ }
12
+
13
+ offCommand := & OffCommand {
14
+ device : tv ,
15
+ }
16
+
17
+ onButton := & Button {
18
+ command : onCommand ,
19
+ }
20
+ onButton .press ()
21
+
22
+ offButton := & Button {
23
+ command : offCommand ,
24
+ }
25
+ offButton .press ()
26
+ }
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ type Command interface {
6
6
Execute ()
7
7
}
8
8
9
+ // StartCommand contains mother board
9
10
type StartCommand struct {
10
11
mb * MotherBoard
11
12
}
@@ -20,6 +21,7 @@ func (c *StartCommand) Execute() {
20
21
c .mb .Start ()
21
22
}
22
23
24
+ // RebootCommand
23
25
type RebootCommand struct {
24
26
mb * MotherBoard
25
27
}
@@ -34,6 +36,7 @@ func (c *RebootCommand) Execute() {
34
36
c .mb .Reboot ()
35
37
}
36
38
39
+ // MotherBoard
37
40
type MotherBoard struct {}
38
41
39
42
func (* MotherBoard ) Start () {
@@ -44,6 +47,7 @@ func (*MotherBoard) Reboot() {
44
47
fmt .Print ("system rebooting\n " )
45
48
}
46
49
50
+ // Box is the invoker
47
51
type Box struct {
48
52
button1 Command
49
53
button2 Command
You can’t perform that action at this time.
0 commit comments