forked from go/golangs_learn
VivimanZhang
3 years ago
9 changed files with 235 additions and 0 deletions
@ -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++ |
|||
} |
|||
} |
@ -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) |
|||
} |
|||
|
|||
} |
@ -0,0 +1,7 @@ |
|||
|
|||
```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") |
|||
} |
@ -0,0 +1,9 @@ |
|||
package template |
|||
|
|||
import "testing" |
|||
|
|||
func TestWorker_Daily(t *testing.T) { |
|||
worker := NewWorker(&Coder{}) |
|||
|
|||
worker.Daily() |
|||
} |
@ -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时,实现多态调用 |
@ -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() |
|||
} |
@ -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)) |
|||
} |
@ -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…
Reference in new issue