Browse Source

算法温习

master
VivimanZhang 3 years ago
parent
commit
7117356167
  1. 34
      golang_learn/data_prattern/composite/main.go
  2. 25
      golang_learn/data_prattern/composite/main_test.go
  3. 7
      golang_learn/data_prattern/composite/说明.md
  4. 27
      golang_learn/data_prattern/fly_weight/main.go
  5. 16
      golang_learn/data_prattern/fly_weight/main_test.go
  6. 7
      golang_learn/data_prattern/fly_weight/说明.md
  7. 60
      golang_learn/data_prattern/mediator/main.go
  8. 8
      golang_learn/data_prattern/mediator/main_test.go
  9. 7
      golang_learn/data_prattern/mediator/说明.md
  10. 18
      golang_learn/data_prattern/prototype/main.go
  11. 15
      golang_learn/data_prattern/prototype/main_test.go
  12. 7
      golang_learn/data_prattern/prototype/说明.md
  13. 60
      golang_learn/data_prattern/state/main.go
  14. 11
      golang_learn/data_prattern/state/main_test.go
  15. 7
      golang_learn/data_prattern/state/说明.md
  16. 45
      golang_learn/data_prattern/strategy/main.go
  17. 14
      golang_learn/data_prattern/strategy/main_test.go
  18. 7
      golang_learn/data_prattern/strategy/说明.md
  19. 2
      golang_learn/go.mod

34
golang_learn/data_prattern/composite/main.go

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

25
golang_learn/data_prattern/composite/main_test.go

@ -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("+++++++++++")
}
}

7
golang_learn/data_prattern/composite/说明.md

@ -0,0 +1,7 @@
```go
```
* 组合模式
*

27
golang_learn/data_prattern/fly_weight/main.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
}

16
golang_learn/data_prattern/fly_weight/main_test.go

@ -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["不不不"])
}

7
golang_learn/data_prattern/fly_weight/说明.md

@ -0,0 +1,7 @@
```go
```
* 享元模式
*

60
golang_learn/data_prattern/mediator/main.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()
}
}

8
golang_learn/data_prattern/mediator/main_test.go

@ -0,0 +1,8 @@
package mediator
import "testing"
func TestMediator(t *testing.T) {
mediator := NewMediator()
mediator.Teb.Talk()
}

7
golang_learn/data_prattern/mediator/说明.md

@ -0,0 +1,7 @@
```go
```
* 中介模式
*

18
golang_learn/data_prattern/prototype/main.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}
}

15
golang_learn/data_prattern/prototype/main_test.go

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

7
golang_learn/data_prattern/prototype/说明.md

@ -0,0 +1,7 @@
```go
```
* 克隆模式
*

60
golang_learn/data_prattern/state/main.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("已经关闭")
}

11
golang_learn/data_prattern/state/main_test.go

@ -0,0 +1,11 @@
package state
import "testing"
func TestState(t *testing.T) {
machine := NewMachine()
machine.Off()
machine.On()
machine.On()
machine.Off()
}

7
golang_learn/data_prattern/state/说明.md

@ -0,0 +1,7 @@
```go
```
* 状态模式
*

45
golang_learn/data_prattern/strategy/main.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
}

14
golang_learn/data_prattern/strategy/main_test.go

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

7
golang_learn/data_prattern/strategy/说明.md

@ -0,0 +1,7 @@
```go
```
* 策略模式
*

2
golang_learn/go.mod

@ -8,7 +8,7 @@ require (
github.com/PuerkitoBio/goquery v1.7.1 github.com/PuerkitoBio/goquery v1.7.1
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
github.com/bitly/go-simplejson v0.5.0 github.com/bitly/go-simplejson v0.5.0
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869
github.com/go-ini/ini v1.62.0 github.com/go-ini/ini v1.62.0
github.com/go-sql-driver/mysql v1.6.0 github.com/go-sql-driver/mysql v1.6.0
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b

Loading…
Cancel
Save