1. 什么是“状态模式”?
状态模式:对象行为是基于状态来改变的。
内部的状态转化,导致了行为表现形式不同。
所以,用户在外面看起来,好像是修改了行为。
2. 优缺点
优点
封装了转化规则,对于大量分支语句,可以考虑使用状态类进一步封装。
每个状态都是确定的,所以对象行为是可控的。
缺点
状态模式的关键是将事物的状态都封装成单独的类,这个类的各种方法就是“此种状态对应的表现行为”。
因此,状态类会增加程序开销。
3. 代码实现
3.1 ES6 实现
在 JavaScript 中,可以直接用 JSON 对象来代替状态类。
下面代码展示的就是 FSM(有限状态机)里面有 3 种状态:download
、pause
、deleted
。
控制状态转化的代码也在其中。
DownLoad
类就是,常说的Context
对象,它的行为会随着状态的改变而改变。
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
| const FSM = (() => { let currenState = 'download' return { download: { click: () => { console.log('暂停下载') currenState = 'pause' }, del: () => { console.log('先暂停, 再删除') }, }, pause: { click: () => { console.log('继续下载') currenState = 'download' }, del: () => { console.log('删除任务') currenState = 'deleted' }, }, deleted: { click: () => { console.log('任务已删除, 请重新开始') }, del: () => { console.log('任务已删除') }, }, getState: () => currenState, } })()
class Download { constructor(fsm) { this.fsm = fsm }
handleClick() { const { fsm } = this fsm[fsm.getState()].click() }
hanldeDel() { const { fsm } = this fsm[fsm.getState()].del() } }
let download = new Download(FSM)
download.handleClick() download.handleClick() download.hanldeDel() download.handleClick() download.hanldeDel()
|
3.2 Python3 实现
python 的代码采用的是“面向对象”的编程,没有过度使用函数式的闭包写法(python 写起来也不难)。
因此,负责状态转化的类,专门拿出来单独封装。
其他 3 个状态类的状态,均由这个状态类来管理。
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
| class StateTransform: def __init__(self): self.__state = 'download' self.__states = ['download', 'pause', 'deleted']
def change(self, to_state): if (not to_state) or (to_state not in self.__states) : raise Exception('state is unvalid') self.__state = to_state
def get_state(self): return self.__state
class DownloadState: def __init__(self, transfomer): self.__state = 'download' self.__transfomer = transfomer
def click(self): print('暂停下载') self.__transfomer.change('pause')
def delete(self): print('先暂停, 再删除')
class PauseState: def __init__(self, transfomer): self.__state = 'pause' self.__transfomer = transfomer
def click(self): print('继续下载') self.__transfomer.change('download')
def delete(self): print('删除任务') self.__transfomer.change('deleted')
class DeletedState: def __init__(self, transfomer): self.__state = 'deleted' self.__transfomer = transfomer
def click(self): print('任务已删除, 请重新开始')
def delete(self): print('任务已删除')
class Download: def __init__(self): self.state_transformer = StateTransform() self.state_map = { 'download': DownloadState(self.state_transformer), 'pause': PauseState(self.state_transformer), 'deleted': DeletedState(self.state_transformer) }
def handle_click(self): state = self.state_transformer.get_state() self.state_map[state].click()
def handle_del(self): state = self.state_transformer.get_state() self.state_map[state].delete()
if __name__ == '__main__': download = Download() download.handle_click(); download.handle_click(); download.handle_del(); download.handle_click(); download.handle_del();
|
4. 参考