Design Patterns (18) - Memo Pattern

memo mode

1. Definition

       Without breaking encapsulation, capture the internal state of an object and save that state outside the object. In this way, the modified object can be restored to its original saved state in the future.

2. Sample code

    Simulation system, an example of simulating the operation of process A

   

/*The memorandum interface of the object that simulates running process A is a narrow interface*/
public interface FlowAMockMemento{
   //empty interface
}

/*Simulate running process A, just a hint, referring to a specific process*/
public class FlowaMock{
   //process name
   private String flowName;
   //Refers to an intermediate result, which requires externally stored state data
   private int tempResult;
   //Refers to an intermediate state that requires externally stored state data
   private int tempState;
   //The constructor passes in the process name
   public FlowAMock(String flowName){
      this.flowName = flowName;
   }
   //Indicate the first stage of the process running
   public void runPhaseOne(){
      tempResult = 3;
      tempState = "PhaseOne";
   }
   //Indicate that the second half of the process should be run according to the scheme 1
   public void schema1(){
       this.tempState += ",Schema1";
       System.out.println(this.tempState + ":now run "+ tempResult);
       this.tempResult += 11;
   }
   //Indicate that the second half of the process should be run according to the second plan
   public void schema2(){
       this.tempState += ",Schema2";
       System.out.println(this.tempState + ":now run "+ tempResult);
       this.tempResult += 22;
   }
   //Create a memo object that holds the state of the originator object
   public FlowAMockMemento createMemento(){
      return new MementoImpl(this.tempResult,this.tempState);
   }    
   //Reset the state of the originator object to return it to the state recorded by the memo object
   public void setMemento(FlowAMockMemento memento){
       MementoImpl mementoImpl = (MementoImpl)memento;
       this.tempResult = memento.getTempResult();
       this.tempState = memento.getTempState();
   }  
   //Real memorandum object, implements the memorandum narrow interface, implements it as a private inner class, and does not allow external access
   private static class MementoImpl implements FlowAMockMemento{
      //Indicates to save an intermediate result
      private int tempResult;
      private String tempState;
      public MementoImpl(int tempResult,String tempState){
         this.tempResult = tempResult;      
         this.tempState = tempState;
      }
      public int getTempResult(){
          return tempResult;
      }
      public String getTempState(){
          return tempState;
      }
   }  
}

  

/* The memo object responsible for saving the object that simulates running process A */
public class FlowAMockMementoCareTaker{
    //Record the saved memo object
    private FlowAMockMemento memento = null;
    // save the memo object
    public void SaveMemento(FlowAMockMemento memento){
        this.memento = memento;
    }
    public FlowAMockMemento retriveMemento(){
        return memento;
    }
}

   

/*The client makes a simulation call*/
public class Client{
    public static void maiin(String args[]){
        //Create an object that simulates running process A
        FlowAMock mock = new FlowAMock("TestFlow");
        // run the first stage of the process (prepare data)
        mock.runPhaseOne();
        //create a manager
        FlowAMementoCareTaker careTaker = new FlowAMementoCareTaker();
        //Create a memo object for the object at this time
        FlowAMockMemento memento = mock.createMemento();
        careTaker.saveMemento(memento);
        //Run the second half of the process according to scheme 1
        mock.schema1();
        //Get the memo object from the manager object and restore the internal state
        mock.setMemento(careTaker.retriveMemento());
        //Run the second half of the process according to scheme 2
        mock.schema2();
    }
}

 

3. Practical application

       In the memento mode, the captured internal state is stored in the memo object, and the memo object is usually stored outside the originator object, that is, outside the object whose state is saved, usually in the manager object. In the memento mode, in order to control the access of objects, the concepts of narrow interface and wide interface appear. Narrow interface: The manager can only see the narrow interface of the memorandum. There is usually no method in the implementation of the narrow interface, just a type identifier. Narrow interfaces allow managers to pass memoranda only to other objects. Wide interface: The originator sees a wide interface that allows it to access all the data it needs, usually implemented as a private inner class inside the originator.

 

The essence of memo mode: saving and restoring internal state

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326386986&siteId=291194637