mirror of
https://github.com/Estom/notes.git
synced 2026-02-12 23:05:38 +08:00
go知识重新整理
This commit is contained in:
86
Go/DesignPattern/mediator/mediator.go
Normal file
86
Go/DesignPattern/mediator/mediator.go
Normal file
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* @Author:zhoutao
|
||||
* @Date:2020/12/12 上午9:52
|
||||
* @Desc:
|
||||
*/
|
||||
|
||||
package mediator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//中介模式:封装对象之间的交互,使依赖变得简单,并且是复杂交互简单化
|
||||
//例子中的中介者使用单例模式生成
|
||||
|
||||
type CDDriver struct {
|
||||
Data string
|
||||
}
|
||||
|
||||
func (c *CDDriver) ReadData() {
|
||||
c.Data = "music,image"
|
||||
fmt.Printf("CDDriver:reading data %s\n", c.Data)
|
||||
|
||||
}
|
||||
|
||||
type CPU struct {
|
||||
Video string
|
||||
Sound string
|
||||
}
|
||||
|
||||
func (c *CPU) Process(data string) {
|
||||
sp := strings.Split(data, ",")
|
||||
c.Sound = sp[0]
|
||||
c.Video = sp[1]
|
||||
|
||||
fmt.Printf("CPU:split data with Sound %s,Video %s\n", c.Sound, c.Video)
|
||||
GetMediatorInstance().changed(c)
|
||||
}
|
||||
|
||||
type VideoCard struct {
|
||||
Data string
|
||||
}
|
||||
|
||||
func (v *VideoCard) Display(data string) {
|
||||
v.Data = data
|
||||
fmt.Printf("VideoCard:display %s\n", v.Data)
|
||||
GetMediatorInstance().changed(v)
|
||||
}
|
||||
|
||||
type SoundCard struct {
|
||||
Data string
|
||||
}
|
||||
|
||||
func (s *SoundCard) Play(data string) {
|
||||
s.Data = data
|
||||
fmt.Printf("SoundCard:play %s\n", s.Data)
|
||||
GetMediatorInstance().changed(s)
|
||||
}
|
||||
|
||||
//封装Mediator对象
|
||||
type Mediator struct {
|
||||
CD *CDDriver
|
||||
CPU *CPU
|
||||
Video *VideoCard
|
||||
Sound *SoundCard
|
||||
}
|
||||
|
||||
var mediator *Mediator
|
||||
|
||||
func GetMediatorInstance() *Mediator {
|
||||
if mediator == nil {
|
||||
mediator = &Mediator{}
|
||||
}
|
||||
return mediator
|
||||
}
|
||||
|
||||
func (m *Mediator) changed(i interface{}) {
|
||||
switch inst := i.(type) {
|
||||
case *CDDriver:
|
||||
m.CPU.Process(inst.Data)
|
||||
case *CPU:
|
||||
m.Sound.Play(inst.Sound)
|
||||
m.Video.Display(inst.Video)
|
||||
}
|
||||
}
|
||||
37
Go/DesignPattern/mediator/mediator_test.go
Normal file
37
Go/DesignPattern/mediator/mediator_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* @Author:zhoutao
|
||||
* @Date:2020/12/12 上午10:16
|
||||
* @Desc:
|
||||
*/
|
||||
|
||||
package mediator
|
||||
|
||||
import "testing"
|
||||
|
||||
//中介模式
|
||||
func TestMediator(t *testing.T) {
|
||||
mediator := GetMediatorInstance()
|
||||
mediator.CD = &CDDriver{}
|
||||
mediator.Video = &VideoCard{}
|
||||
mediator.Sound = &SoundCard{}
|
||||
mediator.CPU = &CPU{}
|
||||
|
||||
//run
|
||||
mediator.CD.ReadData()
|
||||
|
||||
if mediator.CD.Data != "music,image" {
|
||||
t.Fatalf("CD unexpect data %s", mediator.CD.Data)
|
||||
}
|
||||
if mediator.CPU.Sound != "music" {
|
||||
t.Fatalf("CPU unexpect data %s", mediator.CPU.Sound)
|
||||
}
|
||||
if mediator.CPU.Video != "image" {
|
||||
t.Fatalf("CPU unexpect data data %s", mediator.CPU.Video)
|
||||
}
|
||||
if mediator.Video.Data != "image" {
|
||||
t.Fatalf("VidelCard unexpect data %s", mediator.Video.Data)
|
||||
}
|
||||
if mediator.Sound.Data != "music" {
|
||||
t.Fatalf("SoundCard unexpect data %s", mediator.Sound.Data)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user