The story of Guanyin Ganquan Living Tree turned out to be a Java design pattern: memo pattern

definition

The memo mode is a behavioral mode of an object. The memo object is an object used to store a snapshot of the internal state of another object. The purpose of the memo mode is to capture and externalize the state of an object without destroying the encapsulation. Stored, so that the object can be restored to the stored state at an appropriate time in the future

intention

Under the premise of not destroying the encapsulation, capture the internal state of an object and save this state outside the object

Mainly solve the problem

Under the premise of not destroying the encapsulation, capture the internal state of an object and save this state outside the object, so that the object can be restored to the original state in the future

When to use

Many times we always need to record the internal state of an object. The purpose of this is to allow the user to cancel the uncertain or wrong operation and restore it to his original state, so that he has "regret medicine" to eat.

Pros and cons

advantage:

  • Sometimes the internal information of some initiator objects must be stored outside the initiator object, but it must be read by the initiator object itself. At this time, the memo mode can be used to shield the complicated internal information of the initiator from other objects. So as to maintain the boundaries of the package
  • When the status of the initiator role changes, the status may be invalid. At this time, the temporarily stored memo can be used to restore the status

Disadvantages:
Consume resources. If there are too many member variables of the class, it will inevitably take up relatively large resources, and each save will consume a certain amount of memory

structure

Insert picture description here
Roles involved:

  • Memento role:

    • Store the internal state of the Originator object; the memo can determine how many internal states of the Originator object to store according to the judgment of the originator object
    • The memo can protect its content from being read by any object other than the Originator object. The memo has two equivalent interfaces:
      • Narrow interface: The Caretaker object and any other objects except the initiator object, what you see is the narrow interface of the memo, this interface only allows it to pass the memo object to other objects
      • Wide interface: the initiator object can see a wide interface, this wide interface allows it to read all the data, in order to restore the internal state of the initiator object based on these data
  • Originator role:

    • Create a memo object with the current internal state
    • Use the memo object to store its internal state
  • Role of Caretaker:

    • Responsible for saving the memo object
    • Do not check the content of the memo object

White box realization

The state of the initiator role is stored in a place that everyone can see, so it is to destroy the encapsulation. The
Insert picture description here
source code is as follows:

public class Originator {
    
    

    private String state;

    /** 返回一个新的备忘录对象 */
    public Memento createMemento() {
    
    
        return new Memento(state);
    }

    /** 将发起人恢复到备忘录对象所记录的状态 */
    public void restoreMemento(Memento memento) {
    
    
        this.state = memento.getState();
    }

    /** 状态的取值方法 */
    public String getState() {
    
    
        return this.state;
    }

    /** 状态的赋值方法 */
    public void setState(String state) {
    
    
        this.state = state;
        System.out.println("当前状态:" + this.state);
    }
}
public class Memento {
    
    

    private String state;

    public Memento(String state) {
    
    
        this.state = state;
    }

    /** 状态的取值方法 */
    public String getState() {
    
    
        return this.state;
    }

    /** 状态的赋值方法 */
    public void setState(String state) {
    
    
        this.state = state;
    }
}
public class Caretaker {
    
    

    private Memento memento;

    /** 备忘录的取值方法 */
    public Memento getMemento() {
    
    
        return this.memento;
    }

    /** 备忘录的赋值方法 */
    public void setMemento(Memento memento) {
    
    
        this.memento = memento;
    }
}
public class Client {
    
    
    public static void main(String[] args) {
    
    
        Originator originator = new Originator();
        Caretaker caretaker = new Caretaker();
        //改变发起人对象的状态
        originator.setState("On");
        //创建备忘录对象,并将发起人对象的状态存储起来
        caretaker.setMemento(originator.createMemento());
        //修改发起人对象的状态
        originator.setState("Off");
        originator.restoreMemento(caretaker.getMemento());
        //恢复发起人对象的状态
        System.out.println("恢复状态:" + originator.getState());
    }
}

