Browse Source

模式补充

master
viviman 4 years ago
parent
commit
af6bf4a4a5
  1. 47
      golang_learn/data_prattern/bridage/说明.md
  2. 66
      golang_learn/data_prattern/command/main.go
  3. 14
      golang_learn/data_prattern/command/main_test.go
  4. 73
      golang_learn/data_prattern/command/说明.md

47
golang_learn/data_prattern/bridage/说明.md

@ -1,5 +1,52 @@
```go
package bridage
import "fmt"
type Draw interface {
DrawCircle(radius, x, y int)
}
type RedCircle struct {
}
func (r *RedCircle) DrawCircle(radius, x, y int) {
fmt.Printf("Red :: radius=%d, x=%d, y=%d\n", radius, x, y)
}
type YellowCircle struct {
}
func (r *YellowCircle) DrawCircle(radius, x, y int) {
fmt.Printf("Yellow :: radius=%d, x=%d, y=%d\n", radius, x, y)
}
type Shape struct {
draw Draw
}
func (s *Shape) Shape(d Draw) {
s.draw = d
}
type Circle struct {
shape Shape
x int
y int
radius int
}
func (c *Circle) Constructor(radius, x, y int, draw Draw) {
c.radius = radius
c.x = x
c.y = y
c.shape.Shape(draw)
}
func (c *Circle) Draw() {
c.shape.draw.DrawCircle(c.radius, c.x, c.y)
}
```
* 桥接模式

66
golang_learn/data_prattern/command/main.go

@ -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()
}
}

14
golang_learn/data_prattern/command/main_test.go

@ -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()
}

73
golang_learn/data_prattern/command/说明.md

@ -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…
Cancel
Save