VivimanZhang 4 years ago
parent
commit
6a5ae03a17
  1. 2
      golang_learn/data_prattern/abstract_factory/说明.md
  2. 41
      golang_learn/data_prattern/builder/main.go
  3. 14
      golang_learn/data_prattern/builder/main_test.go
  4. 6
      golang_learn/data_prattern/builder/说明.md
  5. 39
      golang_learn/data_prattern/facade/main.go
  6. 8
      golang_learn/data_prattern/facade/main_test.go
  7. 48
      golang_learn/data_prattern/facade/说明.md
  8. 1
      golang_learn/data_prattern/factory/说明.md

2
golang_learn/data_prattern/abstract_factory/说明.md

@ -43,6 +43,8 @@ func (s *SimpleLunchFactory) CreateVegetable() Lunch {
} }
``` ```
* 抽象工厂
* 实际抽象工厂是对工程的工厂函数的再次接口抽象 * 实际抽象工厂是对工程的工厂函数的再次接口抽象
* `type Lunch interface { Cook() }` 声明接口,声明`type Rise struct {}` 和 `type Tomato struct {}` 结构体,通过结构体声明接口函数,使得实现接口,从而实现多态。 * `type Lunch interface { Cook() }` 声明接口,声明`type Rise struct {}` 和 `type Tomato struct {}` 结构体,通过结构体声明接口函数,使得实现接口,从而实现多态。

41
golang_learn/data_prattern/builder/main.go

@ -0,0 +1,41 @@
package builder
type Builder interface {
Build()
}
func NewDirector(b Builder) Director {
return Director{build: b}
}
type Director struct {
build Builder
}
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 NewProduct() Product {
return Product{Built: false}
}
func (b *ConcreateBuilder) GetResult() Product {
return Product{b.Built}
}

14
golang_learn/data_prattern/builder/main_test.go

@ -0,0 +1,14 @@
package builder
import (
"fmt"
"testing"
)
func TestConcreateBuilder_GetResult(t *testing.T) {
concreateBuilder := NewConcreateBuilder()
director := NewDirector(&concreateBuilder)
director.Construct()
result := concreateBuilder.GetResult()
fmt.Println(result)
}

6
golang_learn/data_prattern/builder/说明.md

@ -0,0 +1,6 @@
```go
```
* 建造者模式
* 将整体封装起来,对外仅仅展示一个 调用接口

39
golang_learn/data_prattern/facade/main.go

@ -0,0 +1,39 @@
package facade
import "fmt"
type CarModel struct {
}
func NewCarModel() *CarModel {
return &CarModel{}
}
func (c *CarModel) SetModel() {
fmt.Println("SetModel测试...", c)
}
type CarEngine struct {
}
func (c *CarEngine) SetEngine() {
fmt.Println("SetEngine测试...", c)
}
type CarBody struct {
}
func (c *CarBody) SetBody() {
fmt.Println("SetBody测试...", c)
}
type CarFacade struct {
model CarModel
engine CarEngine
body CarBody
}
func (c *CarFacade) CreateCompleteCar() {
c.model.SetModel()
c.engine.SetEngine()
c.body.SetBody()
}

8
golang_learn/data_prattern/facade/main_test.go

@ -0,0 +1,8 @@
package facade
import "testing"
func TestCarFacade_CreateCompleteCar(t *testing.T) {
facade := CarFacade{}
facade.CreateCompleteCar()
}

48
golang_learn/data_prattern/facade/说明.md

@ -0,0 +1,48 @@
```go
package facade
import "fmt"
type CarModel struct {
}
func NewCarModel() *CarModel {
return &CarModel{}
}
func (c *CarModel) SetModel() {
fmt.Println("SetModel测试...", c)
}
type CarEngine struct {
}
func (c *CarEngine) SetEngine() {
fmt.Println("SetEngine测试...", c)
}
type CarBody struct {
}
func (c *CarBody) SetBody() {
fmt.Println("SetBody测试...", c)
}
type CarFacade struct {
model CarModel
engine CarEngine
body CarBody
}
func (c *CarFacade) CreateCompleteCar() {
c.model.SetModel()
c.engine.SetEngine()
c.body.SetBody()
}
```
* 外观模式
* 将整体封装起来,对外仅仅展示一个 调用接口

1
golang_learn/data_prattern/factory/说明.md

@ -34,6 +34,7 @@ func NewRestaurant(name string) Restaurant {
return nil return nil
} }
``` ```
* 工厂模式
* `type Restaurant interface { GetFood() }` 声明接口,声明`type Aa struct {}` 和 `type Bb struct {}` 结构体,通过结构体声明接口函数,使得实现接口,从而实现多态。 * `type Restaurant interface { GetFood() }` 声明接口,声明`type Aa struct {}` 和 `type Bb struct {}` 结构体,通过结构体声明接口函数,使得实现接口,从而实现多态。

Loading…
Cancel
Save