Browse Source

模式补充

master
viviman 4 years ago
parent
commit
77c7121e81
  1. 47
      golang_learn/data_prattern/bridage/main.go
  2. 13
      golang_learn/data_prattern/bridage/main_test.go
  3. 9
      golang_learn/data_prattern/bridage/说明.md
  4. 22
      golang_learn/data_prattern/builder/main.go
  5. 8
      golang_learn/data_prattern/builder/main_test.go
  6. 41
      golang_learn/data_prattern/builder/说明.md
  7. 3
      golang_learn/data_prattern/facade/main.go
  8. 7
      golang_learn/data_prattern/说明.md

47
golang_learn/data_prattern/bridage/main.go

@ -0,0 +1,47 @@
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)
}

13
golang_learn/data_prattern/bridage/main_test.go

@ -0,0 +1,13 @@
package bridage
import "testing"
func TestCircle_Draw(t *testing.T) {
red := Circle{}
red.Constructor(100, 100, 10, &RedCircle{})
red.Draw()
yellow := Circle{}
yellow.Constructor(200, 200, 20, &YellowCircle{})
yellow.Draw()
}

9
golang_learn/data_prattern/bridage/说明.md

@ -0,0 +1,9 @@
```go
```
* 桥接模式
* 定义接口并且实现接口,并定义统一结构体,其中一个属性是接口
* 调用时,将需要调用对象作为参数传递进行,实现具体对象调用

22
golang_learn/data_prattern/builder/main.go

@ -4,27 +4,27 @@ type Builder interface {
Build() Build()
} }
func NewDirector(b Builder) Director {
return Director{build: b}
}
type Director struct { type Director struct {
build Builder build Builder
} }
func NewDirector(b Builder) Director {
return Director{build: b}
}
func (d *Director) Construct() { func (d *Director) Construct() {
d.build.Build() d.build.Build()
} }
type ConcreateBuilder struct { type ConcreteBuilder struct {
Built bool Built bool
} }
func NewConcreateBuilder() ConcreateBuilder { func NewConcreteBuilder() ConcreteBuilder {
return ConcreateBuilder{Built: false} return ConcreteBuilder{Built: false}
} }
func (b *ConcreateBuilder) Build() { func (b *ConcreteBuilder) Build() {
b.Built = true b.Built = true
} }
@ -32,10 +32,6 @@ type Product struct {
Built bool Built bool
} }
func NewProduct() Product { func (b *ConcreteBuilder) GetResult() Product {
return Product{Built: false}
}
func (b *ConcreateBuilder) GetResult() Product {
return Product{b.Built} return Product{b.Built}
} }

8
golang_learn/data_prattern/builder/main_test.go

@ -6,9 +6,9 @@ import (
) )
func TestConcreateBuilder_GetResult(t *testing.T) { func TestConcreateBuilder_GetResult(t *testing.T) {
concreateBuilder := NewConcreateBuilder() builder := NewConcreteBuilder()
director := NewDirector(&concreateBuilder) fmt.Println("原值:", builder)
director := NewDirector(&builder)
director.Construct() director.Construct()
result := concreateBuilder.GetResult() fmt.Println("后值:", builder.GetResult())
fmt.Println(result)
} }

41
golang_learn/data_prattern/builder/说明.md

@ -1,6 +1,45 @@
```go ```go
package builder
type Builder interface {
Build()
}
type Director struct {
build Builder
}
func NewDirector(b Builder) Director {
return Director{build: b}
}
func (d *Director) Construct() {
d.build.Build()
}
type ConcreateBuilder struct {
Built bool
}
func NewConcreateBuilder() ConcreateBuilder {
return ConcreateBuilder{Built: false}
}
func (b *ConcreateBuilder) Build() {
b.Built = true
}
type Product struct {
Built bool
}
func (b *ConcreateBuilder) GetResult() Product {
return Product{b.Built}
}
``` ```
* 建造者模式 * 建造者模式
* 将整体封装起来,对外仅仅展示一个 调用接口 * 封装接口并写实现方法,这是定义的函数,当参数为接口时,入参可以是任何实现接口的实现,并且调用时和作为参数传入时,均会对数据校验。
* 在具体接口实现中完成自己想要完成的代码实现,即实现分离。

3
golang_learn/data_prattern/facade/main.go

@ -5,9 +5,6 @@ import "fmt"
type CarModel struct { type CarModel struct {
} }
func NewCarModel() *CarModel {
return &CarModel{}
}
func (c *CarModel) SetModel() { func (c *CarModel) SetModel() {
fmt.Println("SetModel测试...", c) fmt.Println("SetModel测试...", c)
} }

7
golang_learn/data_prattern/说明.md

@ -0,0 +1,7 @@
```go
```
* 模式
*
Loading…
Cancel
Save