// 策略模式,策略组映射
class tactics{
constructor(){
// 加权映射关系
const levelMap = {
S: 10,
A: 8,
B: 6,
C: 4
}
// 组策略
this.scoreLevel = {
basicScore: 80,
S: function() {
return this.basicScore + levelMap.S
},
A: function() {
return this.basicScore + levelMap.A
},
B: function() {
return this.basicScore + levelMap.B
},
C: function() {
return this.basicScore + levelMap.C
},
}
}
// 调用策略模式
getScore(level) {
return this.scoreLevel[level] ? this.scoreLevel[level]() : 0
}
}
let tactics_s = new tactics()
console.log(
tactics_s.getScore('S'),
tactics_s.getScore('A'),
tactics_s.getScore('B'),
tactics_s.getScore('C'),
tactics_s.getScore('D')
)