Black box implementation

Set the Memento class as an internal class of the Originator class, and provide an external identification interface MementoIF for the Caretaker class and other objects. The
Insert picture description here
source code is as follows:

public class Originator {
    
    

    private String state;

    public Originator() {
    
    
    }

    /** 返回一个新的备忘录对象 */
    public MementoIF createMemento() {
    
    
        return new Memento(this.state);
    }

    /** 将发起人恢复到备忘录对象所记录的状态 */
    public void restoreMemento(MementoIF memento) {
    
    
        Memento memento1 = (Memento) memento;
        this.state = memento1.getState();
    }

    /** 状态的取值方法 */
    public String getState() {
    
    
        return this.state;
    }

    /** 状态的赋值方法 */
    public void setState(String state) {
    
    
        this.state = state;
        System.out.println("当前状态:" + this.state);
    }

    /** 内部成员类,备忘录 */
    protected class Memento implements MementoIF {
    
    
        private String saveState;

        private Memento(String state) {
    
    
            this.saveState = state;
        }

        /** 状态的取值方法 */
        private String getState() {
    
    
            return this.saveState;
        }

        /** 状态的赋值方法 */
        private void setState(String state) {
    
    
            this.saveState = state;
        }
    }
}
public interface MementoIF {
    
    
}
public class Caretaker {
    
    

    private MementoIF memento;

    /** 备忘录的取值方法 */
    public MementoIF getMemento() {
    
    
        return this.memento;
    }

    /** 备忘录的赋值方法 */
    public void setMemento(MementoIF memento) {
    
    
        this.memento = memento;
    }
}
public class Client {
    
    
    private static Originator o = new Originator();
    private static Caretaker c = new Caretaker();
    public static void main(String[] args) {
    
    
        //改变发起人对象的状态
        o.setState("On");
        //创建备忘录对象,并将发起人对象的状态存储起来
        c.setMemento(o.createMemento());
        //修改发起人对象的状态
        o.setState("Off");
        o.restoreMemento(c.getMemento());
        //恢复发起人对象的状态
        System.out.println("恢复状态:" + o.getState());
    }
}

Multiple checkpoints

Store multiple states, or called multiple checkpoints

public class Originator {
    
    

    private Vector states;

    private int index;

    public Originator() {
    
    
        this.states = new Vector();
        this.index = 0;
    }

    /** 返回一个新的备忘录对象 */
    public Memento createMemento() {
    
    
        return new Memento(states, index);
    }

    /** 将发起人恢复到备忘录对象所记录的状态 */
    public void restoreMemento(Memento memento) {
    
    
        this.states = memento.getStates();
        this.index = memento.getIndex();
    }

    /** 状态的赋值方法 */
    public void setState(String state) {
    
    
        this.states.addElement(state);
        index++;
    }

    /** 打印出所有的状态 */
    public void printStates() {
    
    
        System.out.println("总的状态数量:" + index);
        for (Enumeration e = states.elements(); e.hasMoreElements();) {
    
    
            System.out.println(e.nextElement());
        }
    }
}
public class Memento {
    
    

    private Vector states;

    private int index;

    public Memento(Vector states, int index) {
    
    
        this.states = (Vector) states.clone();
        this.index = index;
    }

    /** 状态的取值方法 */
    public Vector getStates() {
    
    
        return states;
    }

    /** 检查点指数的取值方法 */
    public int getIndex() {
    
    
        return index;
    }
}
public class Caretaker {
    
    

    private Originator originator;
    private Vector vector = new Vector();
    private int current;

    public Caretaker(Originator originator) {
    
    
        this.originator = originator;
        this.current = 0;
    }

    /** 创建一个新的检查点 */
    public int createMemento() {
    
    
        Memento memento = originator.createMemento();
        vector.addElement(memento);
        return current++;
    }

