状态模式
状态模式:一个对象有状态变化,每次状态变化都会触发一个逻辑,不能总是用 if...else 来控制
比如红绿灯:
// 状态(红灯,绿灯 黄灯)
class State {
constructor(color){
this.color = color
}
// 设置状态
handle(context){
console.log(`turn to ${this.color} light`);
context.setState(this)
}
}
// 主体
class Context {
constructor(){
this.state = null
}
// 获取状态
getState(){
return this.state
}
setState(state){
this.state = state
}
}
// 测试
let context = new Context()
let green = new State('green')
let yellow = new State('yellow')
let red = new State('red')
// 绿灯
green.handle(context) // 让{color: 'green'}赋予context的state
console.log(context.getState())
// 黄灯亮了
yellow.handle(context);
console.log(context.getState())
// 红灯亮了
red.handle(context);
console.log(context.getState())
设计原则验证
将状态对象和主体对象分离,状态的变化逻辑单独处理
符合开放封闭原则