前言:【模式總覽】——————————by xingoohtml
這個模式主要是想經過一個對象來記錄對象的某種狀態,這樣有利於在其餘須要的場合進行恢復。設計模式
該模式還有跟多能夠擴展的地方,好比能夠記錄多個時間的狀態,每一個角色都有能夠擴展的空間,徹底看業務場景而定。ide
應用場景this
1 保存對象某一時刻的狀態spa
2 避免直接暴露接口,破壞封裝性設計
Originator 是備忘錄的發起者,記錄狀態的對象rest
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 備忘錄角色,一般用於保存某種狀態code
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 備忘錄的負責人,負責在恰當的時機,進行狀態的恢復htm
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點,配小麗看電影