-
Notifications
You must be signed in to change notification settings - Fork 46
/
main.go
84 lines (70 loc) · 1.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 状态模式 state pattern.
// 与策略模式在一些场景可以通用,但策略模式倾向于调用者根据情况手动改变内部策略以切换算法,
// 状态模式倾向于由Context内部自行管理状态,只需要设定初始状态即可,不需要手动切换.
//
// 以下是以跷跷板seesaw为例,分为[左侧高LeftState|右侧高RightState]
package main
import (
"math/rand"
"time"
)
// state
type SeesawState interface {
LiftLeftSide(S *Seesaw)
LiftRightSide(S *Seesaw)
}
type LeftState struct {}
func (LeftState) LiftLeftSide(S *Seesaw) {
println("↑LEFT > left side wads already lifted")
}
func (l LeftState) LiftRightSide(S *Seesaw) {
println("RIGHT↑ > lift right side")
S.State = RightState{}
}
type RightState struct {}
func (r RightState) LiftLeftSide(S *Seesaw) {
println("↑LEFT > lift left side")
S.State = LeftState{}
}
func (RightState) LiftRightSide(S *Seesaw) {
println("RIGHT↑ > right side wads already lifted")
}
// context
type Seesaw struct {
State SeesawState
}
func (s *Seesaw)MakeLeftUp(){
s.State.LiftLeftSide(s)
}
func (s *Seesaw)MakeRightUp(){
s.State.LiftRightSide(s)
}
func main(){
// init left
seesaw := &Seesaw{
State:LeftState{},
}
rand.Seed(time.Now().UnixNano())
for i:=0 ;i<10 ;i++{
if rand.Intn(2) == 1{
// ▄▃▂
seesaw.MakeLeftUp()
}else{
// ▂▃▄
seesaw.MakeRightUp()
}
}
/*
output:
RIGHT↑ > lift right side
RIGHT↑ > right side wads already lifted
RIGHT↑ > right side wads already lifted
↑LEFT > lift left side
↑LEFT > left side wads already lifted
↑LEFT > left side wads already lifted
↑LEFT > left side wads already lifted
RIGHT↑ > lift right side
RIGHT↑ > right side wads already lifted
↑LEFT > lift left side
*/
}