Skip to content

Commit 48f693d

Browse files
committed
design pattern
1 parent 95442ba commit 48f693d

22 files changed

+579
-0
lines changed

behavioral/Command/command.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package Command
2+
3+
import "fmt"
4+
5+
type Person struct {
6+
name string
7+
cmd Command
8+
}
9+
10+
type Command struct {
11+
person *Person
12+
method func()
13+
}
14+
15+
func NewCommand(p *Person, method func()) Command {
16+
return Command{person: p, method: method}
17+
}
18+
19+
func (c *Command) Execute() {
20+
c.method()
21+
}
22+
23+
func NewPerson(name string, cmd Command) Person {
24+
return Person{name, cmd}
25+
}
26+
27+
func (p *Person) Talk() {
28+
fmt.Println(fmt.Sprintf("%s is talking.\n", p.name))
29+
p.cmd.Execute()
30+
}
31+
32+
func (p *Person) PassOn() {
33+
fmt.Println(fmt.Sprintf("%s is passing on.\n", p.name))
34+
p.cmd.Execute()
35+
}
36+
37+
func (p *Person) Gossip() {
38+
fmt.Println(fmt.Sprintf("%s is gossiping.\n", p.name))
39+
p.cmd.Execute()
40+
}
41+
42+
func (p *Person) Listen() {
43+
fmt.Println(fmt.Sprintf("%s is Listening.\n", p.name))
44+
}

behavioral/Command/command_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package Command
2+
3+
import "testing"
4+
5+
func TestPerson_Talk(t *testing.T) {
6+
laowang := NewPerson("wang", NewCommand(nil, nil))
7+
laozhang := NewPerson("zhang", NewCommand(&laowang, laowang.Listen))
8+
laofeng := NewPerson("feng", NewCommand(&laozhang, laozhang.Gossip))
9+
laoding := NewPerson("ding", NewCommand(&laofeng, laofeng.PassOn))
10+
11+
laoding.Talk()
12+
}

behavioral/Iterator/iterator.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package Iterator
2+
3+
type Iterator interface {
4+
Index() int
5+
Value() interface{}
6+
HasNext() bool
7+
Next()
8+
}
9+
10+
type ArrayIterator struct {
11+
array []interface{}
12+
index *int
13+
}
14+
15+
func (a *ArrayIterator) Index() *int {
16+
17+
return a.index
18+
}
19+
20+
func (a *ArrayIterator) Value() interface{} {
21+
return a.array[*a.index]
22+
}
23+
24+
func (a *ArrayIterator) HasNext() bool {
25+
return *a.index+1 <= len(a.array)
26+
}
27+
28+
func (a *ArrayIterator) Next() {
29+
if a.HasNext() {
30+
31+
*a.index++
32+
}
33+
}

