forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
938 B
61 lines
938 B
3 years ago
|
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()
|
||
|
}
|
||
|
}
|