mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-22 11:56:03 +03:00
update mediator pattern
This commit is contained in:
parent
20cee1421f
commit
8886ba434d
@ -18,52 +18,52 @@ type Person struct {
|
|||||||
//Tenant 租客
|
//Tenant 租客
|
||||||
type Tenant struct {
|
type Tenant struct {
|
||||||
Person
|
Person
|
||||||
|
m *mediator
|
||||||
}
|
}
|
||||||
|
|
||||||
//Landlord 房东,要收房租
|
//Landlord 房东,要收房租
|
||||||
type Landlord struct {
|
type Landlord struct {
|
||||||
Person
|
Person
|
||||||
accout int //房东的租金账户
|
accout int //房东的租金账户
|
||||||
|
m *mediator
|
||||||
}
|
}
|
||||||
|
|
||||||
//mediator 中介,比如某居客,某家,某壳,即代表租客跟房东谈条件,又代表房东对付租客
|
//Mediator 中介,比如某居客,某家,某壳,即代表租客跟房东谈条件,又代表房东对付租客
|
||||||
//mediator 所以中介一定会持有两方的信息,最好用接口代表对象
|
//Mediator 所以中介一定会持有两方的信息,最好用接口代表对象
|
||||||
//mediator 这里简化一下,直接用类型的引用,表示拥有关系
|
//Mediator 这里简化一下,假设中介只为一个房东和一个租客服务,直接用类型的引用,表示拥有关系
|
||||||
type mediator struct {
|
type Mediator struct {
|
||||||
tenant *Tenant
|
tenant interface{}
|
||||||
landlord *Landlord
|
landlord interface{}
|
||||||
feeLandlord int //向房东收费的账户
|
feelandlord int
|
||||||
feeTenant int //向租客收费的账户
|
feelandtenant int
|
||||||
}
|
}
|
||||||
|
|
||||||
//AskRepair 要求房东修家具
|
//AskRepair 要求房东修家具
|
||||||
func (c *tenant) AskRepair(furniture string) {
|
func (t *Tenant) AskRepair(furniture string) {
|
||||||
fmt.Printf("CDDriver: reading data %s\n", c.Data)
|
fmt.Println("i need landlord fix the:", furniture)
|
||||||
GetMediatorInstance().changed(c)
|
t.m.Changed()
|
||||||
}
|
}
|
||||||
|
|
||||||
//CollectRent 房东收租金了
|
//CollectRent 房东收租金了
|
||||||
func (l *landlord) CollectRent(moneyCount int) {
|
func (l *Landlord) CollectRent(moneyCount int) {
|
||||||
|
|
||||||
fmt.Printf("CPU: split data with Sound %s, Video %s\n", c.Sound, c.Video)
|
fmt.Printf("CPU: split data with Sound %s, Video %s\n", c.Sound, c.Video)
|
||||||
GetMediatorInstance().changed(c)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var mediator *Mediator
|
//Changed 中介要提两边或者多边办事,所以它
|
||||||
|
func (m *Mediator) Changed(i interface{}) {
|
||||||
|
|
||||||
func GetMediatorInstance() *Mediator {
|
|
||||||
if mediator == nil {
|
|
||||||
mediator = &Mediator{}
|
|
||||||
}
|
|
||||||
return mediator
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mediator) changed(i interface{}) {
|
//PublishRoom 可以在中介这里发布房源
|
||||||
switch inst := i.(type) {
|
func (m *Mediator) PublishRoom(landlord interface{}) {
|
||||||
case *CDDriver:
|
|
||||||
m.CPU.Process(inst.Data)
|
m.landlord = landlord
|
||||||
case *CPU:
|
|
||||||
m.Sound.Play(inst.Sound)
|
}
|
||||||
m.Video.Display(inst.Video)
|
|
||||||
}
|
//RentRom 可以从中介租房子
|
||||||
|
func (m *Mediator) RentRom(tenant interface{}) {
|
||||||
|
|
||||||
|
m.tenant = tenant
|
||||||
}
|
}
|
||||||
|
@ -3,32 +3,13 @@ package mediator
|
|||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestMediator(t *testing.T) {
|
func TestMediator(t *testing.T) {
|
||||||
mediator := GetMediatorInstance()
|
|
||||||
mediator.CD = &CDDriver{}
|
|
||||||
mediator.CPU = &CPU{}
|
|
||||||
mediator.Video = &VideoCard{}
|
|
||||||
mediator.Sound = &SoundCard{}
|
|
||||||
|
|
||||||
//Tiggle
|
med := &mediator{}
|
||||||
mediator.CD.ReadData()
|
|
||||||
|
|
||||||
if mediator.CD.Data != "music,image" {
|
landlord := &Landlord{}
|
||||||
t.Fatalf("CD unexpect data %s", mediator.CD.Data)
|
|
||||||
}
|
|
||||||
|
|
||||||
if mediator.CPU.Sound != "music" {
|
med.publishRoom(landlord)
|
||||||
t.Fatalf("CPU unexpect sound data %s", mediator.CPU.Sound)
|
|
||||||
}
|
|
||||||
|
|
||||||
if mediator.CPU.Video != "image" {
|
tenant := &Tenant{}
|
||||||
t.Fatalf("CPU unexpect video data %s", mediator.CPU.Video)
|
|
||||||
}
|
|
||||||
|
|
||||||
if mediator.Video.Data != "image" {
|
|
||||||
t.Fatalf("VidoeCard unexpect data %s", mediator.Video.Data)
|
|
||||||
}
|
|
||||||
|
|
||||||
if mediator.Sound.Data != "music" {
|
|
||||||
t.Fatalf("SoundCard unexpect data %s", mediator.Sound.Data)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user