behavioral/Iterator/iterator_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package Iterator
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestIterator(t *testing.T) {
9+
array := []interface{}{1, 3, 9, 2, 8}
10+
a := 0
11+
iterator := ArrayIterator{array: array, index: &a}
12+
for it := iterator; iterator.HasNext(); iterator.Next() {
13+
index, value := it.Index(), it.Value().(int)
14+
if value != array[*index] {
15+
fmt.Println("error...")
16+
}
17+
fmt.Println(*index, value)
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package Responsibility_Chain
2+
3+
import "strconv"
4+
5+
type Handler interface {
6+
Handler(handleID int) string
7+
}
8+
9+
type handler struct {
10+
name string
11+
next Handler
12+
handlerID int
13+
}
14+
15+
func NewHandler(name string, next Handler, handleID int) *handler {
16+
return &handler{name, next, handleID}
17+
}
18+
19+
func (h *handler) Handler(handlerID int) string {
20+
if h.handlerID == handlerID {
21+
return h.name + " handled " + strconv.Itoa(handlerID)
22+
}
23+
return h.next.Handler(handlerID)
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package Responsibility_Chain
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestHandler_Handler(t *testing.T) {
9+
micheal := NewHandler("Micheal", nil, 1)
10+
mike := NewHandler("Mike", micheal, 2)
11+
r := mike.Handler(1)
12+
fmt.Println(r)
13+
r = mike.Handler(2)
14+
fmt.Println(r)
15+
}

behavioral/Strategy/strategry_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package Strategy
2+
3+
import "testing"
4+
5+
func TestContext_Execute(t *testing.T) {
6+
strategy := NewStrategyB()
7+
context := NewContext()
8+
context.SetStrategy(strategy)
9+
context.Execute()
10+
}

behavioral/Strategy/strategy.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package Strategy
2+
3+
import "fmt"
4+
5+
type Strategy interface {
6+
Execute()
7+
}
8+
9+
type strategyA struct {
10+
}
11+
12+
func (s *strategyA) Execute() {
13+
fmt.Println("A plan executed.")
14+
}
15+
16+
func NewStrategyA() Strategy {
17+
return &strategyA{}
18+
}
19+
20+
type strategyB struct {
21+
}
22+
23+
func (s *strategyB) Execute() {
24+
fmt.Println("B plan executed.")
25+
}
26+
27+
func NewStrategyB() Strategy {
28+
return &strategyB{}
29+
}
30+
31+
type Context struct {
32+
strategy Strategy
33+
}
34+
35+
func NewContext() *Context {
36+
return &Context{}
37+
}
38+
39+
func (c *Context) SetStrategy(strategy Strategy) {
40+
c.strategy = strategy
41+
}
42+
func (c *Context) Execute() {
43+
c.strategy.Execute()
44+
}

behavioral/Template/template.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package Template
2+
3+
import "fmt"
4+
5+
type WorkerInterface interface {
6+
GetUp()
7+
Work()
8+
Sleep()
9+
}
10+
11+
type Worker struct {
12+
WorkerInterface
13+
}
14+
15+
func NewWorker(w WorkerInterface) *Worker {
16+
return &Worker{WorkerInterface: w}
17+
}
18+
19+
func (w *Worker) Daily() {
20+
w.GetUp()
21+
w.Work()
22+
w.Sleep()
23+
}
24+
25+
type Coder struct {
26+
}
27+
28+
func (c *Coder) GetUp() {
29+
fmt.Println("coder GetUp")
30+
}
31+
32+
func (c *Coder) Work() {
33+
fmt.Println("coder Work")
34+
}
35+
36+
func (c *Coder) Sleep() {
37+
fmt.Println("coder Sleep")
38+
}

behavioral/Template/template_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package Template
2+
3+
import "testing"
4+
5+
func TestWorker_Daily(t *testing.T) {
6+
worker := NewWorker(&Coder{})
7+
worker.Daily()
8+
}

behavioral/Visitor/visitor.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package Visitor
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type IVisitor interface {
8+
Visit()
9+
}
10+
11+
type WeiboVisitor struct {
12+
}
13+
14+
func (w WeiboVisitor) Visit() {
15+
fmt.Println("这是微博")
16+
}
17+
18+
type IQiYiVisitor struct {
19+
}
20+
21+
func (i IQiYiVisitor) Visit() {
22+
fmt.Println("这里是爱奇艺")
23+
}
24+
25+
type IElement interface {
26+
Accept(visitor IVisitor)
27+
}
28+
29+
type Element struct {
30+
}
31+
32+
func (el Element) Accept(v IVisitor) {
33+
v.Visit()
34+
}

behavioral/Visitor/visitor_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package Visitor
2+
3+
import "testing"
4+
5+
func TestElement_Accept(t *testing.T) {
6+
e := new(Element)
7+
e.Accept(new(WeiboVisitor))
8+
e.Accept(new(IQiYiVisitor))
9+
}

creational/Builder/builder.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package Builder
2+
3+
type Builder interface {
4+
Build()
5+
}
6+
7+
type Director struct {
8+
builder Builder
9+
}
10+
11+
func NewDirector(b Builder) Director {
12+
return Director{builder: b}
13+
}
14+
15+
func (d *Director) Construct() {
16+
d.builder.Build()
17+
}
18+
19+
type ConcreteBuilder struct {
20+
built bool
21+
}
22+
23+
func NewConcreteBuilder() ConcreteBuilder {
24+
return ConcreteBuilder{false}
25+
}
26+
27+
func (b *ConcreteBuilder) Build() {
28+
b.built = true
29+
}
30+
31+
type Product struct {
32+
Built bool
33+
}
34+
35+
func (b *ConcreteBuilder) GetResult() Product {
36+
return Product{b.built}
37+
}

creational/Builder/builder_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package Builder
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestConcreteBuilder_GetResult(t *testing.T) {
9+
builder := NewConcreteBuilder()
10+
director := NewDirector(&builder)
11+
director.Construct()
12+
product := builder.GetResult()
13+
fmt.Println(product.Built)
14+
}

0 commit comments

Comments
 (0)