18. The memo mode

Memento Pattern

A memorandum mode (Memento Pattern)

1. Basic Introduction

1) memo mode (Memento Pattern) without violating encapsulation, capture and the internal state of an object, and save the state outside the object. So that later you can restore the object to a previously saved state

2) memo is primarily used to record one state, or some of the data, when to do rollback, you can get the original data from the memo objects in recovery.

2. The principle of class diagram

1) originator: Object (need to save the state of the object)

2) Memento: memo objects, responsible for keeping good records, namely Originator internal state

3) Caretaker: Keeper object responsible for saving more than memo objects, using a collection management, improve efficiency

4) Note: If you want to save the state of a plurality of different times originator objects, may only need to HashMap <String, set>

3. code implementation

public class Client {
    public static void main(String[] args) {
        // 状态对象
        Originator originator = new Originator();
        // 守护者对象
        Caretaker caretaker = new Caretaker();

        //保存了当前的状态
        originator.setState(" 状态#1 攻击力 100 ");
        caretaker.add(originator.saveStateMemento());
        
        originator.setState(" 状态#2 攻击力 80 ");
        caretaker.add(originator.saveStateMemento());

        System.out.println("当前的状态是 =" + originator.getState());
        
        //希望得到状态 1, 将 originator 恢复到状态1
        originator.getStateFromMemento(caretaker.get(0));
        System.out.println("恢复到状态1 , 当前的状态是");
        System.out.println("当前的状态是 =" + originator.getState());
    }
}
/**
 * 备忘录对象
 */
public class Memento {
    private String state;

    //构造器
    public Memento(String state) {
        super();
        this.state = state;
    }

    public String getState() {
        return state;
    }
}
public class Originator {
    private String state;//状态信息

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
    
    //编写一个方法,可以保存一个状态对象 Memento
    //因此编写一个方法,返回 Memento
    public Memento saveStateMemento() {
        return new Memento(state);
    }
    
    //通过备忘录对象,恢复状态
    public void getStateFromMemento(Memento memento) {
        state = memento.getState();
    }
}
public class Caretaker {
    //在List 集合中会有很多的备忘录对象
    private List<Memento> mementoList = new ArrayList<Memento>();
    
    public void add(Memento memento) {
        mementoList.add(memento);
    }
    
    //获取到第index个Originator 的 备忘录对象(即保存状态)
    public Memento get(int index) {
        return mementoList.get(index);
    }
}

Second, note

1) provides a mechanism to restore, we can return to a more convenient state history

2) to achieve a saving of package details, the user need not be concerned state

3) Applicable scenarios: 1 to play the game archive. 2.windows the CTRL + Z. 3. Database Transaction Manager

Guess you like

Origin www.cnblogs.com/chao-zjj/p/11335059.html