Design Patterns memo mode

Memo mode 

defined memo (Memento) mode:
without violating encapsulation, capture and the internal state of an object, and save the state of the object outside,
so that later when an object needs to be able to restore the original saved status. This mode is also called snapshot mode.

Originator (Originator) Role:
Record internal state information of current time, offer to create memos and memo data recovery, and
    achieve other business functions, it can access all the information in the memo.

Memorandum (Memento) roles:
the internal state is responsible for storing the promoters of these internal states when needed to sponsor.
Managers (Caretaker) role:
to manage the memorandum, provide preservation and access the memo function, but it can not be accessed with a modification of the contents of the memo.
  . 1  Import of java.util.ArrayList;
   2  
  . 3  public  class Memento {
   . 4      Private  static the Caretaker Caretaker = new new the Caretaker ();
   . 5      // current backup location 
  . 6      Private  static  int COUNT = -1 ;
   . 7  
  . 8      public  static  void main (String [] args) {
   . 9          Chess A = new new Chess ( "Jun",. 1,. 1 );
 10          Play (A, "car", 6, 3 );
 . 11          Play (A, "Ma", 3, 6 );
 12         play(a, "象", 7, 9);
 13         play(a, "卒", 2, 4);
 14         undo(a);
 15         undo(a);
 16         redo(a);
 17 
 18     }
 19 
 20     //下棋,同时保存
 21     public static void play(chess a, String name, int x, int y) {
 22         a.setName(name);
 23         a.setX(x);
 24         a.setY(y);
 25         caretaker.save(a.save());
 26         count++;
 27         a.show ();
 28      }
 29  
30      // destroyed chess 
31 is      public  static  void Use the undo (Chess A) {
 32          a.recover (caretaker.getMemento (- COUNT));
 33 is          a.show ();
 34 is      }
 35  
36      // cancel destroys 
37 [      public  static  void the redo (Chess A) {
 38 is          a.recover (caretaker.getMemento (++ COUNT));
 39          a.show ();
 40      }
 41 is  }
 42 is  
43 is  // current status, the initiator 
44 class chess {
 45     private String name;
 46     private int x;
 47     private int y;
 48 
 49     public String getName() {
 50         return name;
 51     }
 52 
 53     public void setName(String name) {
 54         this.name = name;
 55     }
 56 
 57     public int getX() {
 58         return x;
 59     }
 60 
 61     public void setX(int x) {
 62         this.x = x;
 63     }
 64 
 65     public int getY() {
 66         return y;
 67     }
 68 
 69     public void setY(int y) {
 70         this.y = y;
 71     }
 72 
 73     public chess(String name, int x, int y) {
 74         this.name = name;
 75         this.x = x;
 76         this.y = y;
 77     }
 78 
 79     //保存
 80     public ChessMemento save() {
 81         return new ChessMemento(name, x, y);
 82     }
 83 
 84     //恢复
 85     public void recover(ChessMemento ch) {
 86         name = ch.getName();
 87         x = ch.getX();
 88         y = ch.getY();
 89     }
 90 
 91     public void show() {
 92         System.out.println(name + "---" + x + "---" + y);
 93     }
 94 }
 95 
 96 //备忘录
 97 class ChessMemento {
 98     private String name;
 99     private int x;
100     private int y;
101 
102     public String getName() {
103         return name;
104     }
105 
106     public int getX() {
107         return x;
108     }
109 
110     public int getY() {
111         return y;
112     }
113 
114     public ChessMemento(String name, int x, int y) {
115         this.name = name;
116         this.x = x;
117         this.y = y;
118     }
119 }
120 
121 //备份管理者
122 class Caretaker {
123     private ArrayList<ChessMemento> arr = newThe ArrayList <> ();
 124  
125      // Get memo 
126      public ChessMemento getMemento ( int I) {
 127          return arr.get (I);
 128      }
 129  
130.      // save the memo 
131 is      public  void Save (ChessMemento CH) {
 132          ARR. the Add (CH);
 133      }
 134 }

 

 

Guess you like

Origin www.cnblogs.com/loveer/p/11279625.html