前言:【模式总览】——————————by xingoo
模式意图
这个模式主要是想通过一个对象来记录对象的某种状态,这样有利于在其他需要的场合进行恢复。
该模式还有跟多可以扩展的地方,比如可以记录多个时间的状态,每个角色都有可以扩展的空间,完全看业务场景而定。
应用场景
1 保存对象某一时刻的状态
2 避免直接暴露接口,破坏封装性
模式结构
Originator 是备忘录的发起者,记录状态的对象
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 备忘录角色,通常用于保存某种状态
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 备忘录的负责人,负责在恰当的时机,进行状态的恢复
class Caretaker{
private Memento memento;
public Memento retrieveMemento(){
return this.memento;
}
public void saveMemento(Memento memento){
this.memento = memento;
}
}
全部代码
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());
}
}
运行结果
Current state = On
Current state = Off
生活中的设计模式
最近看了会 恶魔奶爸,挺扯淡的漫画。不过看到其中的女仆,让我想起了这种备忘录模式。
主人在有什么重要的事情时,都会交给女仆记着,规定的时间在提醒自己。
下面的主人就有一件很重要的事情,就是陪亲爱的小丽去看电影,于是他弄了一个笔记本,记录下了这个信息。女仆拿到笔记本,并在预先商量好的时间提醒主人。这里的笔记本就是上面的备忘录对象Memento,而这个模式中,主人就是备忘录的发起者,女仆是负责人。
这里涉及到的备忘录是属于【白箱】的,也就是说,备忘录中的信息,可以被发起人和负责人看到。还有一种是【黑箱】的,主要是用了一种内部类继承这个备忘录对象,这样外部的负责人就得不到真正备忘录中的具体信息。
下面看下具体的实现,主人的代码如下:
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 }
女仆的代码如下:
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 }
备忘录的代码如下:
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 }
全部代码:
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 }
运行结果
女仆写笔记
****主人正在...睡觉吃饭打豆豆!
女仆拿到笔记本
读笔记!
主人看到笔记,记起了 晚上6点,配小丽看电影
****主人正在...晚上6点,配小丽看电影