From e4cc42e2ae2e8e9b6ba6dd054623d070acab9031 Mon Sep 17 00:00:00 2001 From: VivimanZhang Date: Sun, 15 Aug 2021 21:10:51 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E8=AE=A1=E6=A8=A1=E5=BC=8F=E5=AD=A6?= =?UTF-8?q?=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- golang_learn/data_prattern/iterator/main.go | 31 ++++++++++++ .../data_prattern/iterator/main_test.go | 20 ++++++++ golang_learn/data_prattern/iterator/说明.md | 7 +++ golang_learn/data_prattern/template/main.go | 38 +++++++++++++++ .../data_prattern/template/main_test.go | 9 ++++ golang_learn/data_prattern/template/说明.md | 47 +++++++++++++++++++ golang_learn/data_prattern/visitor/main.go | 32 +++++++++++++ .../data_prattern/visitor/main_test.go | 10 ++++ golang_learn/data_prattern/visitor/说明.md | 41 ++++++++++++++++ 9 files changed, 235 insertions(+) create mode 100644 golang_learn/data_prattern/iterator/main.go create mode 100644 golang_learn/data_prattern/iterator/main_test.go create mode 100644 golang_learn/data_prattern/iterator/说明.md create mode 100644 golang_learn/data_prattern/template/main.go create mode 100644 golang_learn/data_prattern/template/main_test.go create mode 100644 golang_learn/data_prattern/template/说明.md create mode 100644 golang_learn/data_prattern/visitor/main.go create mode 100644 golang_learn/data_prattern/visitor/main_test.go create mode 100644 golang_learn/data_prattern/visitor/说明.md diff --git a/golang_learn/data_prattern/iterator/main.go b/golang_learn/data_prattern/iterator/main.go new file mode 100644 index 0000000..6963405 --- /dev/null +++ b/golang_learn/data_prattern/iterator/main.go @@ -0,0 +1,31 @@ +package iterator + +type Iterator interface { + Index() int + Value() interface{} + HashNext() bool + Next() +} + +type ArrayIterator struct { + array []interface{} + index *int +} + +func (a *ArrayIterator) Index() *int { + return a.index +} + +func (a *ArrayIterator) Value() interface{} { + return a.array[*a.index] +} + +func (a *ArrayIterator) HashNext() bool { + return *a.index+1 <= len(a.array) +} + +func (a *ArrayIterator) Next() { + if a.HashNext() { + *a.index++ + } +} diff --git a/golang_learn/data_prattern/iterator/main_test.go b/golang_learn/data_prattern/iterator/main_test.go new file mode 100644 index 0000000..bce3c55 --- /dev/null +++ b/golang_learn/data_prattern/iterator/main_test.go @@ -0,0 +1,20 @@ +package iterator + +import ( + "fmt" + "testing" +) + +func TestArrayIterator_HashNext(t *testing.T) { + arr := []interface{}{1, 3, 6, 9, 2, 4, 6} + a := 0 + iterator := ArrayIterator{array: arr, index: &a} + for it := iterator; iterator.HashNext(); iterator.Next() { + k, v := it.Index(), it.Value().(int) + for v != arr[*k] { + fmt.Errorf("发生错误!") + } + fmt.Println(*k, v) + } + +} diff --git a/golang_learn/data_prattern/iterator/说明.md b/golang_learn/data_prattern/iterator/说明.md new file mode 100644 index 0000000..893bbf8 --- /dev/null +++ b/golang_learn/data_prattern/iterator/说明.md @@ -0,0 +1,7 @@ + +```go + +``` +* 迭代器模式 + +* 全程记录下标是使用的指针,返回值使用指针获取具体值 \ No newline at end of file diff --git a/golang_learn/data_prattern/template/main.go b/golang_learn/data_prattern/template/main.go new file mode 100644 index 0000000..cb3f649 --- /dev/null +++ b/golang_learn/data_prattern/template/main.go @@ -0,0 +1,38 @@ +package template + +import "fmt" + +type WorkInterface interface { + GetUp() + Work() + Sleep() +} + +type Worker struct { + WorkInterface +} + +func NewWorker(w WorkInterface) *Worker { + return &Worker{w} +} + +func (w *Worker) Daily() { + w.GetUp() + w.Work() + w.Sleep() +} + +type Coder struct { +} + +func (c *Coder) GetUp() { + fmt.Println("::操作:GetUp") +} + +func (c *Coder) Work() { + fmt.Println("::操作:Work") +} + +func (c *Coder) Sleep() { + fmt.Println("::操作:Sleep") +} diff --git a/golang_learn/data_prattern/template/main_test.go b/golang_learn/data_prattern/template/main_test.go new file mode 100644 index 0000000..fd7ce49 --- /dev/null +++ b/golang_learn/data_prattern/template/main_test.go @@ -0,0 +1,9 @@ +package template + +import "testing" + +func TestWorker_Daily(t *testing.T) { + worker := NewWorker(&Coder{}) + + worker.Daily() +} diff --git a/golang_learn/data_prattern/template/说明.md b/golang_learn/data_prattern/template/说明.md new file mode 100644 index 0000000..36e44d3 --- /dev/null +++ b/golang_learn/data_prattern/template/说明.md @@ -0,0 +1,47 @@ + +```go +package template + +import "fmt" + +type WorkInterface interface { + GetUp() + Work() + Sleep() +} + +type Worker struct { + WorkInterface +} + +func NewWorker(w WorkInterface) *Worker { + return &Worker{w} +} + +func (w *Worker) Daily() { + w.GetUp() + w.Work() + w.Sleep() +} + +type Coder struct { + +} + +func (c *Coder) GetUp() { + fmt.Println("::操作:GetUp") +} + +func (c *Coder) Work() { + fmt.Println("::操作:Work") +} + +func (c *Coder) Sleep() { + fmt.Println("::操作:Sleep") +} +``` +* 模版模式 + +* Worker 继承接口,Coder实现接口,及Coder是Worker的子类 + +* 当将Coder作为参数传递给Worker时,实现多态调用 \ No newline at end of file diff --git a/golang_learn/data_prattern/visitor/main.go b/golang_learn/data_prattern/visitor/main.go new file mode 100644 index 0000000..017a1cd --- /dev/null +++ b/golang_learn/data_prattern/visitor/main.go @@ -0,0 +1,32 @@ +package visitor + +import "fmt" + +type IVisitor interface { + Visit() +} + +type WeiBoVisitor struct { +} + +func (w *WeiBoVisitor) Visit() { + fmt.Println("调用::WeiBoVisitor") +} + +type WeiXinVisitor struct { +} + +func (w *WeiXinVisitor) Visit() { + fmt.Println("调用::WeiXinVisitor") +} + +type IElement interface { + Accept(v IVisitor) +} + +type Element struct { +} + +func (e *Element) Accept(v IVisitor) { + v.Visit() +} diff --git a/golang_learn/data_prattern/visitor/main_test.go b/golang_learn/data_prattern/visitor/main_test.go new file mode 100644 index 0000000..ca9a6dc --- /dev/null +++ b/golang_learn/data_prattern/visitor/main_test.go @@ -0,0 +1,10 @@ +package visitor + +import "testing" + +func TestElement_Accept(t *testing.T) { + + e := new(Element) + e.Accept(new(WeiBoVisitor)) + e.Accept(new(WeiXinVisitor)) +} diff --git a/golang_learn/data_prattern/visitor/说明.md b/golang_learn/data_prattern/visitor/说明.md new file mode 100644 index 0000000..1f83fb1 --- /dev/null +++ b/golang_learn/data_prattern/visitor/说明.md @@ -0,0 +1,41 @@ + +```go +package visitor + +import "fmt" + +type IVisitor interface { + Visit() +} + +type WeiBoVisitor struct { + +} + +func (w *WeiBoVisitor)Visit() { + fmt.Println("调用::WeiBoVisitor") +} + +type WeiXinVisitor struct { + +} + +func (w *WeiXinVisitor)Visit() { + fmt.Println("调用::WeiXinVisitor") +} + +type IElement interface { + Accept(v IVisitor) +} + +type Element struct { + +} + +func (e *Element)Accept(v IVisitor) { + v.Visit() +} +``` +* 访问者模式 + +* \ No newline at end of file