forked from go/golangs_learn
VivimanZhang
3 years ago
19 changed files with 376 additions and 1 deletions
@ -0,0 +1,34 @@ |
|||
package composite |
|||
|
|||
import "fmt" |
|||
|
|||
type Component interface { |
|||
Traverse() |
|||
} |
|||
|
|||
type Leaf struct { |
|||
value int |
|||
} |
|||
|
|||
func NewLeaf(value int) *Leaf { |
|||
return &Leaf{value: value} |
|||
} |
|||
func (l *Leaf) Traverse() { |
|||
fmt.Println(l.value) |
|||
} |
|||
|
|||
type Composite struct { |
|||
children []Component |
|||
} |
|||
|
|||
func NewComposite() *Composite { |
|||
return &Composite{children: make([]Component, 0)} |
|||
} |
|||
func (c *Composite) Add(component Component) { |
|||
c.children = append(c.children, component) |
|||
} |
|||
func (c *Composite) Traverse() { |
|||
for idx, _ := range c.children { |
|||
c.children[idx].Traverse() |
|||
} |
|||
} |
@ -0,0 +1,25 @@ |
|||
package composite |
|||
|
|||
import ( |
|||
"fmt" |
|||
"testing" |
|||
) |
|||
|
|||
func TestComposite_Traverse(t *testing.T) { |
|||
composites := make([]Composite, 4) |
|||
for i := 0; i < 4; i++ { |
|||
for j := 0; j < 3; j++ { |
|||
composites[i].Add(NewLeaf(i*3 + j)) |
|||
} |
|||
} |
|||
|
|||
for i := 0; i < 4; i++ { |
|||
composites[0].Add(&composites[i]) |
|||
} |
|||
|
|||
for i := 0; i < 4; i++ { |
|||
composites[0].Traverse() |
|||
fmt.Println("+++++++++++") |
|||
} |
|||
|
|||
} |
@ -0,0 +1,7 @@ |
|||
|
|||
```go |
|||
|
|||
``` |
|||
* 组合模式 |
|||
|
|||
* |
@ -0,0 +1,27 @@ |
|||
package fly_weight |
|||
|
|||
type FlyWeight struct { |
|||
Name string |
|||
} |
|||
|
|||
func NewFlyWeight(name string) *FlyWeight { |
|||
return &FlyWeight{Name: name} |
|||
} |
|||
|
|||
type FlyWeightFactory struct { |
|||
pool map[string]*FlyWeight |
|||
} |
|||
|
|||
func NewFlyWeightFactory() *FlyWeightFactory { |
|||
return &FlyWeightFactory{pool: make(map[string]*FlyWeight)} |
|||
} |
|||
|
|||
func (f *FlyWeightFactory) GetFlyWeight(name string) *FlyWeight { |
|||
weight, ok := f.pool[name] |
|||
|
|||
if !ok { |
|||
weight := NewFlyWeight(name) |
|||
f.pool[name] = weight |
|||
} |
|||
return weight |
|||
} |
@ -0,0 +1,16 @@ |
|||
package fly_weight |
|||
|
|||
import ( |
|||
"github.com/bmizerany/assert" |
|||
"testing" |
|||
) |
|||
|
|||
func TestFlyWeightFactory_GetFlyWeight(t *testing.T) { |
|||
factory := NewFlyWeightFactory() |
|||
a := factory.GetFlyWeight("啊啊啊") |
|||
b := factory.GetFlyWeight("不不不") |
|||
|
|||
// assert.Len(&t, factory.pool, 2)
|
|||
assert.Equal(t, a, factory.pool["啊啊啊"]) |
|||
assert.Equal(t, b, factory.pool["不不不"]) |
|||
} |
@ -0,0 +1,7 @@ |
|||
|
|||
```go |
|||
|
|||
``` |
|||
* 享元模式 |
|||
|
|||
* |
@ -0,0 +1,60 @@ |
|||
package mediator |
|||
|
|||
import "fmt" |
|||
|
|||
type Mediator interface { |
|||
Communicate(who string) |
|||
} |
|||
|
|||
type WildStallion interface { |
|||
SetMediator(mediator Mediator) |
|||
} |
|||
|
|||
type Bill struct { |
|||
mediator Mediator |
|||
} |
|||
|
|||
func (b *Bill) SetMediator(mediator Mediator) { |
|||
b.mediator = mediator |
|||
} |
|||
|
|||
func (b *Bill) Respond() { |
|||
fmt.Println("Bill: 多少钱?") |
|||
} |
|||
|
|||
type Teb struct { |
|||
mediator Mediator |
|||
} |
|||
|
|||
func (t *Teb) SetMediator(mediator Mediator) { |
|||
t.mediator = mediator |
|||
} |
|||
|
|||
func (t *Teb) Talk() { |
|||
fmt.Println("bill是什么?") |
|||
t.mediator.Communicate("Ted") |
|||
} |
|||
|
|||
func (t *Teb) Respond() { |
|||
fmt.Println("Ted: 你今天怎么样?") |
|||
} |
|||
|
|||
type ConcreteMediator struct { |
|||
Bill |
|||
Teb |
|||
} |
|||
|
|||
func NewMediator() *ConcreteMediator { |
|||
mediator := &ConcreteMediator{} |
|||
mediator.Bill.SetMediator(mediator) |
|||
mediator.Teb.SetMediator(mediator) |
|||
return mediator |
|||
} |
|||
|
|||
func (m *ConcreteMediator) Communicate(who string) { |
|||
if who == "teb" { |
|||
m.Bill.Respond() |
|||
} else { |
|||
m.Teb.Respond() |
|||
} |
|||
} |
@ -0,0 +1,8 @@ |
|||
package mediator |
|||
|
|||
import "testing" |
|||
|
|||
func TestMediator(t *testing.T) { |
|||
mediator := NewMediator() |
|||
mediator.Teb.Talk() |
|||
} |
@ -0,0 +1,7 @@ |
|||
|
|||
```go |
|||
|
|||
``` |
|||
* 中介模式 |
|||
|
|||
* |
@ -0,0 +1,18 @@ |
|||
package prototype |
|||
|
|||
type Prototype interface { |
|||
Name() string |
|||
Clone() Prototype |
|||
} |
|||
|
|||
type ConcretePrototype struct { |
|||
name string |
|||
} |
|||
|
|||
func (p *ConcretePrototype) Name() string { |
|||
return p.name |
|||
} |
|||
|
|||
func (p *ConcretePrototype) Clone() Prototype { |
|||
return &ConcretePrototype{name: p.name} |
|||
} |
@ -0,0 +1,15 @@ |
|||
package prototype |
|||
|
|||
import ( |
|||
"github.com/bmizerany/assert" |
|||
"testing" |
|||
) |
|||
|
|||
func TestConcretePrototype_Clone(t *testing.T) { |
|||
name := "去除浪" |
|||
prototype := ConcretePrototype{ |
|||
name: name, |
|||
} |
|||
clone := prototype.Clone() |
|||
assert.Equal(t, name, clone.Name()) |
|||
} |
@ -0,0 +1,7 @@ |
|||
|
|||
```go |
|||
|
|||
``` |
|||
* 克隆模式 |
|||
|
|||
* |
@ -0,0 +1,60 @@ |
|||
package state |
|||
|
|||
import "fmt" |
|||
|
|||
type State interface { |
|||
On(m *Machine) |
|||
Off(m *Machine) |
|||
} |
|||
|
|||
type Machine struct { |
|||
current State |
|||
} |
|||
|
|||
func NewMachine() *Machine { |
|||
return &Machine{NewOff()} |
|||
} |
|||
|
|||
func (m *Machine) On() { |
|||
m.current.On(m) |
|||
} |
|||
|
|||
func (m *Machine) Off() { |
|||
m.current.On(m) |
|||
} |
|||
|
|||
func (m *Machine) setCurrent(s State) { |
|||
m.current = s |
|||
} |
|||
|
|||
type On struct { |
|||
} |
|||
|
|||
func NewOn() State { |
|||
return &On{} |
|||
} |
|||
|
|||
func (o *On) On(m *Machine) { |
|||
fmt.Println("从On到Off") |
|||
m.setCurrent(NewOff()) |
|||
} |
|||
|
|||
func (o *On) Off(m *Machine) { |
|||
fmt.Println("已经开启") |
|||
} |
|||
|
|||
type Off struct { |
|||
} |
|||
|
|||
func NewOff() State { |
|||
return &Off{} |
|||
} |
|||
|
|||
func (o *Off) On(m *Machine) { |
|||
fmt.Println("从Off到On") |
|||
m.setCurrent(NewOn()) |
|||
} |
|||
|
|||
func (o *Off) Off(m *Machine) { |
|||
fmt.Println("已经关闭") |
|||
} |
@ -0,0 +1,11 @@ |
|||
package state |
|||
|
|||
import "testing" |
|||
|
|||
func TestState(t *testing.T) { |
|||
machine := NewMachine() |
|||
machine.Off() |
|||
machine.On() |
|||
machine.On() |
|||
machine.Off() |
|||
} |
@ -0,0 +1,7 @@ |
|||
|
|||
```go |
|||
|
|||
``` |
|||
* 状态模式 |
|||
|
|||
* |
@ -0,0 +1,45 @@ |
|||
package strategy |
|||
|
|||
import "fmt" |
|||
|
|||
type Strategy interface { |
|||
Execute() |
|||
} |
|||
|
|||
type StrategyA struct { |
|||
} |
|||
|
|||
func (s *StrategyA) Execute() { |
|||
fmt.Println("A 执行了...") |
|||
} |
|||
|
|||
func NewStrategyA() *StrategyA { |
|||
return &StrategyA{} |
|||
} |
|||
|
|||
type StrategyB struct { |
|||
} |
|||
|
|||
func (s *StrategyB) Execute() { |
|||
fmt.Println("B 执行了...") |
|||
} |
|||
|
|||
func NewStrategyB() *StrategyB { |
|||
return &StrategyB{} |
|||
} |
|||
|
|||
type Contest struct { |
|||
strategy Strategy |
|||
} |
|||
|
|||
func (c *Contest) Execute() { |
|||
c.strategy.Execute() |
|||
} |
|||
|
|||
func NewContest() *Contest { |
|||
return &Contest{} |
|||
} |
|||
|
|||
func (c *Contest) SetContest(strategy Strategy) { |
|||
c.strategy = strategy |
|||
} |
@ -0,0 +1,14 @@ |
|||
package strategy |
|||
|
|||
import "testing" |
|||
|
|||
func TestContest_Execute(t *testing.T) { |
|||
a := NewStrategyA() |
|||
contest := NewContest() |
|||
contest.SetContest(a) |
|||
contest.Execute() |
|||
|
|||
b := NewStrategyB() |
|||
contest.SetContest(b) |
|||
contest.Execute() |
|||
} |
@ -0,0 +1,7 @@ |
|||
|
|||
```go |
|||
|
|||
``` |
|||
* 策略模式 |
|||
|
|||
* |
Loading…
Reference in new issue