软件设计 备忘录模式 Memento Pattern

程序虽然来源于生活,有时候也能高于生活,比如遗憾,在程序中就是能补救的。游戏中有存档和读档,设计模式中也有备忘录模式可以实现。

备忘录模式就是为程序提供了一个可回溯的时间节点,如果程序在运行过程中某一步出现了错误,就可以回到之前的某个被保存的节点上重新来过(就像艾克的大招)。平时编辑文本的时候,当编辑出现错误时,就需要撤回,此时只需要按下 Ctrl + Z 就可以回到上一步,这样大大方便了文本编辑。

其实备忘录模式在程序中的应用十分广泛,比如安卓程序在很多情况下都会重新加载 Activity,实际上安卓中 ActivityonSaveInstanceStateonRestoreInstanceState 就是用到了备忘录模式,分别用于保存和恢复,这样就算重新加载也可以恢复到之前的状态。

1.学生为例的代码实现:

下面以学生学习为例介绍备忘录模式:

1、定义学生
public class Student {
   /**
    * 当前正在做的事
    */
   private String currentThing;
   /**
    * 当前做的事完成百分比
    */
   private int percentage;
   /**
    * 做事
    * @param currentThing 当前正在做的事
    */
   public void todo(String currentThing) {
       this.currentThing = currentThing;
       this.percentage = new Random().nextInt(100);
  }
   /**
    * 保存当前状态
    * @return 当前状态
    */
   public State save() {
       return new State(this.currentThing, this.percentage);
  }
   /**
    * 重置状态
    * @param state 状态
    */
   public void restore(State state){
       this.currentThing = state.getCurrentThing();
       this.percentage = state.getPercentage();
  }
   @Override
   public String toString() {
       return "现在正在做:" + this.currentThing + ",进度:" + this.percentage + "%";
  }
}

2、定义状态
public class State {
   /**
    * 当前正在做的事
    */
   private final String currentThing;
   /**
    * 当前做的事完成百分比
    */
   private final int percentage;
   public State(String currentThing, int percentage) {   //仅开放给同一个包下的Student类使用
       this.currentThing = currentThing;
       this.percentage = percentage;
  }
   public String getCurrentThing() {
       return currentThing;
  }
   public int getPercentage() {
       return percentage;
  }
}

3、调用

// 定义学生
Student student = new Student();
// 开始学习
student.todo("学习");
// 查看当前的状态
System.out.println(student);
// 保存当前的状态
State savedState = student.save();
// 学到中途打游戏去了
student.todo("打游戏");
// 查看当前的状态
System.out.println(student);
// 打着游戏又想着该学习,又回去学习,回到上次的进度继续学习
student.restore(savedState);
// 查看当前的状态
System.out.println(student);

输出结果为:

现在正在做:学习,进度:4%
现在正在做:打游戏,进度:14%
现在正在做:学习,进度:4%
   
这样就实现了状态的回溯并继续进行,是备忘录模式的典型应用。

 

优缺点:

优点

1、给用户提供了一种可以恢复状态的机制,可以使用户能够比较方便地回到某个历史的状态。

2、实现了信息的封装,使得用户不需要关心状态的保存细节。

缺点

消耗资源。需要冗余属性相同的类,如果类的成员变量过多,势必会占用比较大的资源,而且每一次保存都会消耗一定的内存。

 

使用场景:

1、需要保存或者恢复数据的相关状态场景。

2、需要提供一个可回滚的操作。

 

注意:

1、为了符合迪米特原则,还要增加一个管理备忘录的类。

2、为了节约内存,可使用原型模式 + 备忘录模式

 

2.撤销操作(增加了管理备忘录的类)

1.用户类
package com19.memorandum;

public class User {
   private String id;
   private String password;

   public void setId(String id) {
       this.id = id;
  }

   public void setPassword(String password) {
       this.password = password;
  }

   public Memo saveState(){
       return new Memo(id,password);
  }

   public void restoreState(Memo m){
       this.id = m.getId();
       this.password = m.getPassword();
  }

   public void show(){
       System.out.println("id:" + this.id);
       System.out.println("password:" + this.password);
  }
}


2.备忘录类
package com19.memorandum;

// 备忘录
public class Memo {
   private String id;
   private String password;

   public Memo(String id, String password) {
       this.id = id;
       this.password = password;
  }

   public String getId() {
       return id;
  }

   public String getPassword() {
       return password;
  }

}

3.管理备忘录的类
public class Caretaker {
   // 关联关系 Memo(长期的)
   private List<Memo> list = new ArrayList<>();
// 依赖关系 Memo(临时的)
   public void setMemo(Memo m){
       list.add(m);
  }

//   2是指 list加入了 id,password
   public Memo getMemo(){
       Memo m = list.get( list.size() - 2 );
       list.remove( list.size() - 2 );
       return m;
  }
}
   
4.Client
public class Client {
   public static void main(String[] args) {
       User user = new User();
       Caretaker caretaker = new Caretaker();

       user.setId("1");
       user.setPassword("1");
       caretaker.setMemo(user.saveState());
       System.out.println("保存一次");
       user.show();
       System.out.println("************************************************************");

       user.setId("2");
       user.setPassword("2");
       caretaker.setMemo(user.saveState());
       System.out.println("保存二次");
       user.show();
       System.out.println("************************************************************");


       user.setId("3");
       user.setPassword("3");
       caretaker.setMemo(user.saveState());
       System.out.println("保存三次");
       user.show();
       System.out.println("************************************************************");

       System.out.println("第一次撤销");
       user.restoreState(caretaker.getMemo());
       user.show();
       System.out.println("************************************************************");

       System.out.println("第二次撤销");
       user.restoreState(caretaker.getMemo());
       user.show();
  }
}

结果:
   
保存一次
id:1
password:1
************************************************************
保存二次
id:2
password:2
************************************************************
保存三次
id:3
password:3
************************************************************
第一次撤销
id:2
password:2
************************************************************
第二次撤销
id:1
password:1

 

 

 

 

posted @ 2023-02-11 15:01  kuaiquxie  阅读(17)  评论(0编辑  收藏  举报