java design pattern: memo pattern

preface

Memo mode can record the internal state of an object. When the user regrets, it can undo the current operation and restore the data to its original state.

definition

Without destroying the encapsulation, capture the internal state of an object and save the state outside the object, so that the object can be restored to the original saved state when necessary in the future. This mode is also called snapshot mode.

advantage

Provides a mechanism to restore state. When users need it, they can easily restore the data to a historical state.
The encapsulation of internal state is realized. Except for the initiator who created it, no other object can access this status information.
Simplified the human. The initiator does not need to manage and save each backup of its internal status. All status information is saved in the memo and managed by the manager, which is in line with the principle of single responsibility.

shortcoming

Large resource consumption. If the internal state information to be saved is too much or too frequent, it will occupy a large amount of memory resources.

structure

  • Originator role: record the internal status information at the current time, provide the function of creating memos and recovering memo data, and realize other business functions. It can access all the information in memos.
  • Memo role: responsible for storing the internal status of the initiator and providing these internal status to the initiator when necessary.
  • Caretaker role: manages memos and provides the function of saving and obtaining memos, but it cannot access and modify the contents of memos.

realization

The most commonly used calculator in life has the function of memo. After the user completes the calculation, the software will automatically record the last few calculation results for the user. We can simulate the process of using the calculator and open the memo to view the records.

package com.rabbit;

/**
 * Memo originator, analog calculator, addition
 * Created by HASEE on 2018/4/29.
 */
public class Originator {

    private double num1;

    private double num2;

    //Create memo object
    public Memento createMemento() {
        return new Memento(num1, num2);
    }

    public Originator(double num1, double num2) {
        this.num1 = num1;
        this.num2 = num2;
        System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
    }

}
package com.rabbit;

/**
 * Memo, properties to save
 * Created by HASEE on 2018/4/29.
 */
public class Memento {

    private double num1;//First number of calculator

    private double num2;//Calculator second number

    private double result;//Calculation results

    public Memento(double num1, double num2) {
        this.num1 = num1;
        this.num2 = num2;
        this.result = num1 + num2;
    }

    public void show() {
        System.out.println(num1 + " + " + num2 + " = " + result);
    }

}
package com.rabbit;

import java.util.ArrayList;
import java.util.List;

/**
 * Memo Manager
 * Created by HASEE on 2018/4/29.
 */
public class Caretaker {

    private List<Memento> mementos;

    public boolean addMenento(Memento memento) {
        if (mementos == null) {
            mementos = new ArrayList<>();
        }
        return mementos.add(memento);
    }

    public List<Memento> getMementos() {
        return mementos;
    }

    public static Caretaker newInstance() {
        return new Caretaker();
    }
}
package com.rabbit;

import org.junit.Test;

import java.util.Random;

/**
 * Created by HASEE on 2018/4/29.
 */
public class Demo {

    @Test
    public void test() {
        Caretaker c = Caretaker.newInstance();
        //Using circular simulation, users use calculators to add
        Random ran = new Random(1000);
        for (int i = 0; i < 5; i++) {
            //User computing
            Originator o = new Originator(ran.nextDouble(), ran.nextDouble());
            //The calculator software backs up the user's calculations so that you can view the history
            c.addMenento(o.createMemento());
        }
        System.out.println("---------------------User browsing history---------------------");
        for (Memento m : c.getMementos()) {
            m.show();
        }
        System.out.println("---------------------User selects a record to view----------------------");
        c.getMementos().get(2).show();
    }

}

Keywords: Java

Added by yorktown on Mon, 31 Jan 2022 14:49:08 +0200