forked from go/golangs_learn
viviman
3 years ago
4 changed files with 200 additions and 0 deletions
@ -0,0 +1,66 @@ |
|||
package command |
|||
|
|||
import "fmt" |
|||
|
|||
type Person struct { |
|||
name string |
|||
cmd Command |
|||
} |
|||
|
|||
func NewPerson(name string, cmd Command) Person { |
|||
return Person{ |
|||
name: name, |
|||
cmd: cmd, |
|||
} |
|||
} |
|||
|
|||
type Command struct { |
|||
person *Person |
|||
method func() |
|||
} |
|||
|
|||
func (c *Command) Exec() { |
|||
c.method() |
|||
} |
|||
|
|||
func NewCommand(person *Person, method func()) Command { |
|||
return Command{ |
|||
person: person, |
|||
method: method, |
|||
} |
|||
} |
|||
|
|||
func (p *Person) Buy() { |
|||
if p != nil { |
|||
fmt.Printf("%s 正在Buy\n", p.name) |
|||
p.cmd.Exec() |
|||
} |
|||
} |
|||
|
|||
func (p *Person) Sleep() { |
|||
if p != nil { |
|||
fmt.Printf("%s 正在Sleep\n", p.name) |
|||
p.cmd.Exec() |
|||
} |
|||
} |
|||
|
|||
func (p *Person) Wash() { |
|||
if p != nil { |
|||
fmt.Printf("%s 正在Wash\n", p.name) |
|||
p.cmd.Exec() |
|||
} |
|||
} |
|||
|
|||
func (p *Person) Drink() { |
|||
if p != nil { |
|||
fmt.Printf("%s 正在Drink\n", p.name) |
|||
p.cmd.Exec() |
|||
} |
|||
} |
|||
|
|||
func (p *Person) Eat() { |
|||
if p != nil { |
|||
fmt.Printf("%s 正在Eat\n", p.name) |
|||
p.cmd.Exec() |
|||
} |
|||
} |
@ -0,0 +1,14 @@ |
|||
package command |
|||
|
|||
import "testing" |
|||
|
|||
func TestCommand_Exec(t *testing.T) { |
|||
zs := NewPerson("张三", NewCommand(nil, nil)) |
|||
ls := NewPerson("老四", NewCommand(&zs, zs.Buy)) |
|||
ww := NewPerson("王五", NewCommand(&ls, ls.Drink)) |
|||
ml := NewPerson("马六", NewCommand(&ww, ww.Eat)) |
|||
tq := NewPerson("田七", NewCommand(&ml, ml.Sleep)) |
|||
hb := NewPerson("胡八", NewCommand(&tq, tq.Wash)) |
|||
|
|||
hb.cmd.Exec() |
|||
} |
@ -0,0 +1,73 @@ |
|||
|
|||
```go |
|||
package command |
|||
|
|||
import "fmt" |
|||
|
|||
type Person struct { |
|||
name string |
|||
cmd Command |
|||
} |
|||
|
|||
func NewPerson(name string, cmd Command) Person { |
|||
return Person{ |
|||
name: name, |
|||
cmd: cmd, |
|||
} |
|||
} |
|||
|
|||
type Command struct { |
|||
person *Person |
|||
method func() |
|||
} |
|||
|
|||
func (c *Command) Exec() { |
|||
c.method() |
|||
} |
|||
|
|||
func NewCommand(person *Person, method func()) Command { |
|||
return Command{ |
|||
person: person, |
|||
method: method, |
|||
} |
|||
} |
|||
|
|||
func (p *Person) Buy() { |
|||
if p != nil { |
|||
fmt.Printf("%s 正在Buy\n", p.name) |
|||
p.cmd.Exec() |
|||
} |
|||
} |
|||
|
|||
func (p *Person) Sleep() { |
|||
if p != nil { |
|||
fmt.Printf("%s 正在Sleep\n", p.name) |
|||
p.cmd.Exec() |
|||
} |
|||
} |
|||
|
|||
func (p *Person) Wash() { |
|||
if p != nil { |
|||
fmt.Printf("%s 正在Wash\n", p.name) |
|||
p.cmd.Exec() |
|||
} |
|||
} |
|||
|
|||
func (p *Person) Drink() { |
|||
if p != nil { |
|||
fmt.Printf("%s 正在Drink\n", p.name) |
|||
p.cmd.Exec() |
|||
} |
|||
} |
|||
|
|||
func (p *Person) Eat() { |
|||
if p != nil { |
|||
fmt.Printf("%s 正在Eat\n", p.name) |
|||
p.cmd.Exec() |
|||
} |
|||
} |
|||
|
|||
``` |
|||
* 命令模式 |
|||
|
|||
* 命令模式属于将函数作为属性操作,通过实例化结构体,调用属性函数,实现命令执行 |
Loading…
Reference in new issue