Skip to content

Commit c4c7d7f

Browse files
committed
cmd, mediator pattern
1 parent 1df3464 commit c4c7d7f

File tree

5 files changed

+97
-2
lines changed

5 files changed

+97
-2
lines changed

08_mediator/README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# 中介者模式
22

3+
行为型 模式
4+
35
加入中介,改变结构,提高动态服务的能力。
46

7+
https://time.geekbang.org/column/article/226710
8+
59
## intro
610

711
在现实生活中,常常会出现好多对象之间存在复杂的交互关系,这种交互关系常常是“网状结构”,
@@ -33,10 +37,13 @@
3337

3438
## more
3539

36-
http://c.biancheng.net/view/1393.html
37-
3840
中介者模式封装对象之间互交,使依赖变的简单,并且使复杂互交简单化,封装在中介者中。
3941

4042
例子中的中介者使用单例模式生成中介者。
4143

4244
中介者的change使用switch判断类型。
45+
46+
从代码中我们可以看出,原本业务逻辑会分散在各个控件中,现在都集中到了中介类中。实际上,这样做既有好处,也有坏处。好处是简化了控件之间的交互,坏处是中介类有可能会变成大而复杂的“上帝类”(God Class)。所以,在使用中介模式的时候,我们要根据实际的情况,平衡对象之间交互的复杂度和中介类本身的复杂度。
47+
48+
而中介模式正好相反。只有当参与者之间的交互关系错综复杂,维护成本很高的时候,我们才考虑使用中介模式
49+

08_mediator/mediator.go

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func (c *CDDriver) ReadData() {
1414
c.Data = "music,image"
1515

1616
fmt.Printf("CDDriver: reading data %s\n", c.Data)
17+
1718
// mediator
1819
GetMediatorInstance().changed(c)
1920
}

11_command/alternative/another.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}
+25
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
11
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+
}

11_command/command.go

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type Command interface {
66
Execute()
77
}
88

9+
// StartCommand contains mother board
910
type StartCommand struct {
1011
mb *MotherBoard
1112
}
@@ -20,6 +21,7 @@ func (c *StartCommand) Execute() {
2021
c.mb.Start()
2122
}
2223

24+
// RebootCommand
2325
type RebootCommand struct {
2426
mb *MotherBoard
2527
}
@@ -34,6 +36,7 @@ func (c *RebootCommand) Execute() {
3436
c.mb.Reboot()
3537
}
3638

39+
// MotherBoard
3740
type MotherBoard struct{}
3841

3942
func (*MotherBoard) Start() {
@@ -44,6 +47,7 @@ func (*MotherBoard) Reboot() {
4447
fmt.Print("system rebooting\n")
4548
}
4649

50+
// Box is the invoker
4751
type Box struct {
4852
button1 Command
4953
button2 Command

0 commit comments

Comments
 (0)