mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-25 06:16:03 +03:00
mediator pattern
This commit is contained in:
parent
af1573a1d9
commit
66ce5358a4
@ -38,7 +38,7 @@ A curated collection of idiomatic design & application patterns for Go language.
|
||||
|:-------:|:----------- |:------:|
|
||||
| [Chain of Responsibility](/behavioral/chain_of_responsibility/main.go) | Avoids coupling a sender to receiver by giving more than object a chance to handle the request | ✔ |
|
||||
| [Command](/behavioral/command/main.go) | Bundles a command and arguments to call later | ✔ |
|
||||
| [Mediator](/behavioral/mediator.md) | Connects objects and acts as a proxy | ✘ |
|
||||
| [Mediator](/behavioral/mediator/main.go) | Connects objects and acts as a proxy | ✔ |
|
||||
| [Memento](/behavioral/memento.md) | Generate an opaque token that can be used to go back to a previous state | ✘ |
|
||||
| [Observer](/behavioral/observer.md) | Provide a callback for notification of events/changes to data | ✔ |
|
||||
| [Registry](/behavioral/registry.md) | Keep track of all subclasses of a given class | ✘ |
|
||||
|
76
behavioral/mediator/main.go
Normal file
76
behavioral/mediator/main.go
Normal file
@ -0,0 +1,76 @@
|
||||
// 中介者模式(mediator pattern)
|
||||
// 当两个对象存在复杂的依赖关系时,考虑增加一个中介者使之解耦,使他们不需要进行显示的引用.
|
||||
// 但问题在于中介本身会去实现复杂的逻辑,进而导致中介者变得复杂臃肿难以维护.
|
||||
//
|
||||
// 这里是竞价和消息交换的例子.
|
||||
// 参与竞价的玩家只需要向中介出价就可以知道是否能买到商品,由中介确定谁的出价高
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
// buyer 之间通过中介进行引用
|
||||
type Buyer struct {
|
||||
m *Mediator
|
||||
Name string// unique
|
||||
}
|
||||
|
||||
func (b Buyer)Bid(price int){
|
||||
if p ,ok := b.m.isMaxPrice(b ,price);ok{
|
||||
log.Println(b.Name ,"are in lead temporarily with" ,p)
|
||||
}else{
|
||||
log.Println(b.Name ,"lose with" ,p)
|
||||
}
|
||||
}
|
||||
|
||||
type Mediator struct {
|
||||
Buyers map[Buyer]int
|
||||
MaxPrice int
|
||||
MaxPriceBuyer Buyer
|
||||
}
|
||||
|
||||
func (m *Mediator)isMaxPrice(b Buyer ,price int)(int ,bool){
|
||||
if b.Name == m.MaxPriceBuyer.Name{
|
||||
return m.MaxPrice ,true
|
||||
}
|
||||
m.Buyers[b] = price
|
||||
for k ,v := range m.Buyers{
|
||||
if v > m.MaxPrice{
|
||||
m.MaxPrice = v
|
||||
m.MaxPriceBuyer = k
|
||||
}
|
||||
}
|
||||
if b.Name == m.MaxPriceBuyer.Name{
|
||||
return price ,true
|
||||
}
|
||||
return price ,false
|
||||
}
|
||||
|
||||
func main(){
|
||||
m := &Mediator{
|
||||
make(map[Buyer]int),
|
||||
0,
|
||||
Buyer{},
|
||||
}
|
||||
|
||||
icg := Buyer{m ,"icg"}
|
||||
nyn := Buyer{m ,"nyn"}
|
||||
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
for i:=0 ;i<3 ;i++{
|
||||
icg.Bid(rand.Intn(10000))
|
||||
nyn.Bid(rand.Intn(10000))
|
||||
}
|
||||
// output:
|
||||
/*
|
||||
2019/05/02 12:19:52 icg are in lead temporarily with 1024
|
||||
2019/05/02 12:19:52 nyn are in lead temporarily with 4232
|
||||
2019/05/02 12:19:52 icg are in lead temporarily with 6412
|
||||
2019/05/02 12:19:52 nyn are in lead temporarily with 6747
|
||||
2019/05/02 12:19:52 icg lose with 4951
|
||||
2019/05/02 12:19:52 nyn are in lead temporarily with 6747
|
||||
*/
|
||||
}
|
Loading…
Reference in New Issue
Block a user