第九章节 - 状态模式
状态模式 - 章节
概念
. 本地化状态行为
. 状态对象
. Separates What from Where
. OCP
. Examples:
- None!
- JSF
- Iterator
设计注意事项
Abstract Class / Interface
Class based
Context unaware
Context, State, ConcreteState
UML图
例子:if/else
练习:状态模式
状态
ConcreteState
Conditionnal Logic
陷阱
▪ Know your States
▪ More classes
▪ 让逻辑脱离上下文
▪ State change triggers
对比
状态模式
▪ Interface based
▪ 跃迁
▪ Class per State
策略模式
▪ Interface based
▪ 算法是独立的
▪ Class per Algorithm
状态模式总结
• 简化圈复杂度
•增加额外的州变得更容易
•更多的类
• Similar implementation to Strategy