From 77c7121e813bc10d43f2980144c196d2b918f22c Mon Sep 17 00:00:00 2001 From: viviman Date: Thu, 12 Aug 2021 22:31:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- golang_learn/data_prattern/bridage/main.go | 47 +++++++++++++++++++ .../data_prattern/bridage/main_test.go | 13 +++++ golang_learn/data_prattern/bridage/说明.md | 9 ++++ golang_learn/data_prattern/builder/main.go | 22 ++++----- .../data_prattern/builder/main_test.go | 8 ++-- golang_learn/data_prattern/builder/说明.md | 41 +++++++++++++++- golang_learn/data_prattern/facade/main.go | 3 -- golang_learn/data_prattern/说明.md | 7 +++ 8 files changed, 129 insertions(+), 21 deletions(-) create mode 100644 golang_learn/data_prattern/bridage/main.go create mode 100644 golang_learn/data_prattern/bridage/main_test.go create mode 100644 golang_learn/data_prattern/bridage/说明.md create mode 100644 golang_learn/data_prattern/说明.md diff --git a/golang_learn/data_prattern/bridage/main.go b/golang_learn/data_prattern/bridage/main.go new file mode 100644 index 0000000..d7eb06e --- /dev/null +++ b/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) +} diff --git a/golang_learn/data_prattern/bridage/main_test.go b/golang_learn/data_prattern/bridage/main_test.go new file mode 100644 index 0000000..dac8108 --- /dev/null +++ b/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() +} diff --git a/golang_learn/data_prattern/bridage/说明.md b/golang_learn/data_prattern/bridage/说明.md new file mode 100644 index 0000000..bc3fcc9 --- /dev/null +++ b/golang_learn/data_prattern/bridage/说明.md @@ -0,0 +1,9 @@ + +```go + +``` +* 桥接模式 + +* 定义接口并且实现接口,并定义统一结构体,其中一个属性是接口 + +* 调用时,将需要调用对象作为参数传递进行,实现具体对象调用 \ No newline at end of file diff --git a/golang_learn/data_prattern/builder/main.go b/golang_learn/data_prattern/builder/main.go index a645a4a..90a7957 100644 --- a/golang_learn/data_prattern/builder/main.go +++ b/golang_learn/data_prattern/builder/main.go @@ -4,27 +4,27 @@ type Builder interface { Build() } -func NewDirector(b Builder) Director { - return Director{build: b} -} - type Director struct { build Builder } +func NewDirector(b Builder) Director { + return Director{build: b} +} + func (d *Director) Construct() { d.build.Build() } -type ConcreateBuilder struct { +type ConcreteBuilder struct { Built bool } -func NewConcreateBuilder() ConcreateBuilder { - return ConcreateBuilder{Built: false} +func NewConcreteBuilder() ConcreteBuilder { + return ConcreteBuilder{Built: false} } -func (b *ConcreateBuilder) Build() { +func (b *ConcreteBuilder) Build() { b.Built = true } @@ -32,10 +32,6 @@ type Product struct { Built bool } -func NewProduct() Product { - return Product{Built: false} -} - -func (b *ConcreateBuilder) GetResult() Product { +func (b *ConcreteBuilder) 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 index 1cd2087..a33df7c 100644 --- a/golang_learn/data_prattern/builder/main_test.go +++ b/golang_learn/data_prattern/builder/main_test.go @@ -6,9 +6,9 @@ import ( ) func TestConcreateBuilder_GetResult(t *testing.T) { - concreateBuilder := NewConcreateBuilder() - director := NewDirector(&concreateBuilder) + builder := NewConcreteBuilder() + fmt.Println("原值:", builder) + director := NewDirector(&builder) director.Construct() - result := concreateBuilder.GetResult() - fmt.Println(result) + fmt.Println("后值:", builder.GetResult()) } diff --git a/golang_learn/data_prattern/builder/说明.md b/golang_learn/data_prattern/builder/说明.md index e518a0f..ca19fd5 100644 --- a/golang_learn/data_prattern/builder/说明.md +++ b/golang_learn/data_prattern/builder/说明.md @@ -1,6 +1,45 @@ ```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} +} ``` * 建造者模式 -* 将整体封装起来,对外仅仅展示一个 调用接口 \ No newline at end of file +* 封装接口并写实现方法,这是定义的函数,当参数为接口时,入参可以是任何实现接口的实现,并且调用时和作为参数传入时,均会对数据校验。 + +* 在具体接口实现中完成自己想要完成的代码实现,即实现分离。 \ No newline at end of file diff --git a/golang_learn/data_prattern/facade/main.go b/golang_learn/data_prattern/facade/main.go index 893c893..61f81b7 100644 --- a/golang_learn/data_prattern/facade/main.go +++ b/golang_learn/data_prattern/facade/main.go @@ -5,9 +5,6 @@ import "fmt" type CarModel struct { } -func NewCarModel() *CarModel { - return &CarModel{} -} func (c *CarModel) SetModel() { fmt.Println("SetModel测试...", c) } diff --git a/golang_learn/data_prattern/说明.md b/golang_learn/data_prattern/说明.md new file mode 100644 index 0000000..5e5453e --- /dev/null +++ b/golang_learn/data_prattern/说明.md @@ -0,0 +1,7 @@ + +```go + +``` +* 模式 + +* \ No newline at end of file