    /** 将发起人恢复到某个检查点 */
    public void restoreMemento(int index) {
    
    
        Memento memento = (Memento) vector.elementAt(index);
        originator.restoreMemento(memento);
    }

    /** 将某个检查点删除 */
    public void removeMemento(int index) {
    
    
        vector.removeElementAt(index);
    }
}
public class Client {
    
    

    private static Originator o = new Originator();
    private static Caretaker c = new Caretaker(o);

    public static void main(String[] args) {
    
    
        //改变状态
        o.setState("state 0");
        //建立一个检查点
        c.createMemento();
        //改变状态
        o.setState("state 1");
        //建立一个检查点
        c.createMemento();
        //改变状态
        o.setState("state 2");
        //建立一个检查点
        c.createMemento();
        //改变状态
        o.setState("state 3");
        //建立一个检查点
        c.createMemento();

        //打印所有的检查点
        o.printStates();
        //恢复到第1个检查点
        System.out.println("恢复到第1个检查点");
        c.restoreMemento(1);
        o.printStates();
        //恢复到第3个检查点
        System.out.println("恢复到第2个检查点");
        c.restoreMemento(2);
        o.printStates();
    }
}

Insert picture description here

The story of Avalokitesvara's sweet spring living tree

Sun Dasheng protects the Tang Seng going west, passing by the Wuzhuang Temple of Longevity Mountain, and quarreling with Daotong, he becomes angry and knocks down the ginseng fruit tree; the great sage has to ask Guanyin Bodhisattva to save the ginseng fruit tree

Here, the state of the fruit tree is preserved in the nectar of Avalokitesvara. The bodhisattva can restore the state of the fruit tree from the nectar. The fruit tree is the initiator, the nectar is the memorandum role, and the bodhisattva is the person in charge.

public class SweetSpringWater {
    
    

    private String fruiterState;

    public SweetSpringWater(String fruiterState) {
    
    
        this.fruiterState = fruiterState;
    }

    /** 获取果树的状态 */
    public String getFruiterState() {
    
    
        return fruiterState;
    }

    /** 设置果树的状态 */
    public void setFruiterState(String fruiterState) {
    
    
        this.fruiterState = fruiterState;
    }
}
public class Fruiter {
    
    

    private String fruiterState;

    /** 返回一个新的备忘录对象 */
    public SweetSpringWater createMemento() {
    
    
        return new SweetSpringWater(fruiterState);
    }

    /** 将发起人恢复到备忘录对象所记录的状态 */
    public void restoreSweetSpringWater(SweetSpringWater springWater) {
    
    
        this.fruiterState = springWater.getFruiterState();
    }

    /** 果树状态的取值方法 */
    public String getState() {
    
    
        return this.fruiterState;
    }

    /** 果树状态态的赋值方法 */
    public void setState(String fruiterState) {
    
    
        this.fruiterState = fruiterState;
        System.out.println("当前果树状态:" + this.fruiterState);
    }
}
public class Goddess {
    
    

    private SweetSpringWater sweetSpringWater;

    /** 备忘录的取值方法 */
    public SweetSpringWater getSweetSpringWater() {
    
    
        return this.sweetSpringWater;
    }

    /** 备忘录的赋值方法 */
    public void setMemento(SweetSpringWater sweetSpringWater) {
    
    
        this.sweetSpringWater = sweetSpringWater;
    }
}
public class Client {
    
    
    public static void main(String[] args) {
    
    
        Fruiter f = new Fruiter();
        Goddess g = new Goddess();
        //改变发起人对象的状态
        f.setState("果树被孙悟空推倒了");
        //创建备忘录对象,并将发起人对象的状态存储起来
        g.setMemento(f.createMemento());
        //修改发起人对象的状态
        f.setState("观世音用甘泉恢复果树状态");
        //恢复发起人对象的状态
        System.out.println("恢复状态:" + f.getState());
    }
}

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_34365173/article/details/108454005