设计模式读书笔记之备忘录模式(Memento)

备忘录模式:在不破坏封装性的前提下,捕获对象的内部状态并保存,这样以后就可以恢复该对象恢复到保存的状态。

备忘录模式比较简单, 不要因为名字比较陌生而不敢学它. 它没有复杂的结构, 上图已经能足够说明问题.

Originator: 备忘录发起人, 通常是需要备忘的对象自己.

Memento: 备忘录对象, 保存了Originator的内部状态.

CareTaker: 备忘录管理者.

基本代码:

[java]  view plain  copy
  1. //备忘录发起人,即需要备忘的对象  
  2. package designpattern.memento;  
  3. public class Originator {  
  4.     private String state;  
  5.   
  6.     public String getState() {  
  7.         return state;  
  8.     }  
  9.   
  10.     public void setState(String state) {  
  11.         this.state = state;  
  12.     }  
  13.     public Memento createMemo(){  
  14.         return new Memento(state);  
  15.     }  
  16.     public void recover(Memento m){  
  17.         this.state = m.getState();  
  18.     }  
  19.     public void show(){  
  20.         System.out.println("state = " + state);  
  21.     }  
  22. }  
  23. //备忘录  
  24. package designpattern.memento;  
  25.   
  26. public class Memento {  
  27.     private String state;  
  28.   
  29.     public String getState() {  
  30.         return state;  
  31.     }  
  32.     public Memento(String state){  
  33.         this.state = state;  
  34.     }  
  35. }  
  36. //备忘录管理者  
  37. package designpattern.memento;  
  38. public class CareTaker {  
  39.     private Memento memento;  
  40.     public Memento getMemento() {  
  41.         return memento;  
  42.     }  
  43.   
  44.     public void setMemento(Memento memento) {  
  45.         this.memento = memento;  
  46.     }  
  47.       
  48. }  
  49. //test case  
  50. package designpattern.memento;  
  51. public class Test {  
  52.     public static void main(String[] args) {  
  53.         Originator o = new Originator();  
  54.         o.setState("On");  
  55.         o.show();  
  56.         CareTaker c = new CareTaker();  
  57.         c.setMemento(o.createMemo());  
  58.         o.setState("Off");  
  59.         o.show();  
  60.         o.recover(c.getMemento());  
  61.         o.show();  
  62.     }  
  63. }  

举例:

打rpg游戏的时候经常在打大boss之前把游戏保存一下,如果死了,就重新来。其实这就是备忘录模式。看代码:

[java]  view plain  copy
  1. package designpattern.memento.game;  
  2.   
  3. public class GameRole {  
  4.     private int vitality;  
  5.     private int attack;  
  6.     private int defense;  
  7.       
  8.     public GameRole(){  
  9.         this.vitality = 100;  
  10.         this.attack = 100;  
  11.         this.defense = 100;  
  12.     }  
  13.     public void showState(){  
  14.         System.out.println("vitality = " + vitality + "/nattack = " + attack + "/ndefence = " + defense);  
  15.     }  
  16.     public RoleStateMemo save(){  
  17.         return new RoleStateMemo(vitality, attack, defense);  
  18.     }  
  19.     public void recover(RoleStateMemo m){  
  20.         System.out.println("太菜了我,还好我存盘了,哈哈。");  
  21.         System.out.println("复活中......");  
  22.         this.vitality = m.getVitality();  
  23.         this.attack = m.getAttack();  
  24.         this.defense = m.getDefense();  
  25.         System.out.println("又复活了.");  
  26.     }  
  27.     public void fight(){  
  28.         System.out.println("与大Boss决战./n......");  
  29.         System.out.println("你输了, 你的生命,防御, 战斗力都将减为0.");  
  30.         this.vitality = 0;  
  31.         this.defense = 0;  
  32.         this.attack = 0;  
  33.     }  
  34. }  
  35. //  
  36. package designpattern.memento.game;  
  37.   
  38. public class RoleStateMemo {  
  39.     private int vitality;  
  40.     private int attack;  
  41.     private int defense;  
  42.     public RoleStateMemo(int v, int a, int d){  
  43.         this.vitality = v;  
  44.         this.attack = a;  
  45.         this.defense = d;  
  46.     }  
  47.     public int getVitality() {  
  48.         return vitality;  
  49.     }  
  50.     public void setVitality(int vitality) {  
  51.         this.vitality = vitality;  
  52.     }  
  53.     public int getAttack() {  
  54.         return attack;  
  55.     }  
  56.     public void setAttack(int attack) {  
  57.         this.attack = attack;  
  58.     }  
  59.     public int getDefense() {  
  60.         return defense;  
  61.     }  
  62.     public void setDefense(int defense) {  
  63.         this.defense = defense;  
  64.     }  
  65.       
  66. }  
  67. //  
  68. package designpattern.memento.game;  
  69.   
  70. public class RoleMemoManager {  
  71.     private RoleStateMemo m;  
  72.     public RoleStateMemo getM() {  
  73.         return m;  
  74.     }  
  75.     public void setM(RoleStateMemo m) {  
  76.         this.m = m;  
  77.     }  
  78. }  
  79. //test case  
  80. package designpattern.memento.game;  
  81. public class Test {  
  82.     public static void main(String[] args) {  
  83.         GameRole role = new GameRole();  
  84.         role.showState();  
  85.         RoleMemoManager manager = new RoleMemoManager();  
  86.         manager.setM(role.save());  
  87.         role.fight();  
  88.         role.showState();  
  89.         role.recover(manager.getM());  
  90.         role.showState();  
  91.     }  
  92. }  

适用情景:

需要维护或记录历史的类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值