Browse Source

设计模式学习

master
VivimanZhang 3 years ago
parent
commit
e4cc42e2ae
  1. 31
      golang_learn/data_prattern/iterator/main.go
  2. 20
      golang_learn/data_prattern/iterator/main_test.go
  3. 7
      golang_learn/data_prattern/iterator/说明.md
  4. 38
      golang_learn/data_prattern/template/main.go
  5. 9
      golang_learn/data_prattern/template/main_test.go
  6. 47
      golang_learn/data_prattern/template/说明.md
  7. 32
      golang_learn/data_prattern/visitor/main.go
  8. 10
      golang_learn/data_prattern/visitor/main_test.go
  9. 41
      golang_learn/data_prattern/visitor/说明.md

31
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++
}
}

20
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)
}
}

7
golang_learn/data_prattern/iterator/说明.md

@ -0,0 +1,7 @@
```go
```
* 迭代器模式
* 全程记录下标是使用的指针,返回值使用指针获取具体值

38
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")
}

9
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()
}

47
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时,实现多态调用

32
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()
}

10
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))
}

41
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()
}
```
* 访问者模式
*
Loading…
Cancel
Save