diff --git a/golang_learn/data_prattern/abstract_factory/说明.md b/golang_learn/data_prattern/abstract_factory/说明.md index d76b5b3..e11a67a 100644 --- a/golang_learn/data_prattern/abstract_factory/说明.md +++ b/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 {}` 结构体,通过结构体声明接口函数,使得实现接口,从而实现多态。 diff --git a/golang_learn/data_prattern/builder/main.go b/golang_learn/data_prattern/builder/main.go new file mode 100644 index 0000000..a645a4a --- /dev/null +++ b/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} +} diff --git a/golang_learn/data_prattern/builder/main_test.go b/golang_learn/data_prattern/builder/main_test.go new file mode 100644 index 0000000..1cd2087 --- /dev/null +++ b/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) +} diff --git a/golang_learn/data_prattern/builder/说明.md b/golang_learn/data_prattern/builder/说明.md new file mode 100644 index 0000000..e518a0f --- /dev/null +++ b/golang_learn/data_prattern/builder/说明.md @@ -0,0 +1,6 @@ + +```go +``` +* 建造者模式 + +* 将整体封装起来,对外仅仅展示一个 调用接口 \ No newline at end of file diff --git a/golang_learn/data_prattern/facade/main.go b/golang_learn/data_prattern/facade/main.go new file mode 100644 index 0000000..893c893 --- /dev/null +++ b/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() +} diff --git a/golang_learn/data_prattern/facade/main_test.go b/golang_learn/data_prattern/facade/main_test.go new file mode 100644 index 0000000..e6e9670 --- /dev/null +++ b/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() +} diff --git a/golang_learn/data_prattern/facade/说明.md b/golang_learn/data_prattern/facade/说明.md new file mode 100644 index 0000000..1d2d3de --- /dev/null +++ b/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() +} +``` +* 外观模式 + +* 将整体封装起来,对外仅仅展示一个 调用接口 \ No newline at end of file diff --git a/golang_learn/data_prattern/factory/说明.md b/golang_learn/data_prattern/factory/说明.md index 2ee9d13..36a9e5b 100644 --- a/golang_learn/data_prattern/factory/说明.md +++ b/golang_learn/data_prattern/factory/说明.md @@ -34,6 +34,7 @@ func NewRestaurant(name string) Restaurant { return nil } ``` +* 工厂模式 * `type Restaurant interface { GetFood() }` 声明接口,声明`type Aa struct {}` 和 `type Bb struct {}` 结构体,通过结构体声明接口函数,使得实现接口,从而实现多态。