mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-22 11:56:03 +03:00
finish mediator pattern
This commit is contained in:
parent
8886ba434d
commit
c4c60eea07
@ -12,58 +12,85 @@ 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 //中介持有房客的信息
|
||||||
|
landlord ILandlord //中介持有房东的信息
|
||||||
feelandlord int
|
feelandlord int
|
||||||
feelandtenant 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")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -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)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user