策略模式

  • 定义一组算法、将每个算法都封装,并且使它们之间可以互换
  • 用于替换 多个ifelseswitch

代码实现

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
class  Location {
position: string
constructor(position: string){
this.position = position
}
}

class Stratege {
locations: Location [] = []
constructor(...locations){
this.locations = locations
console.log('路线经过了')
this.location.forEach(el=>{
console.log(el.position+ ',')
})
}
}

class Move {
start: Location
end: Location
stratege: Stratege

constructor(){
this.start = new Location('1 1')
this.end = new Location('0 0')
const sea = new Location('1 0')
const land = new Location('1 0')
this.stratege = new Stratege(this.start, sea, this.end)
}
}

参考链接