備忘錄模式是一種軟體設計模式:在不破壞封閉的前提下,捕獲一個對象的內部狀態,並在該對象之外保存這個狀態。這樣以後就可將該對象恢復到原先保存的狀態。
基本介紹
- 中文名:備忘錄模式
- 外文名:Memento Pattern
- 亦稱:快照模式
- 屬於:行為模式
基本介紹
舉例說明
package com.zyh.designpattern.memento;public class PlayRole {private int vitality;private int aggressivity;private int defencivity;public PlayRole(int vitality, int aggressivity, int defencivity) {super();this.vitality = vitality;this.aggressivity = aggressivity;this.defencivity = defencivity;}public PlayRole() {}public int getVitality() {return vitality;}public void setVitality(int vitality) {this.vitality = vitality;}public int getAggressivity() {return aggressivity;}public void setAggressivity(int aggressivity) {this.aggressivity = aggressivity;}public int getDefencivity() {return defencivity;}public void setDefencivity(int defencivity) {this.defencivity = defencivity;}public RoleMemento createMemento() {RoleMemento memento = new RoleMemento();memento.setAggressivity(aggressivity);memento.setDefencivity(defencivity);memento.setVitality(vitality);return memento;}public void setMemento(RoleMemento memento) {this.aggressivity = memento.getAggressivity();this.defencivity = memento.getDefencivity();this.vitality = memento.getVitality();}public void showState() {System.out.println("攻擊力:" + this.aggressivity + "|防禦力:" + this.defencivity+ "|生命力:" + this.vitality);}}
package com.zyh.designpattern.memento;public class RoleMemento {private int vitality;private int aggressivity;private int defencivity;public int getVitality() {return vitality;}public void setVitality(int vitality) {this.vitality = vitality;}public int getAggressivity() {return aggressivity;}public void setAggressivity(int aggressivity) {this.aggressivity = aggressivity;}public int getDefencivity() {return defencivity;}public void setDefencivity(int defencivity) {this.defencivity = defencivity;}}
package com.zyh.designpattern.memento;public class Caretaker {RoleMemento memento;public RoleMemento getMemento() {return memento;}public void setMemento(RoleMemento memento) {this.memento = memento;}}
package com.zyh.designpattern.memento;public class Client {public static void main(String[] args) {// 測試程式// 新建角色PlayRole role = new PlayRole(100, 100, 100);// 新建管理者Caretaker taker = new Caretaker();// 角色初始狀態System.out.println("遊戲剛開始,角色各屬性:");role.showState();// 利用備忘錄模式保存當前狀態System.out.println("\n【保存遊戲狀態!】\n");taker.setMemento(role.createMemento());role.setAggressivity(20);role.setDefencivity(30);role.setVitality(0);// 大戰過後,角色能力值下降System.out.println("與Boss對戰後,角色各項能力已大大下降:");role.showState();// 恢復保存的角色狀態role.setMemento(taker.getMemento());System.out.println("\n【恢復保存的角色狀態!】");System.out.println("\n恢復后角色的當前狀態:");role.showState();}}
#include <iostream>#include <string>using namespace std;//備忘者類備份發起者的狀態class Memento{public: //構造的時候備份數據 Memento(string state):_state(state){}; string _state;};//發起者類備份自己的狀態class Originator{public: //建立備份 Memento* createBackups(){ return new Memento(_state); } //恢復備份內容 void restoreBackups(Memento* memento){ _state = memento->_state; } //顯示狀態 void show() { cout<<_state<<endl; } string _state;};//管理者類管理備份class Caretaker{public: //得到備份 Memento* getMemento(){ return _memento; } //設定備份 void setMemento(Memento* memento){ _memento = memento; } Memento *_memento;};//客戶端代碼#include "Memento.h"int main(void){ //設定和顯示狀態 Originator o; o._state = "ON"; o.show(); //創建備份 Memento* m = o.createBackups(); //管理備份 Caretaker c; c.setMemento(m); //設定和顯示狀態 o._state = "OFF"; o.show(); //恢復備份 m = c.getMemento(); o.restoreBackups(m); //顯示狀態 o.show(); getchar(); return 0;}