您的位置:首页 > 其它

备忘录模式

2013-09-25 09:40 381 查看
备忘录模式(别名:标记)

在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样以后就可将该对象恢复到原先保存的状态。

一 、 概述

备忘录模式是关于怎样保存对象状态的成熟模式,其关键是提供一个备忘录对象,该备忘录负责存储一个对象的状态,程序可以在磁盘或内存中保存这个备忘录,这样一来,程序就可以根据对象的备忘录将该对象恢复到备忘录中所存储的状态。

二、备忘录模式的结构与使用

模式的结构中包括三种角色:

•原发者(Originator)

•备忘录(Memento)

负责人(Caretaker)



1.原发者(Originator):
ReadPhrase.java

package tom.jiafei;

import java.io.*;

public class ReadPhrase {

long readPosition; File
file;

RandomAccessFile in;
String phrase=null;

public ReadPhrase(File
file){

this.file=file;

try{

in=new
RandomAccessFile(file,"r");

}

catch(IOException exp){ }

}

public Memento createMemento(){

Memento mem=new Memento();

mem.setPositionState(readPosition);

return mem;

}

public void restoreFromMemento(Memento
mem){

readPosition=mem.getPositionState();

}

public String readLine(){

try{ in.seek(readPosition);

phrase=in.readLine();

if(phrase!=null){

byte b[]=
phrase.getBytes("iso-8859-1");

phrase=newString(b);

}

readPosition=in.getFilePointer();

}

catch(IOException exp){}

return phrase;

}

public void closeRead(){

try{ in.close();

}

catch(IOException exp){ }

}

}

2.备忘录(Memento):
Memento.java

package tom.jiafei;

public class Mementoimplements
java.io.Serializable{

private longstate;

void setPositionState(long state){

this.state=state;

}

long getPositionState(){

returnstate;

}

}

3.负责人(Caretaker):Caretaker.java

import tom.jiafei.*;

import java.io.*;

public classCaretaker{

File file;

private Memento
memento=null;

Caretaker(){

file=newFile("saveObject.txt");

}

public Memento
getMemento(){

if(file.exists()) {

try{

FileInputStream
in=new FileInputStream("saveObject.txt");

ObjectInputStream
inObject=new
ObjectInputStream(in);

memento=(Memento)inObject.readObject();

}

catch(Exception exp){}

}

return memento;

}

public void saveMemento(Memento
memento){

try{
FileOutputStream out=new
FileOutputStream("saveObject.txt");

ObjectOutputStream
outObject=new
ObjectOutputStream(out);

outObject.writeObject(memento);

}

catch(Exception exp){}

}

}

4.应用:
Application.java _1

import tom.jiafei.*;

import java.util.Scanner;

import java.io.*;

public classApplication{

public static void main(String
args[]) {

Scanner reader=new Scanner(System.in);

ReadPhrase
readPhrase=new
ReadPhrase(newFile("phrase.txt"));

File favorPhrase=newFile("favorPhrase.txt");

RandomAccessFile out=null;

try{ out=new
RandomAccessFile(favorPhrase,"rw");

}

catch(IOException exp){}

System.out.println("是否从上次读取的位置继续读取成语(输入y或n)");

String answer=reader.nextLine();

if(answer.startsWith("y")||answer.startsWith("Y")){

Caretaker
caretaker=newCaretaker(); //创建负责人

Memento
memento=caretaker.getMemento();
//得到备忘录

if(memento!=null)

readPhrase.restoreFromMemento(memento); //使用备忘录恢复状态

}

String phrase=null;

while((phrase=readPhrase.readLine())!=null){

System.out.println(phrase);

System.out.println("是否将该成语保存到"+favorPhrase.getName());

answer=reader.nextLine();

4.应用:
Application.java _2

if(answer.startsWith("y")||answer.startsWith("Y")){

try{
out.seek(favorPhrase.length());

byte [] b=phrase.getBytes();

out.write(b);

out.writeChar('\n');

}

catch(IOException exp){}

}

System.out.println("是否继续读取成语?(输入y或n)");

answer=reader.nextLine();

if(answer.startsWith("y")||answer.startsWith("Y"))

continue;

else{

readPhrase.closeRead();

Caretaker
caretaker=newCaretaker(); //创建负责人

caretaker.saveMemento(readPhrase.createMemento());//保存备忘录

try{
out.close();

}

catch(IOException exp){}

System.exit(0);

}

}

System.out.println("读完全部成语");

}

}

三、备忘录模式的优点

•备忘录模式使用备忘录可以把原发者的内部状态保存起来,使得只有很“亲密的”的对象可以访问备忘录中的数据。
•备忘录模式强调了类设计单一责任原则,即将状态的刻画和保存分开。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: