使用 TOML 文件管理配置的简要指南
2026/9/13 23:20:59
英文定义(《设计模式:可复用面向对象软件的基础》)
Without violating encapsulation, capture and externalize an object,t internal state so that the object can be restored this state later.
中文翻译
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便以后可以将对象恢复到原先保存的状态。
理解
saveToMemento())restoreFromMemento())角色说明:
背景
以文件编辑器为例,简化业务,只有保存和撤销功能
设计
原发器
publicclassTextEditor{privateStringcontent;publicStringgetContent(){returncontent;}publicvoidsetContent(Stringcontent){this.content=content;}publicTextEditor(Stringcontent){this.content=content;}/** * @description 创建备忘 * @author bigHao * @date 2026/1/27 * @return designpattern.memento.pac.TextMemento 备忘录 **/publicTextMementocreateMemento(){returnnewTextMemento(content);}/** * @description 恢复备忘 * @author bigHao * @date 2026/1/27 * @param memento 备忘录 **/publicvoidrestoreMemento(TextMementomemento){this.content=memento.getContent();}}备忘录
publicclassTextMemento{privateStringcontent;// 包访问权限,仅对Originator开放状态访问TextMemento(Stringcontent){this.content=content;}StringgetContent(){returncontent;}}管理者
publicclassCaretaker{privatestaticfinalintMAX_SIZE=100;// 业务场景刚好使用stack 先进后出privateStack<TextMemento>history=newStack<>();publicvoidpush(TextMementomemento){// 限制下长度limitHistory();this.history.push(memento);}publicTextMementopop(){returnthis.history.pop();}privatevoidlimitHistory(){if(history.size()>MAX_SIZE){history.removeFirst();}}}测试
publicclassClient{staticvoidmain(){// 创建Stringcontent="备忘录模式笔记";TextEditortextEditor=newTextEditor(content);printInfo(textEditor);Caretakercaretaker=newCaretaker();// 安全考虑,TextMemento是包访问权限,和TextEditor同包,其它地方无法直接创建// new TextMemento(""); 会报错// 保存1caretaker.push(textEditor.createMemento());content+="包括三个角色,分别是原发器";textEditor.setContent(content);caretaker.push(textEditor.createMemento());printInfo(textEditor);// 保存2content+="、备忘录";textEditor.setContent(content);caretaker.push(textEditor.createMemento());printInfo(textEditor);// 保存3content+="和管理者";textEditor.setContent(content);caretaker.push(textEditor.createMemento());printInfo(textEditor);// 编辑content+=" 功能分别是";textEditor.setContent(content);printInfo(textEditor);// 撤销1System.out.println("===撤销===");textEditor.restoreMemento(caretaker.pop());printInfo(textEditor);// 撤销2System.out.println("===撤销===");textEditor.restoreMemento(caretaker.pop());printInfo(textEditor);}privatestaticvoidprintInfo(TextEditortextEditor){System.out.println("当前内容: "+textEditor.getContent());}}输出
当前内容: 备忘录模式笔记 当前内容: 备忘录模式笔记包括三个角色,分别是原发器 当前内容: 备忘录模式笔记包括三个角色,分别是原发器、备忘录 当前内容: 备忘录模式笔记包括三个角色,分别是原发器、备忘录和管理者 当前内容: 备忘录模式笔记包括三个角色,分别是原发器、备忘录和管理者 功能分别是 ===撤销=== 当前内容: 备忘录模式笔记包括三个角色,分别是原发器、备忘录和管理者 ===撤销=== 当前内容: 备忘录模式笔记包括三个角色,分别是原发器、备忘录高内聚低耦合
复用性
可读性
维护性
稳定性
资源消耗
设计复杂度
状态同步
代码位置:javax.swing.undo.UndoManager
角色分析:
// Originator:各种Document类(如PlainDocument)publicclassPlainDocumentextendsAbstractDocument{// 创建UndoableEdit(备忘录)protectedUndoableEditcreateUndoableEdit(ElementChangeec){returnnewDocumentUndoableEdit(ec);}}// Memento:UndoableEdit接口及其实现publicinterfaceUndoableEdit{voidundo()throwsCannotUndoException;voidredo()throwsCannotRedoException;booleancanUndo();booleancanRedo();}// Caretaker:UndoManager类publicclassUndoManagerextendsCompoundEditimplementsUndoableEditListener{privateVector<UndoableEdit>edits;// 存储备忘录privateintindexOfNextAdd;// 下一个添加位置publicvoidundo()throwsCannotUndoException{// 执行撤销操作UndoableEditedit=edits.elementAt(--indexOfNextAdd);edit.undo();}publicvoidredo()throwsCannotRedoException{// 执行重做操作UndoableEditedit=edits.elementAt(indexOfNextAdd);edit.redo();indexOfNextAdd++;}}参考: