package mediator import "fmt" type Mediator interface { Communicate(who string) } type WildStallion interface { SetMediator(mediator Mediator) } type Bill struct { mediator Mediator } func (b *Bill) SetMediator(mediator Mediator) { b.mediator = mediator } func (b *Bill) Respond() { fmt.Println("Bill: 多少钱?") } type Teb struct { mediator Mediator } func (t *Teb) SetMediator(mediator Mediator) { t.mediator = mediator } func (t *Teb) Talk() { fmt.Println("bill是什么?") t.mediator.Communicate("Ted") } func (t *Teb) Respond() { fmt.Println("Ted: 你今天怎么样?") } type ConcreteMediator struct { Bill Teb } func NewMediator() *ConcreteMediator { mediator := &ConcreteMediator{} mediator.Bill.SetMediator(mediator) mediator.Teb.SetMediator(mediator) return mediator } func (m *ConcreteMediator) Communicate(who string) { if who == "teb" { m.Bill.Respond() } else { m.Teb.Respond() } }