备忘录模式

备忘录模式的用意是在不破坏封装的条件下,将一个对象的状态捕捉住,并外部化,存储起来,从而可以在将来合适的时候把这个对象还原到存储起来的状态

 

备忘录模式结构图:


 

备忘录模式中的角色
发起人:创建含有内部状态的备忘录对象,并使用备忘录对象存储状态
负责人:负责人保存备忘录对象,但不检查备忘录对象的内容
备忘录:备忘录对象将发起人对象的内部状态存起来,并保正其内容不被发起人对象之外的对象像读取
注意:在备忘录的角色中,定义了他必须对不同的人提供不同的接口,对发起人提供宽接口,对其它任何人提供窄接口。也许你说我都提供宽接口得了。对这也是备忘录的一种实现,叫做白箱备忘录,不过这种方法的封装没有设计
好,安全性不够好。

 

白箱备忘录模式:

白箱备忘录的实现:

 public class Originator{
  private String state;
    public Memento CreateMemento(){
       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);
   }
}
public class Memento{
   private String state;
   public Memento(String state){
       this.state = state;
  }
   public String getState(){
        return this.state;
   }

   public void setState(){
       this.state = state;
    }
}
public 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(Sting[] args){
        o.setState("ON");
        c.saveMemento(o.createMemento());
       o.setState("OFF");
       o.restoreMemento(c.retrieveMemento());
    }
}

白箱的优点:实现简单
白箱的缺点:上边说了,破坏了封装,安全性有些问题。
说明:这里白箱的实现只保存了一个状态,其实是可以保存多个状态的。
3
,双接口的实现,宽窄接口(黑箱)
如何实现宽窄接口呢,内部类也许是个好方法。我们把备忘录类设计"成发起人"的内部类,但这样还有的问题是同一
package
中的其它类也能访问到,为了解决这个问题,我们可以把"备忘录"的方法设计成私有的方法,这样就
可以保正封装,又保正发起人能访问到。实现如下:
定义窄接口.

public interface NarrowMemento{
   public void narrowMethod();
 
}
 
class Originator {
    private String state;
    private NarrowMemento memento;
     public Originator(){
     }
    public NarrowMemento createMemento(){
       memento = new Memento(this.state);
       return memento;
   }
    public void restoreMemento(NarrowMemento memento){
        Memento aMemento = (Memento)memento;
       this.setState(aMemento.getState());
    }
   public String getState(){
       return this.state;
   }
    public void setState(String state){

        this.state = state;
   }
   //
内部类
   protected class Memento implements NarrowMemento{
        private String savedState;
       private Memento(String someState){
           saveState = someState;
       }
        private void setState(String someState){

           saveState = someState;
       }
       private String getState(){
           return saveState;
       }
       public void narrowMethod(){
           System.out.println("this is narrow method");
      }
      
   }
   public NarrowMemento getNarrowMemento(){
       return memento;
   }
}
public class Caretaker{
   private NarrowMemento memento;

   public NarrowMemento retrieveMemento(){
       return this.memento;
   }
   public void saveMemento(NarrowMemento 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){
       //use wide interface
       o.setState("On");
       c.saveMemento(o.createMemento());
       o.setState("Off");
       o.restoreMemento(c.retrieveMemento());
       //use narrow interface
        NarrowMemento memento = o.getNarrowMemento();
        memento.narrowMethod();
      
   }
}

ok,实现了对大多数人实现比较窄的接口,对Originator实现了宽接口.

原文地址:https://www.cnblogs.com/jinzhengquan/p/1935885.html