diff --git a/README.md b/README.md index 7d9f5f7..1f8bb61 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ A curated collection of idiomatic design & application patterns for Go language. | Pattern | Description | Status | |:-------:|:----------- |:------:| -| [Chain of Responsibility](/behavioral/chain_of_responsibility.md) | Avoids coupling a sender to receiver by giving more than object a chance to handle the request | ✘ | +| [Chain of Responsibility](/behavioral/chain_of_responsibility.md) | Avoids coupling a sender to receiver by giving more than object a chance to handle the request | ✔ | | [Command](/behavioral/command.md) | Bundles a command and arguments to call later | ✘ | | [Mediator](/behavioral/mediator.md) | 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 | ✘ | diff --git a/behavioral/chain_of_responsibility/main.go b/behavioral/chain_of_responsibility/main.go index e88bc9a..a8b0b7e 100644 --- a/behavioral/chain_of_responsibility/main.go +++ b/behavioral/chain_of_responsibility/main.go @@ -58,7 +58,7 @@ func main(){ fps := FPSGame{TypeFPS} rpg := RPGGame{TypeRPG} - sl := GameSelector{make([]Game ,0)} + sl := GameSelector{} sl.AddGame(fps ,rpg) player := "icg" diff --git a/behavioral/command/main.go b/behavioral/command/main.go new file mode 100644 index 0000000..b6d89e5 --- /dev/null +++ b/behavioral/command/main.go @@ -0,0 +1,80 @@ +// 主要分为四个部分,command ,ConcreteCommand ,receiver ,invoker. +// 分别对应以下GameCommand ,(CommandAttack|CommandEscape) ,GamePlayer ,Invoker. +package main + +// command +type GameCommand interface { + Execute() +} + +// ConcreteCommand +type CommandAttack struct {Player GamePlayer} + +func (c CommandAttack)Execute(){ + c.Player.Attack() +} + +// ConcreteCommand +type CommandEscape struct {Player GamePlayer} + +func (c CommandEscape)Execute(){ + c.Player.Escape() +} + +// receiver +type GamePlayer interface { + Attack() + Escape() +} + +type GunPlayer struct {Name string} + +func (g GunPlayer) Attack() { + println(g.Name ,"opened fire") +} + +func (g GunPlayer) Escape() { + println(g.Name ,"escape") +} + +// invoker +type CommandInvoker struct { + CommandList chan GameCommand +} + +func (in *CommandInvoker)CallCommands(){ + for{ + select { + case cmd := <- in.CommandList: + cmd.Execute() + default: + return + } + } +} + +func (in *CommandInvoker)PushCommands(c ...GameCommand){ + for _ ,v := range c{ + in.CommandList <- v + } +} + +func main(){ + invoker := &CommandInvoker{ + make(chan GameCommand ,10), + } + + playerA := GunPlayer{"icg"} + attk := CommandAttack{playerA} + escp := CommandEscape{playerA} + + invoker.PushCommands(attk ,escp ,escp ,attk) + invoker.CallCommands() + // output: + /* + icg opened fire + icg escape + icg escape + icg opened fire + */ +} diff --git a/behavioral/template/main.go b/behavioral/template/main.go index f1c5245..d6aaa18 100644 --- a/behavioral/template/main.go +++ b/behavioral/template/main.go @@ -3,7 +3,6 @@ // 策略模式倾向于通过改变结构的属性从而改变算法,模板模式则倾向于事先定义一个处理的流程,该流程是可以被整体替换的. // 个人认为应该保证模板模式下的结构体应当独立出来,每个流程单独被调用. // 感觉以下两种写法都有点别扭,待指正 - package main type Game interface {