command pattern

This commit is contained in:
nynicg 2019-05-02 09:37:29 +08:00
parent 821af9d44b
commit 48ed8bc094
4 changed files with 82 additions and 3 deletions

View File

@ -36,7 +36,7 @@ A curated collection of idiomatic design & application patterns for Go language.
| Pattern | Description | Status | | 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 | ✘ | | [Command](/behavioral/command.md) | Bundles a command and arguments to call later | ✘ |
| [Mediator](/behavioral/mediator.md) | Connects objects and acts as a proxy | ✘ | | [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 | ✘ | | [Memento](/behavioral/memento.md) | Generate an opaque token that can be used to go back to a previous state | ✘ |

View File

@ -58,7 +58,7 @@ func main(){
fps := FPSGame{TypeFPS} fps := FPSGame{TypeFPS}
rpg := RPGGame{TypeRPG} rpg := RPGGame{TypeRPG}
sl := GameSelector{make([]Game ,0)} sl := GameSelector{}
sl.AddGame(fps ,rpg) sl.AddGame(fps ,rpg)
player := "icg" player := "icg"

View File

@ -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
*/
}

View File

@ -3,7 +3,6 @@
// 策略模式倾向于通过改变结构的属性从而改变算法,模板模式则倾向于事先定义一个处理的流程,该流程是可以被整体替换的. // 策略模式倾向于通过改变结构的属性从而改变算法,模板模式则倾向于事先定义一个处理的流程,该流程是可以被整体替换的.
// 个人认为应该保证模板模式下的结构体应当独立出来,每个流程单独被调用. // 个人认为应该保证模板模式下的结构体应当独立出来,每个流程单独被调用.
// 感觉以下两种写法都有点别扭,待指正 // 感觉以下两种写法都有点别扭,待指正
package main package main
type Game interface { type Game interface {