Skip to content

Commit 16c3d90

Browse files
committed
add mediator
1 parent 5dc8ad1 commit 16c3d90

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

08_mediator/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# 中介者模式
2+
3+
中介者模式封装对象之间互交,使依赖变的简单,并且使复杂互交简单化,封装在中介者中。
4+
5+
例子中的中介者使用单例模式生成中介者。
6+
7+
中介者的change使用switch判断类型。

08_mediator/mediator.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package mediator
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
type CDDriver struct {
9+
Data string
10+
}
11+
12+
func (c *CDDriver) ReadData() {
13+
c.Data = "music,image"
14+
15+
fmt.Printf("CDDriver: reading data %s\n", c.Data)
16+
GetMediatorInstance().changed(c)
17+
}
18+
19+
type CPU struct {
20+
Video string
21+
Sound string
22+
}
23+
24+
func (c *CPU) Process(data string) {
25+
sp := strings.Split(data, ",")
26+
c.Sound = sp[0]
27+
c.Video = sp[1]
28+
29+
fmt.Printf("CPU: split data with Sound %s, Video %s\n", c.Sound, c.Video)
30+
GetMediatorInstance().changed(c)
31+
}
32+
33+
type VideoCard struct {
34+
Data string
35+
}
36+
37+
func (v *VideoCard) Display(data string) {
38+
v.Data = data
39+
fmt.Printf("VideoCard: display %s\n", v.Data)
40+
GetMediatorInstance().changed(v)
41+
}
42+
43+
type SoundCard struct {
44+
Data string
45+
}
46+
47+
func (s *SoundCard) Play(data string) {
48+
s.Data = data
49+
fmt.Printf("SoundCard: play %s\n", s.Data)
50+
GetMediatorInstance().changed(s)
51+
}
52+
53+
type Mediator struct {
54+
CD *CDDriver
55+
CPU *CPU
56+
Video *VideoCard
57+
Sound *SoundCard
58+
}
59+
60+
var mediator *Mediator
61+
62+
func GetMediatorInstance() *Mediator {
63+
if mediator == nil {
64+
mediator = &Mediator{}
65+
}
66+
return mediator
67+
}
68+
69+
func (m *Mediator) changed(i interface{}) {
70+
switch inst := i.(type) {
71+
case *CDDriver:
72+
m.CPU.Process(inst.Data)
73+
case *CPU:
74+
m.Sound.Play(inst.Sound)
75+
m.Video.Display(inst.Video)
76+
}
77+
}

08_mediator/mediator_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package mediator
2+
3+
import "testing"
4+
5+
func TestMediator(t *testing.T) {
6+
mediator := GetMediatorInstance()
7+
mediator.CD = &CDDriver{}
8+
mediator.CPU = &CPU{}
9+
mediator.Video = &VideoCard{}
10+
mediator.Sound = &SoundCard{}
11+
12+
//Tiggle
13+
mediator.CD.ReadData()
14+
15+
if mediator.CD.Data != "music,image" {
16+
t.Fatalf("CD unexpect data %s", mediator.CD.Data)
17+
}
18+
19+
if mediator.CPU.Sound != "music" {
20+
t.Fatalf("CPU unexpect sound data %s", mediator.CPU.Sound)
21+
}
22+
23+
if mediator.CPU.Video != "image" {
24+
t.Fatalf("CPU unexpect video data %s", mediator.CPU.Video)
25+
}
26+
27+
if mediator.Video.Data != "image" {
28+
t.Fatalf("VidoeCard unexpect data %s", mediator.Video.Data)
29+
}
30+
31+
if mediator.Sound.Data != "music" {
32+
t.Fatalf("SoundCard unexpect data %s", mediator.Sound.Data)
33+
}
34+
}

0 commit comments

Comments
 (0)