Memo mode of design mode

Keywords: Programming Java

scene

Protect the integrity of the saved state of these objects and the internal implementation from being exposed

  • You need to store the state externally on the premise of getting the state. So that at some point, you can restore your previous state.

role

There are three conventional roles:

  • Originator: primitive state class
  • Memo: a specific memo used to record the current status
  • Caretaker (Manager): manage memo information

For ease of understanding, the classes of this demo are:

  • State (Originator) primitive state class
  • Memo: Memo
  • MemoManager (CareTaker): memo Manager

Code

Memo

/**
 * Memorandum class
 *
 * Core, which is used to store the current state and cannot modify the historical state (that is, there is no set method)
 *
 * @author GaoYuan
 * @date 2018/11/11 8:25 a.m.
 */
public class Memo {

    /** state */
    private String state;

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

    public String getState() {
        return state;
    }
}

State

/**
 * State class
 * @author GaoYuan
 * @date 2018/11/11 8:25 a.m.
 */
public class State {

    private String state;

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    /** Get the current status and encapsulate it into memo class */
    public Memo getMemo(){
        return new Memo(this.state);
    }

    /** Get status from memo */
    public void getStateFromMemo(Memo memo){
        this.state = memo.getState();
    }
}

MemoManager

/**
 * Memorandum class
 * Used to store historical status
 * @author GaoYuan
 * @date 2018/11/11 8:25 a.m.
 */
public class MemoManager {

    private List<Memo> stateList = new ArrayList();

    public void add(Memo state){
        stateList.add(state);
    }

    public Memo get(int index){
        return stateList.get(index);
    }

}

Test class

/**
 * Use only MemoManage and State
 * Cannot use Memo
 * @author GaoYuan
 * @date 2018/11/11 9:16 a.m.
 */
public static void main(String[] args){
    State state = new State();
    // Initial state is 1
    state.setState("1");
    System.out.println("Status:" + state.getState());

    // Change status to 2
    state.setState("2");
    System.out.println("Status:" + state.getState());

    // Store current status
    MemoManager memoManager = new MemoManager();
    memoManager.add(state.getMemo());

    // Change status to 3
    state.setState("3");
    System.out.println("Status:" + state.getState());

    // Get status of storage
    state.getStateFromMemo(memoManager.get(0));
    System.out.println("Status:" + state.getState());
}

output

Status: 1
 Status: 2
 Status: 3
 Status: 2

Code cloud

https://gitee.com/gmarshal/foruo-learn-java/tree/master/src/main/java/com/foruo/learn/designmode/memo

Blog

https://my.oschina.net/gmarshal/blog/2875314

Welcome to my personal wechat subscription number: (it is said that this head portrait app is dedicated to apes)

Posted by omelin on Sat, 07 Dec 2019 19:08:52 -0800