1.描述
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样就可以将对象恢复到原先保存的状态。
2.模式的使用
·原发者(Originator):需要在某个时刻保存状态的对象。
·备忘录(Memento):负责存储原发者状态的对象,创建备忘录的类和创建原发者的类在同一个包中,该类提供的访问数据方法都是友好方法。
·负责人(Caretaker):负责管理保存备忘录的对象。
3.使用情景
·必须保存一个对象在某一时刻部分或全部状态,以便在需要的时候回复到先前的状态。
·一个对象不想通过提供public权限让其他对象得到自己的内部状态。
4.优点
·可以保存原发者的内部状态。
5.UML图
6案例
原发者从一个文件中读取内容,可以随时保存期状态,以便在下次读取时从上次停止的地方继续读取。
1 package 备忘录模式; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import java.io.ObjectInputStream; 9 import java.io.ObjectOutputStream; 10 import java.io.RandomAccessFile; 11 import java.io.Serializable; 12 13 public class test1 { 14 15 public static void main(String[] args) { 16 17 } 18 19 } 20 21 /* 22 * 原发者 23 */ 24 class ReadPhrase{ 25 long readPosition;//读取的位置 26 File file; 27 RandomAccessFile in; 28 String phrase = null; 29 ReadPhrase(File file){ 30 this.file = file; 31 try{ 32 in = new RandomAccessFile(file, "r"); 33 }catch(IOException e){ 34 e.printStackTrace(); 35 } 36 } 37 38 public Memento CreateMemento(){ 39 Memento memento = new Memento(); 40 memento.setPositionState(readPosition); 41 return memento; 42 } 43 44 public void restoreFromMemento(Memento memento){ 45 readPosition = memento.getPositionState(); 46 } 47 48 public String readLine(){ 49 try { 50 in.seek(readPosition); 51 phrase = in.readLine(); 52 if(phrase != null){ 53 byte b[] = phrase.getBytes("iso-8859-1"); 54 phrase = new String(b); 55 } 56 readPosition = in.getFilePointer(); 57 } catch (IOException e) { 58 // TODO Auto-generated catch block 59 e.printStackTrace(); 60 } 61 return phrase; 62 } 63 64 public void closeRead(){ 65 try { 66 in.close(); 67 } catch (IOException e) { 68 // TODO Auto-generated catch block 69 e.printStackTrace(); 70 } 71 } 72 } 73 74 /* 75 * 备忘录 76 */ 77 class Memento implements Serializable{ 78 79 /** 80 * 81 */ 82 private static final long serialVersionUID = 1L; 83 private long state; 84 public void setPositionState(long state){ 85 this.state = state; 86 } 87 public long getPositionState(){ 88 return state; 89 } 90 } 91 92 /* 93 * 负责人 94 */ 95 class Caretaker{ 96 File file; 97 private Memento memento = null; 98 Caretaker(){ 99 file = new File(""); 100 } 101 public Memento getMenento(){ 102 if(file.exists()){ 103 try { 104 FileInputStream in = new FileInputStream(""); 105 ObjectInputStream inObject = new ObjectInputStream(in); 106 memento = (Memento)inObject.readObject(); 107 } catch (FileNotFoundException e) { 108 // TODO Auto-generated catch block 109 e.printStackTrace(); 110 } catch (IOException e) { 111 // TODO Auto-generated catch block 112 e.printStackTrace(); 113 } catch (ClassNotFoundException e) { 114 // TODO Auto-generated catch block 115 e.printStackTrace(); 116 } 117 } 118 return memento; 119 } 120 121 public void saveMemento(Memento menento){ 122 try { 123 FileOutputStream out = new FileOutputStream(""); 124 ObjectOutputStream outObject = new ObjectOutputStream(out); 125 outObject.writeObject(outObject); 126 } catch (FileNotFoundException e) { 127 // TODO Auto-generated catch block 128 e.printStackTrace(); 129 } catch (IOException e) { 130 // TODO Auto-generated catch block 131 e.printStackTrace(); 132 } 133 } 134 }
填上文件路径就能用。