设计模式-备忘录模式(十八)

备忘录模式,又叫 快照模式 或 token模式
用意:用来存储另外一个对象内部状态的快照

备忘录角色的宽,窄接口
窄接口只允许把备忘录对象传递给其他对象,负责人对象(Caretaker)
宽接口允许读取所有的数据,以便根据这些数据恢复发起人对象的内部状态
(Originator对象)

java中可以用内部类的方法实现双接口,内部类的方法都设计成私有的。


上类图:


备忘录角色.png

  1. 创建备忘录和发起人角色
  • 代码示例:
package com.byedbl.memento; 
/**
 *  Our test Originator class with Memento class.
 *  Put 2 class into 1 file, so Memento member can be  visited by Originator 
 */
import java.util.LinkedList;

/**
 * 发起人角色,创建,保存备忘录角色
 */
public class TextOriginator  {
    private StringBuffer strbuf = new StringBuffer();
    private String state = "";
    private TextMemento memo;
    public TextOriginator() {
        memo = new TextMemento();
    }
    public void addText(String addStr) {
        state = addStr;
        strbuf.append(addStr);
    }
    public void createMemento() {
        memo.addMemo(state);
    }
    public void setMemento() {
        String tmp = memo.getState();
        if(tmp != null) {            
            int start = strbuf.length() - tmp.length();
            int end = strbuf.length();
            if(start >= 0) {
                strbuf.delete(start,  end);
            } else {
                System.out.println("Get Memo Error !");
            }
        }
    }
    public String getText() {
        return strbuf.toString();
    }
}

/**
 * 存储值,备忘录对象
 */
class TextMemento {
    //String state;
    int i = 0;
    LinkedList memoList;
    
    TextMemento() {
        //state = null;
        memoList = new LinkedList<>();
    }
    void addMemo(String state) {
        memoList.addLast(state);
    }
    String getState() {
        if(memoList.size() != 0) {
            String tmp = memoList.getLast();
            memoList.removeLast();
            return tmp;
        } else {
            return null;
        }      
    }
}
  1. 创建负责人角色
package com.byedbl.memento; /**
 *  A Caretaker as our test client
 */
import java.io.*;

/**
 * 负责人角色
 *
 */
public class CaretakerTest  {
    public static void main(String[] args) {
        TextOriginator originator = new TextOriginator();
        System.out.println("-----  Please edit text  -----");
        System.out.println("Usage: input \"undo\" to undo your edit");
        System.out.println("Usage: input \"end\" to end edit");
        try {
            BufferedReader in = new BufferedReader(
                new InputStreamReader(System.in));            

            String str = "";
            while(true) {
                str = in.readLine();
                if(str.equals("end")) {
                    System.out.println("-----  End edit  -----");
                    break;
                } else if(str.equals("undo")) {
                    originator.setMemento();
                } else { 
                    if(! str.equals("")) {
                        originator.addText(str);
                        originator.createMemento();
                    }
                    
                }
                System.out.println("--Input Text is :" + originator.getText());
            }
        } catch(IOException e) {
            System.out.println("I/O Error !");
            System.exit(1);
        }
    }
}

你可能感兴趣的:(设计模式-备忘录模式(十八))