首页 > 数据库 > 数据库开发技术 > 行为型设计模式(三):状态模式 & 备忘录模式

状态模式允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它的类,将对象的行为包装在不同的状态类中,对象在运行时根据内部状态的改变而改变它的行为。
封装了转换规则:状态模式将每个状态的行为封装在一个类中,使得转换规则更加清晰,易于理解和维护。
减少条件语句:状态模式减少了对象中大量的条件语句,提高了代码的可读性和可维护性。
状态切换更加灵活:状态模式使得状态切换变得更加灵活,可以在运行时动态地改变对象的状态。
设计实现一个电梯状态:开门、关门、上升、下降等
// 状态接口interface ElevatorState {void open();void close();void up();void down();}// 具体状态1:开门class OpenState implements ElevatorState {public void open() {System.out.println("The door is already open.");}public void close() {System.out.println("Closing the door.");}public void up() {System.out.println("Cannot go up while the door is open.");}public void down() {System.out.println("Cannot go down while the door is open.");}}// 具体状态2:关门class CloseState implements ElevatorState {public void open() {System.out.println("Opening the door.");}public void close() {System.out.println("The door is already closed.");}public void up() {System.out.println("Going up.");}public void down() {System.out.println("Going down.");}}// 上下文类,维护当前状态class ElevatorContext {private ElevatorState currentState;// 设置当前状态void setCurrentState(ElevatorState currentState) {this.currentState = currentState;}// 调用状态的方法void open() {currentState.open();}void close() {currentState.close();}void up() {currentState.up();}void down() {currentState.down();}}// 客户端代码public class Client {public static void main(String[] args) {ElevatorContext elevator = new ElevatorContext();// 初始状态为关门elevator.setCurrentState(new CloseState());// 执行操作elevator.open();elevator.up();elevator.close();elevator.down();}}
状态模式引入了多个状态类,有可能增加系统中类的数量。
如果状态切换的逻辑比较复杂,会增加状态模式的复杂度。
如果实际业务场景中,存在相似的状态行为,建议考虑合并相似行为的状态,减少状态类的数量。
尽量简化状态切换的逻辑,保持状态模式的清晰性。
备忘录模式允许对象在不暴露内部状态的情况下保存和恢复状态,通过将对象的状态保存到备忘录对象中,以便后续需要时恢复到该状态。
封装了对象状态:备忘录模式将对象的状态封装到备忘录中,使得对象的状态对外部是不可见的。
支持撤销和恢复:备忘录模式支持将对象恢复到之前的状态,从而实现撤销的功能。
设计实现一个文本编辑器,用户可以输入文本并且可以随时保存编辑器的状态
// 备忘录类class EditorMemento {private final String content;EditorMemento(String content) {this.content = content;}String getContent() {return content;}}// 原发器类class TextEditor {private StringBuilder content = new StringBuilder();// 创建备忘录EditorMemento createMemento() {return new EditorMemento(content.toString());}// 恢复备忘录void restoreMemento(EditorMemento memento) {this.content = new StringBuilder(memento.getContent());}// 修改文本内容void addText(String text) {content.append(text);}// 获取当前文本内容String getContent() {return content.toString();}}// 负责人类class EditorHistory {private Stack<EditorMemento> history = new Stack<>();// 将备忘录保存到历史记录中void save(EditorMemento memento) {history.push(memento);}// 从历史记录中获取最近的备忘录EditorMemento undo() {if (!history.isEmpty()) {return history.pop();}return null;}}// 客户端代码public class Client {public static void main(String[] args) {TextEditor editor = new TextEditor();EditorHistory history = new EditorHistory();// 输入文本editor.addText("Hello, ");System.out.println("Current Content: " + editor.getContent());// 保存备忘录history.save(editor.createMemento());// 输入更多文本editor.addText("World!");System.out.println("Current Content: " + editor.getContent());// 撤销操作,恢复到之前的状态editor.restoreMemento(history.undo());System.out.println("After Undo: " + editor.getContent());}}
如果备忘录对象占用大量内存,可能会导致资源消耗较大。
如果原发器类的内部状态包含对其他对象的引用,备忘录模式无法完全保护这些对象的封装性。
精简备忘录:如果备忘录对象占用大量内存,可以考虑设计更加精简的备忘录对象,只保存必要的状态。
深拷贝:如果原发器类的内部状态包含对其他对象的引用,可以使用深拷贝技术来复制这些对象,保护对象的封装性。
原文作者:灸哥漫谈;如有侵权,请联系管理员删除。