From 66ce5358a450c660439c15c08f882941dee5b2f3 Mon Sep 17 00:00:00 2001 From: nynicg Date: Thu, 2 May 2019 12:22:10 +0800 Subject: [PATCH] mediator pattern --- README.md | 2 +- behavioral/mediator/main.go | 76 +++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 behavioral/mediator/main.go diff --git a/README.md b/README.md index 1d4b001..56a6c98 100644 --- a/README.md +++ b/README.md @@ -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 | ✘ | diff --git a/behavioral/mediator/main.go b/behavioral/mediator/main.go new file mode 100644 index 0000000..90d0d5c --- /dev/null +++ b/behavioral/mediator/main.go @@ -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 + */ +}