mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-21 20:36:01 +03:00
template pattern
This commit is contained in:
parent
f978e42036
commit
450074a3d6
36
behavioral/strategy/main.go
Normal file
36
behavioral/strategy/main.go
Normal file
@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
type Operator interface {
|
||||
Apply(int, int) int
|
||||
}
|
||||
|
||||
// 通过定义内部的Operator实现不同策略的切换
|
||||
type Operation struct {
|
||||
Operator Operator
|
||||
}
|
||||
|
||||
func (o *Operation) Operate(leftValue, rightValue int) int {
|
||||
return o.Operator.Apply(leftValue, rightValue)
|
||||
}
|
||||
|
||||
type Addition struct{}
|
||||
|
||||
func (Addition) Apply(lval, rval int) int {
|
||||
return lval + rval
|
||||
}
|
||||
|
||||
type Multiplication struct{}
|
||||
|
||||
func (Multiplication) Apply(lval, rval int) int {
|
||||
return lval * rval
|
||||
}
|
||||
|
||||
func main(){
|
||||
a ,b := 2 ,4
|
||||
op := &Operation{
|
||||
Addition{},
|
||||
}
|
||||
println(op.Operate(a ,b))// 6
|
||||
op.Operator = Multiplication{}
|
||||
println(op.Operate(a ,b))// 8
|
||||
}
|
102
behavioral/template/main.go
Normal file
102
behavioral/template/main.go
Normal file
@ -0,0 +1,102 @@
|
||||
// 存疑的部分.
|
||||
// 模板模式和策略模式在很多时候给人感觉基本可以划等号,事实上两者侧重的方向不同.
|
||||
// 策略模式倾向于通过改变结构的属性从而改变算法,模板模式则倾向于事先定义一个处理的流程,该流程是可以被整体替换的.
|
||||
// 个人认为应该保证模板模式下的结构体应当独立出来,每个流程单独被调用.
|
||||
// 感觉以下两种写法都有点别扭,待指正
|
||||
|
||||
package main
|
||||
|
||||
type Game interface {
|
||||
OnStart()
|
||||
OnEnd()
|
||||
}
|
||||
|
||||
type GameRunner struct {}
|
||||
|
||||
func (GameRunner)Go(g Game){
|
||||
g.OnStart()
|
||||
g.OnEnd()
|
||||
}
|
||||
|
||||
type FPSGame struct {}
|
||||
|
||||
func (f FPSGame)OnStart(){
|
||||
println("fps game start")
|
||||
}
|
||||
|
||||
func (f FPSGame)OnEnd(){
|
||||
println("fps game end")
|
||||
}
|
||||
|
||||
type RPGGame struct {}
|
||||
|
||||
func (RPGGame) OnStart() {
|
||||
println("rpg game start")
|
||||
}
|
||||
|
||||
func (RPGGame) OnEnd() {
|
||||
println("rpg game end")
|
||||
}
|
||||
|
||||
func main(){
|
||||
tpl := GameRunner{}
|
||||
tpl.Go(FPSGame{})
|
||||
tpl.Go(RPGGame{})
|
||||
// output:
|
||||
/*
|
||||
fps game start
|
||||
fps game end
|
||||
rpg game start
|
||||
rpg game end
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
// ______________________________Another_______________
|
||||
|
||||
//type Game struct {
|
||||
// OnStart func()
|
||||
// OnEnd func()
|
||||
//}
|
||||
//
|
||||
//func (g Game)Go(){
|
||||
// g.OnStart()
|
||||
// g.OnEnd()
|
||||
//}
|
||||
//
|
||||
//type FPSGame struct {Game}
|
||||
//
|
||||
//func NewFPSGame()FPSGame{
|
||||
// g := Game{
|
||||
// OnStart: func() {
|
||||
// fmt.Println("start fps")
|
||||
// time.Sleep(time.Second / 2)
|
||||
// },
|
||||
// OnEnd: func() {
|
||||
// fmt.Println("you are killed")
|
||||
// },
|
||||
// }
|
||||
// return FPSGame{g}
|
||||
//}
|
||||
//
|
||||
//type RPGGame struct {Game}
|
||||
//
|
||||
//func NewRPGGame()RPGGame{
|
||||
// g := Game{
|
||||
// OnStart: func() {
|
||||
// fmt.Println("start rpg")
|
||||
// time.Sleep(time.Second / 2)
|
||||
// },
|
||||
// OnEnd: func() {
|
||||
// fmt.Println("you win")
|
||||
// },
|
||||
// }
|
||||
// return RPGGame{g}
|
||||
//}
|
||||
//
|
||||
//func main(){
|
||||
// NewFPSGame().Go()
|
||||
// println()
|
||||
// NewRPGGame().Go()
|
||||
//}
|
||||
|
Loading…
Reference in New Issue
Block a user