备忘录模式

来源:互联网 发布:linux jenkins php 编辑:程序博客网 时间:2024/05/14 08:12

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

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

一 、 概述

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

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

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

原发者(Originator
备忘录(Memento
负责人(Caretaker) 

1原发者(Originator:ReadPhrase.java

package tom.jiafei;

import java.io.*;

public class ReadPhrase {

    long readPosition;        Filefile;

    RandomAccessFile in;      String phrase=null;

    public ReadPhrase(Filefile){

       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(Mementomem){

        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();       //创建负责人

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

     }

}

三、备忘录模式的优点 

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

原创粉丝点击