1+ package com .anxpp .designpattern .memento .textEditor ;
2+
3+ import java .util .LinkedList ;
4+
5+ //文本编辑器
6+ public class TextEditor {
7+ public static void main (String [] args ){
8+ //使用这个文本编辑器
9+ MyTextEditor editor = new MyTextEditor ("这里是初始文本,可能为文件中读取的值。" );
10+ System .out .println ("开始修改文本:" );
11+ editor .append ("添加文字1" );
12+ editor .delWords (); //删除最后一个
13+ // editor.delWords(2); //删除最后2个 这两个方法是没有问题的,这里避免控制台输出太多,取消这两次修改
14+ // editor.delWords(1,5); //删除前面5个
15+ System .out .println ("开始恢复:" );
16+ for (int i =0 ;i <10 ;i ++) editor .recoverMemento ();//恢复大于实际修改的次数不会出错,只会将文本设为o初始化状态
17+ System .out .println ("开始重做:" );
18+ for (int i =0 ;i <10 ;i ++) editor .redo (); //重做大于实际恢复的次数不会出错,只会将文本设为最后状态
19+ System .out .println ("再次恢复:" );
20+ for (int i =0 ;i <10 ;i ++) editor .recoverMemento ();//恢复大于实际修改的次数不会出错,只会将文本设为o初始化状态
21+ System .out .println ("再次重做:" );
22+ for (int i =0 ;i <10 ;i ++) editor .redo (); //重做大于实际恢复的次数不会出错,只会将文本设为最后状态
23+ System .out .println ("再次恢复:" );
24+ for (int i =0 ;i <10 ;i ++) editor .recoverMemento ();//恢复大于实际修改的次数不会出错,只会将文本设为o初始化状态
25+ editor .append ("添加文字2" );
26+ System .out .println ("再次重做:" );
27+ for (int i =0 ;i <10 ;i ++) editor .redo (); //重做大于实际恢复的次数不会出错,只会将文本设为最后状态
28+ }
29+ }
30+ interface IMemento {}
31+ //发起人兼负责人
32+ class MyTextEditor {
33+ public StringBuffer text ;
34+ private LinkedList <IMemento > mementos ; //保存快照
35+ private LinkedList <IMemento > undos ; //保存撤销的操作
36+ public MyTextEditor (){
37+ this ("" );
38+ }
39+ public MyTextEditor (String defaultStr ){
40+ text = new StringBuffer (defaultStr );
41+ mementos = new LinkedList <IMemento >();
42+ undos = new LinkedList <IMemento >();
43+ print ();
44+ }
45+ public void clearHistory (){
46+ mementos .clear ();
47+ undos .clear ();
48+ }
49+ public void append (String appendStr ){
50+ if (appendStr ==null ||appendStr .length ()==0 ) return ;
51+ createMemento ();
52+ text .append (appendStr );
53+ print ();
54+ undos .clear ();
55+ }
56+ //删除最后一个
57+ public void delWords (){
58+ delWords (1 );
59+ }
60+ //删除最后n个
61+ public void delWords (int n ){
62+ if (n <1 ||n >text .length ()) return ;
63+ delWords (text .length ()-n +1 ,text .length ());
64+ }
65+ //删除中间start到end的字符,第一个文字为第一个(而不是0)
66+ public void delWords (int start ,int end ){
67+ if (start <1 || end >text .length ()+1 ) return ;
68+ createMemento ();
69+ text = text .delete (start -1 , end );
70+ print ();
71+ }
72+ public void reset (String text ){
73+ this .text = new StringBuffer (text );
74+ }
75+ //新的快照
76+ public void createMemento (){
77+ mementos .push (new Memento (this ));
78+ }
79+ //恢复状态
80+ public boolean recoverMemento (){
81+ Memento memento = (Memento ) mementos .poll ();
82+ if (memento ==null ) return false ;
83+ undos .push (new Memento (this ));
84+ reset (memento .state );
85+ print ();
86+ return true ;
87+ }
88+ //redo,redo的操作也可以恢复!
89+ public boolean redo (){
90+ Memento memento = (Memento ) undos .poll ();
91+ if (memento ==null ) return false ;
92+ createMemento ();
93+ reset (memento .state );
94+ print ();
95+ return true ;
96+ }
97+ //内部类实现备忘录
98+ private class Memento implements IMemento {
99+ private String state ;
100+ private Memento (MyTextEditor editor ){
101+ this .state = editor .text .toString ();
102+ }
103+ }
104+ void print (){
105+ System .out .println ("当前文本:" + text );
106+ }
107+ }
0 commit comments