ITPub博客

首页 > 应用开发 > Java > 备忘录模式

备忘录模式

Java 作者:壹頁書 时间:2017-03-23 11:19:22 0 删除 编辑
http://www.cnblogs.com/java-my-life/archive/2012/06/06/2534942.html


  备忘录模式又叫做快照模式(Snapshot Pattern)或Token模式,是对象的行为模式。

  备忘录对象是一个用来存储另外一个对象内部状态的快照的对象。备忘录模式的用意是在不破坏封装的条件下,将一个对象的状态捕捉(Capture)住,并外部化,存储起来,从而可以在将来合适的时候把这个对象还原到存储起来的状态。备忘录模式常常与命令模式和迭代子模式一同使用。

 备忘录模式所涉及的角色有三个:备忘录(Memento)角色、发起人(Originator)角色、负责人(Caretaker)角色。

  备忘录(Memento)角色

  备忘录角色又如下责任:

  (1)将发起人(Originator)对象的内战状态存储起来。备忘录可以根据发起人对象的判断来决定存储多少发起人(Originator)对象的内部状态。

  (2)备忘录可以保护其内容不被发起人(Originator)对象之外的任何对象所读取。

  备忘录有两个等效的接口:

  ●  窄接口:负责人(Caretaker)对象(和其他除发起人对象之外的任何对象)看到的是备忘录的窄接口(narrow interface),这个窄接口只允许它把备忘录对象传给其他的对象。

  ●  宽接口:与负责人对象看到的窄接口相反的是,发起人对象可以看到一个宽接口(wide interface),这个宽接口允许它读取所有的数据,以便根据这些数据恢复这个发起人对象的内部状态。

  发起人(Originator)角色

  发起人角色有如下责任:

  (1)创建一个含有当前的内部状态的备忘录对象。

  (2)使用备忘录对象存储其内部状态。

  负责人(Caretaker)角色

  负责人角色有如下责任:

  (1)负责保存备忘录对象。

  (2)不检查备忘录对象的内容。


白箱实现:




  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         Originator o = new Originator();  
  4.         Caretaker c = new Caretaker();  
  5.         // 改变负责人对象的状态  
  6.         o.setState("On");  
  7.         // 创建备忘录对象,并将发起人对象的状态储存起来  
  8.         c.saveMemento(o.createMemento());  
  9.         // 修改发起人的状态  
  10.         o.setState("Off");  
  11.         // 恢复发起人对象的状态  
  12.         o.restoreMemento(c.retrieveMemento());  
  13.   
  14.         System.out.println(o.getState());  
  15.     }  
  16.   
  17. }  
  18.   
  19. class Originator {  
  20.   
  21.     private String state;  
  22.   
  23.     /** 
  24.      * 工厂方法,返回一个新的备忘录对象 
  25.      */  
  26.     public Memento createMemento() {  
  27.         return new Memento(state);  
  28.     }  
  29.   
  30.     /** 
  31.      * 将发起人恢复到备忘录对象所记载的状态 
  32.      */  
  33.     public void restoreMemento(Memento memento) {  
  34.         this.state = memento.getState();  
  35.     }  
  36.   
  37.     public String getState() {  
  38.         return state;  
  39.     }  
  40.   
  41.     public void setState(String state) {  
  42.         this.state = state;  
  43.         System.out.println("当前状态:" + this.state);  
  44.     }  
  45.   
  46. }  
  47.   
  48. class Memento {  
  49.   
  50.     private String state;  
  51.   
  52.     public Memento(String state) {  
  53.         this.state = state;  
  54.     }  
  55.   
  56.     public String getState() {  
  57.         return state;  
  58.     }  
  59.   
  60.     public void setState(String state) {  
  61.         this.state = state;  
  62.     }  
  63.   
  64. }  
  65.   
  66. class Caretaker {  
  67.   
  68.     private Memento memento;  
  69.   
  70.     /** 
  71.      * 备忘录的取值方法 
  72.      */  
  73.     public Memento retrieveMemento() {  
  74.         return this.memento;  
  75.     }  
  76.   
  77.     /** 
  78.      * 备忘录的赋值方法 
  79.      */  
  80.     public void saveMemento(Memento memento) {  
  81.         this.memento = memento;  
  82.     }  
  83. } 


黑箱实现.(就是将备忘录对象设置为内部类,对外提供一个标记接口)


  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         Originator o = new Originator();  
  4.         Caretaker c = new Caretaker();  
  5.         // 改变负责人对象的状态  
  6.         o.setState("On");  
  7.         // 创建备忘录对象,并将发起人对象的状态存储起来  
  8.         c.saveMemento(o.createMemento());  
  9.         // 修改发起人对象的状态  
  10.         o.setState("Off");  
  11.         // 恢复发起人对象的状态  
  12.         o.restoreMemento(c.retrieveMemento());  
  13.     }  
  14.   
  15. }  
  16.   
  17. class Originator {  
  18.   
  19.     private String state;  
  20.   
  21.     public String getState() {  
  22.         return state;  
  23.     }  
  24.   
  25.     public void setState(String state) {  
  26.         this.state = state;  
  27.         System.out.println("赋值状态:" + state);  
  28.     }  
  29.   
  30.     /** 
  31.      * 工厂方法,返还一个新的备忘录对象 
  32.      */  
  33.     public MementoIF createMemento() {  
  34.         return new Memento(state);  
  35.     }  
  36.   
  37.     /** 
  38.      * 发起人恢复到备忘录对象记录的状态 
  39.      */  
  40.     public void restoreMemento(MementoIF memento) {  
  41.         this.setState(((Memento) memento).getState());  
  42.     }  
  43.   
  44.     private class Memento implements MementoIF {  
  45.   
  46.         private String state;  
  47.   
  48.         /** 
  49.          * 构造方法 
  50.          */  
  51.         private Memento(String state) {  
  52.             this.state = state;  
  53.         }  
  54.   
  55.         private String getState() {  
  56.             return state;  
  57.         }  
  58.   
  59.         private void setState(String state) {  
  60.             this.state = state;  
  61.         }  
  62.     }  
  63. }  
  64.   
  65. interface MementoIF {  
  66.   
  67. }  
  68.   
  69. class Caretaker {  
  70.   
  71.     private MementoIF memento;  
  72.   
  73.     /** 
  74.      * 备忘录取值方法 
  75.      */  
  76.     public MementoIF retrieveMemento() {  
  77.         return memento;  
  78.     }  
  79.   
  80.     /** 
  81.      * 备忘录赋值方法 
  82.      */  
  83.     public void saveMemento(MementoIF memento) {  
  84.         this.memento = memento;  
  85.     }  
  86. } 




来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-2135899/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 访问者模式
请登录后发表评论 登录
全部评论

注册时间:2013-10-19

  • 博文量
    620
  • 访问量
    6339872