[Design Mode] - Memento Memento Pattern

  Preface: [ Mode Overview ] ---------- by xingoo

  Intention mode

  This mode is mainly want to record a certain state of the object through an object, it is a good recovery on other occasions need.

  The model also can be extended with many places, such as the status can be recorded multiple times, each character has space for expansion, totally business on the scene.

  Scenarios

  Save 1 state of the object at a time

  2 interface to avoid direct exposure, damage encapsulation

  Mode structure

  Originator is the initiator of the memorandum, the object recording state

class Originator{
    private String state;
    public Memento ceateMemento() {
        return new Memento(state);
    }
    public void restoreMemento(Memento memento) {
        this.state = memento.getState();
    }
    public String getState(){
        return this.state;
    }
    public void setState(String state){
        this.state = state;
        System.out.println("Current state = "+this.state);
    }
}

 

  Memento memorandum role, usually used to hold a certain state

class Memento{
    private String state;
    public Memento(String state) {
        this.state = state;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
}

 

  Caretaker head of the memorandum, in charge at the right time, to restore state

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

 

  All codes

package com.xingoo.test.design.memento;
class Originator{
    private String state;
    public Memento ceateMemento() {
        return new Memento(state);
    }
    public void restoreMemento(Memento memento) {
        this.state = memento.getState();
    }
    public String getState(){
        return this.state;
    }
    public void setState(String state){
        this.state = state;
        System.out.println("Current state = "+this.state);
    }
}
class Memento{
    private String state;
    public Memento(String state) {
        this.state = state;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
}
class Caretaker{
    private Memento memento;
    public Memento retrieveMemento(){
        return this.memento;
    }
    public void saveMemento(Memento memento){
        this.memento = memento;
    }
}
public class Client {
    private static Originator o = new Originator();
    private static Caretaker c = new Caretaker();
    public static void main(String[] args) {
        o.setState("On");
        //记录状态
        c.saveMemento(o.ceateMemento());
        //更改状态
        o.setState("Off");
        //更新状态
        o.restoreMemento(c.retrieveMemento());
    }
}
View Code

  operation result

Current state = On
Current state = Off

 

  Life Design Patterns

 

  Recently read will Beelzebub, very comic nonsense. But to see the maid of them, it reminds me of this memo mode.

  The owner when there is something important, will be handed over to the maid remember, at a specified time to remind ourselves.

  The following owners have a very important thing, it is to accompany dear Mary go to the movies, so he get a notebook to record this information. Maid to get a notebook, and remind the owner in advance to discuss good time. Here is the top of the memo notebook objects Memento, and this model, the owner is the initiator of the memorandum, the maid is in charge.

  The memorandum is involved here belong to the white box [], that is, information memorandum, the founder and head can be seen . There is also a black box [], mainly with an internal memo objects inherit this class, this external person in charge will not get real specific information memorandum .

  The following look specific implementation, the master code is as follows:

 1 class Master{
 2     private String info;
 3     public String getInfo() {
 4         return info;
 5     }
 6     public void setInfo(String info) {
 7         this.info = info;
 8     }
 9     public Note createNote(String info){
10         return new Note(info);
11     }
12     public void action(Note note){
13         this.info = note.getInfo();
14         System.out.println("主人看到笔记,记起了 "+ this.info);
15     }
16     public void toDo(){
17         System.out.println("****主人正在..."+info);
18     }
19 }

  Maid code is as follows:

 1 class Maid{
 2     private Note note;
 3     public Note readNote(){
 4         System.out.println("女仆拿到笔记本");
 5         return this.note;
 6     }
 7     public void writeNote(Note note){
 8         System.out.println("女仆写笔记");
 9         this.note = note;
10     }
11 }

  Memorandum code is as follows:

 1 class Note{
 2     private String info;
 3     public Note(String info) {
 4         this.info = info;
 5     }
 6     public void setInfo(String info){
 7         this.info = info;
 8         System.out.println("写笔记!");
 9     }
10     public String getInfo(){
11         System.out.println("读笔记!");
12         return info;
13     }
14 }

  All Code:

 1 package com.xingoo.test.design.memento;
 2 class Note{
 3     private String info;
 4     public Note(String info) {
 5         this.info = info;
 6     }
 7     public void setInfo(String info){
 8         this.info = info;
 9         System.out.println("写笔记!");
10     }
11     public String getInfo(){
12         System.out.println("读笔记!");
13         return info;
14     }
15 }
16 class Master{
17     private String info;
18     public String getInfo() {
19         return info;
20     }
21     public void setInfo(String info) {
22         this.info = info;
23     }
24     public Note createNote(String info){
25         return new Note(info);
26     }
27     public void action(Note note){
28         this.info = note.getInfo();
29         System.out.println("主人看到笔记,记起了 "+ this.info);
30     }
31     public void toDo(){
32         System.out.println("****主人正在..."+info);
33     }
34 }
35 class Maid{
36     private Note note;
37     public Note readNote(){
38         System.out.println("女仆拿到笔记本");
39         return this.note;
40     }
41     public void writeNote(Note note){
42         System.out.println("女仆写笔记");
43         this.note = note;
44     }
45 }
46 public class LifeWithMaid {
47     public static void main(String[] args) {
48         Master master = new Master();
49         Maid maid = new Maid();
50         //主人想起了要做的事情
51         maid.writeNote(master.createNote("晚上6点,配小丽看电影"));
52         //主人忙其他的事情
53         master.setInfo("睡觉吃饭打豆豆!");
54         master.toDo();//主人正在做什么?
55         //时间到了,女仆提醒主人
56         master.action(maid.readNote());
57         master.toDo();//主人正在做什么?
58     }
59 }
View Code

  operation result

女仆写笔记
****主人正在...睡觉吃饭打豆豆!
女仆拿到笔记本
读笔记!
主人看到笔记,记起了 晚上6点,配小丽看电影
****主人正在...晚上6点,配小丽看电影

 

Reproduced in: https: //my.oschina.net/u/204616/blog/545487

Guess you like

Origin blog.csdn.net/weixin_34186128/article/details/91989285