Design Patterns - Memo Pattern

Memento Pattern - Memento Pattern

My understanding: two methods are declared in the originator, one creates a memorandum, and the other restores the state from the memorandum. The memorandum is the property that the originator needs to save, and the person in charge is responsible for storing and retrieving the memento.

Memento Pattern : Capturing the internal state of an object and saving this state outside of the object without breaking encapsulation, so that the object can be restored to the original saved state later. It is an object-behavioral pattern, aliased as Token .

       Originator: It is a common class, which can create a memorandum and store its current internal state, and can also use memorandum to restore its internal state. Generally, the class that needs to save the internal state is designed as an originator.

      ● Memorandum: Stores the internal state of the originator, and decides which internal states to save according to the originator. The design of the memorandum can generally refer to the design of the originator, and determine the attributes in the memorandum class according to actual needs. It should be noted that, apart from the originator itself and the responsible human, the memorandum object cannot be directly used by other classes, and the design of the originator will be different in different programming languages.

      ● Person in charge: The person in charge, also known as the manager, is responsible for keeping the memorandum, but cannot operate or check the contents of the memorandum. One or more memorandum objects can be stored in the responsible human, which is only responsible for storing the objects, not modifying the objects, nor needing to know the implementation details of the objects.


primary

public class Student {

    private String name;
    private Integer age;

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    //create a memo
    public Memento createMemento(){
        return new Memento(this.age);
    }

    // restore state from incoming memo
    public void restore(Memento memento){
        this.age = memento.getAge();
    }
}


memorandum

package memento;

public class Memento {

    private Integer age;

    public Memento(Integer age) {
        this.age = age;
    }

    public Integer getAge() {
        return age;
    }
}

principal

public class MementoCaretaker {

    //private Memento memento;

    private Stack stack = new Stack();

    public Memento getMemento() {
        //return memento;
        return (Memento) stack.pop();
    }

    public void setMemento(Memento memento) {
        //this.memento = memento;
        stack.push(memento);
    }
}

Client

public class Client {
    public static void main(String[] args) {
        Student student = new Student("喜大壮",18);
        MementoCaretaker mementoCaretaker = new MementoCaretaker();
        System.out.println("Initial state"+student.toString());

        mementoCaretaker.setMemento(student.createMemento()); //Save a memento

        student.setAge(19);
        System.out.println("Modify and save"+student.toString());
        mementoCaretaker.setMemento(student.createMemento());

        student.setAge(20);
        System.out.println("Only modify"+student.toString());

        student.restore(mementoCaretaker.getMemento());         //恢复
        System.out.println("After the first revocation"+student.toString());

        student.restore(mementoCaretaker.getMemento());
        System.out.println("After the second revocation"+student.toString());
    }
}


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326852158&siteId=291194637