20. Behavior-Memorandum Model (Memento)

Keywords: Programming

Memento Pattern: Capture the internal state of an object without destroying the encapsulation and save it outside the object so that it can be restored to its original state in the future. It is an object behavioral pattern, nicknamed Token.

The memorandum model includes the following roles:

Originator: It is a common class. It can create a memo and store its current internal state. It can also use the memo to restore its internal state. Generally, classes that need to save the internal state are designed as the primary.
Memento: Store the internal state of the primary and decide which internal state to save according to the primary. The design of the memo can refer to the design of the primary device and determine the attributes of the memo class according to the actual needs. It should be noted that besides the primary device itself and the responsible human, the memorandum object can not be directly used by other classes, and the design mechanism of the primary device will be different in different programming languages.
Caretaker (person in charge): The person in charge, also known as the manager, is responsible for keeping the memo, but can not operate or check the contents of the memo. One or more memo objects can be stored in the responsible human. It is only responsible for storing objects, but it can not modify objects, and it does not need to know the implementation details of objects.

1. Main Advantages

(1) It provides an implementation mechanism for state recovery, which enables users to easily go back to a specific historical step. When a new state is invalid or has problems, a temporary stored memorandum can be used to restore the state.
(2) Memorandum implements encapsulation of information. A memorandum object is a representation of the state of the primary object and will not be changed by other codes. The state of the primary device is preserved in the memo. The memo object can be revoked many times by using lists, stacks and other collections to store the memo object.

2. Main shortcomings

The resource consumption is too large. If there are too many member variables of the originator class to be saved, it will inevitably need to occupy a large amount of storage space. Every time the state of the object is saved, it will need to consume a certain amount of system resources.

3. Applicable Scenario

(1) Save all or part of the state of an object at a certain time, so that it can restore to the previous state when needed later, and realize the revocation operation.
(2) To prevent external objects from destroying the encapsulation of an object's historical state, and to avoid exposing the details of the realization of the object's historical state to external objects.

Memorandum Mode in Heroic Alliance (Spatio-temporal Rift):

/**
 * 2018/12/6
 * StateOriginator
 *
 * @author machuanpeng
 */
public class StateOriginator {

    private String time;
    
    private String event;
 
    public StateOriginator(){}
 
  // Create a memo object
    public Memento createMemento() {
    return new Memento(this);
    }
 
  // Restoring the state of the primary device according to the object of the memorandum
    public void restoreMemento(Memento memento) {
     this.time = memento.getTime();
        this.event = memento.getEvent();
    }
}
/**
 * 2018/12/6
 * Memento
 *
 * @author machuanpeng
 */
public class Memento {  
  
    private String time;
    
    private String event;
      
    public Memento(StateOriginator originator) {  
     this.time = originator.getTime();
        this.event = originator.getEvent(); 
    }  
}
/**
 * 2018/12/6
 * Caretaker
 *
 * @author machuanpeng
 */

public class Caretaker {
    
	private Memento memento;
 
	public Memento getMemento() {
		return this.memento;
	}
 
	public void setMemento(Memento memento) {
		this.memento=memento;
	}
}

Test:

public class Client {
	public static void main(String args[]) {
        // Create the original class  
        StateOriginator originator = new StateOriginator();  
  		originator.setTime("2018-12-10 17:07:00");
        originator.setEvent("Blue Fang is fighting a dragon");
        // Create a Memorandum  
        Memento memento = originator.createMemento();  
        Caretaker caretaker = new Caretaker();
        caretaker.setMemento(memento);
        System.out.println("The initialization state is:"+originator.getTime()+originator.getEvent()); 
        // Modify the state of the original class  
        originator.setTime("2018-12-10 17:10:00");
        originator.setEvent("Hong Fang is fighting Xiaolong");
        System.out.println("Modified status:"+originator.getTime()+originator.getEvent()); 
        // Return to the state of the original class  
        originator.restoreMemento(caretaker.getMemento());  
        System.out.println("The state after recovery:"+originator.getTime()+originator.getEvent());
	}
}

Posted by whatever on Thu, 10 Oct 2019 02:41:04 -0700