备忘录模式
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("读完全部成语");
}
}
三、备忘录模式的优点
•备忘录模式使用备忘录可以把原发者的内部状态保存起来,使得只有很“亲密的”的对象可以访问备忘录中的数据。
•备忘录模式强调了类设计单一责任原则,即将状态的刻画和保存分开。
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样以后就可将该对象恢复到原先保存的状态。
一 、 概述
备忘录模式是关于怎样保存对象状态的成熟模式,其关键是提供一个备忘录对象,该备忘录负责存储一个对象的状态,程序可以在磁盘或内存中保存这个备忘录,这样一来,程序就可以根据对象的备忘录将该对象恢复到备忘录中所存储的状态。
二、备忘录模式的结构与使用
模式的结构中包括三种角色:
•原发者(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("读完全部成语");
}
}
三、备忘录模式的优点
•备忘录模式使用备忘录可以把原发者的内部状态保存起来,使得只有很“亲密的”的对象可以访问备忘录中的数据。
•备忘录模式强调了类设计单一责任原则,即将状态的刻画和保存分开。
相关文章推荐
- Android开发环境搭建完全图解
- 内核大页实现与分析
- 空间索引网格大小无效的解决方法The spatial index grid size is invalid
- JQuery Tips(3)----关于$()包装集内元素的改变
- ARM裸机程序开发9(GPIO)
- web客户端ocx控件远程下载
- BattleLAN在win7中无法运行一按启动便自动消失
- 优秀开源软件归类
- VC2005调试时看不到局部变量的值
- 表达式树
- Connecting OVS Bridges with Patch Ports
- 1.输入一个数组长度,动态创建数组,所有元素随机生成,输出元素中的最大值;
- JQuery Tips(4)----一些关于提高JQuery性能的Tips
- apache环境Zf2要装的intl验证
- 基于V-ISA检测机制的华为AntiDDos解决方案
- 正则表达式
- 利用FireBug使JQuery的学习更加轻松愉快
- C++排序之选择排序(3)
- STM32的两种debug方式
- IDOC创建、发送、接收及程序代码[转]