finish mediator pattern

This commit is contained in:
Edward 2020-04-28 11:10:03 +08:00
parent 8886ba434d
commit c4c60eea07
2 changed files with 85 additions and 37 deletions

View File

@ -11,59 +11,86 @@ import (
//Person 定义一个本身的人 //Person 定义一个本身的人
type Person struct { type Person struct {
Name string Name string
MoneyCount int //每个人都有钱包 WalletAssets int //每个人都有钱包
} }
//Tenant 租客 //Tenant 租客 继承Person
type Tenant struct { type Tenant struct {
Person Person
m *mediator furniture string
} }
//Landlord 房东,要收房租 //ITenant 租户能做的事情
type ITenant interface {
AskRepair(mediator IMediator)
}
//Landlord 房东也继承Person要收房租
type Landlord struct { type Landlord struct {
Person Person
accout int //房东的租金账户 RentAccout int //房东的租金账户
m *mediator
} }
//Mediator 中介,比如某居客,某家,某壳,即代表租客跟房东谈条件,又代表房东对付租客 //ILandlord 房东能做的事情
//Mediator 所以中介一定会持有两方的信息,最好用接口代表对象 type ILandlord interface {
//Mediator 这里简化一下,假设中介只为一个房东和一个租客服务,直接用类型的引用,表示拥有关系 CollectRent(mediator IMediator)
}
//Mediator 中介也继承Person比如某居客某家某壳即代表租客跟房东谈条件又代表房东对付租客
//Mediator 中介一定会持有两方的必要信息
//Mediator 这里简化一下,假设中介只为一个房东和一个租客服务
type Mediator struct { type Mediator struct {
tenant interface{} Person
landlord interface{} tenant ITenant //中介持有房客的信息
feelandlord int landlord ILandlord //中介持有房东的信息
feelandtenant int feelandlord int
feetenant int
} }
//AskRepair 要求房东修家具 //IMediator 中介能做的事情,中介能代表任何一方,
func (t *Tenant) AskRepair(furniture string) { //所以理论上他需要实现所代表对象的所有能力
fmt.Println("i need landlord fix the:", furniture) //实际设计中,中介对象本身也会成为问题的所在,可能会比较臃肿
t.m.Changed() type IMediator interface {
RegisterRoom(landlord ILandlord)
Serve(client interface{}) //服务日常活动
RentOutRoom(tenant ITenant)
} }
//CollectRent 房东收租金了 //AskRepair 要求房东修家具,只需要向中介提要求,中介会提代替房客提要求
func (l *Landlord) CollectRent(moneyCount int) { func (t *Tenant) AskRepair(mediator IMediator) {
fmt.Println("Tenant: i need landlord fix furniture:")
fmt.Printf("CPU: split data with Sound %s, Video %s\n", c.Sound, c.Video) mediator.Serve(t)
} }
//Changed 中介要提两边或者多边办事,所以它 //CollectRent 房东收租金,只需要向中介收,中介会提代替房东收租金
func (m *Mediator) Changed(i interface{}) { func (l *Landlord) CollectRent(mediator IMediator) {
fmt.Println("Landlord: collect money")
fmt.Printf("Landlord: RentAccout %d, WalletAssets %d\n", l.RentAccout, l.WalletAssets)
mediator.Serve(l)
} }
//PublishRoom 可以在中介这里发布房源 //RegisterRoom 可以在中介这里发布房源
func (m *Mediator) PublishRoom(landlord interface{}) { func (m *Mediator) RegisterRoom(landlord ILandlord) {
m.landlord = landlord m.landlord = landlord
} }
//RentRom 可以从中介租房子 //RentOutRoom 可以从中介租房子
func (m *Mediator) RentRom(tenant interface{}) { func (m *Mediator) RentOutRoom(tenant ITenant) {
m.tenant = tenant m.tenant = tenant
} }
//Serve 中介要替两边或者多边办事,所以它很累,所有事情都要做
//这是关键过程
//简单起见1代表租客2代表房东
func (m *Mediator) Serve(client interface{}) {
switch client.(type) {
case ITenant:
fmt.Println("i am serving tenant")
case ILandlord:
fmt.Println("i am serving landlord")
}
}

View File

@ -1,15 +1,36 @@
package mediator package mediator
import "testing" import (
"fmt"
"testing"
)
func TestMediator(t *testing.T) { func TestMediator(t *testing.T) {
med := &mediator{} med := &Mediator{}
landlord := &Landlord{} landlord := &Landlord{}
//登记房源信息
med.publishRoom(landlord) med.RegisterRoom(landlord)
tenant := &Tenant{} tenant := &Tenant{}
//向中介租房
med.RentOutRoom(tenant)
//房东收租
landlord.CollectRent(med)
//租客要求修理
tenant.AskRepair(med)
} }
func TestClassCompose(t *testing.T) {
med := &Mediator{Person: Person{Name: "mediator", WalletAssets: 1001}}
landlord := &Landlord{Person: Person{Name: "landlord", WalletAssets: 2000}, RentAccout: 500}
tenant := &Tenant{Person: Person{Name: "tenant", WalletAssets: 500}, furniture: "desk"}
fmt.Println("mediator", med)
fmt.Println("landlord", landlord.Name)
fmt.Println("tenant", tenant.furniture)
}