备忘录模式

备忘录模式(别名:标记)

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

一 、 概述

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

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

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

原发者( 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("是否从上次读取的位置继续读取成语(输入yn");

            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("是否继续读取成语?(输入yn)");

                 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("读完全部成语");

     }

}

三、备忘录模式的优点 

备忘录模式使用备忘录可以把原发者的内部状态保存起来,使得只有很 亲密的 的对象可以访问备忘录中的数据。
备忘录模式强调了类设计单一责任原则,即将状态的刻画和保存分开。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值