设计模式一备忘录模式

备忘录模式

备忘录(Memento)模式的定义:
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,
以便以后当需要时能将该对象恢复到原先保存的状态。该模式又叫快照模式。

发起人(Originator)角色:
记录当前时刻的内部状态信息,提供创建备忘录和恢复备忘录数据的功能,
    实现其他业务功能,它可以访问备忘录里的所有信息。

备忘录(Memento)角色:
负责存储发起人的内部状态,在需要的时候提供这些内部状态给发起人。
管理者(Caretaker)角色:
对备忘录进行管理,提供保存与获取备忘录的功能,但其不能对备忘录的内容进行访问与修改。
  1 import java.util.ArrayList;
  2 
  3 public class Memento {
  4     private static Caretaker caretaker = new Caretaker();
  5     //当前备份位置
  6     private static int count = -1;
  7 
  8     public static void main(String[] args) {
  9         chess a = new chess("军", 1, 1);
 10         play(a, "车", 6, 3);
 11         play(a, "马", 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     //毁棋
 31     public static void undo(chess a) {
 32         a.recover(caretaker.getMemento(--count));
 33         a.show();
 34     }
 35 
 36     //取消毁弃
 37     public static void redo(chess a) {
 38         a.recover(caretaker.getMemento(++count));
 39         a.show();
 40     }
 41 }
 42 
 43 //当前状态,发起人
 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 = new ArrayList<>();
124 
125     //获取备忘录
126     public ChessMemento getMemento(int i) {
127         return arr.get(i);
128     }
129 
130     //保存备忘录
131     public void save(ChessMemento ch) {
132         arr.add(ch);
133     }
134 }
 

猜你喜欢

转载自www.cnblogs.com/loveer/p/11279625.html