-
Notifications
You must be signed in to change notification settings - Fork 48
/
main.go
76 lines (68 loc) · 1.73 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
*/
}