Chinaunix首页 | 论坛 | 博客
  • 博客访问: 329668
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1134
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-25 09:41
个人简介

目前任职于搜狐~

文章分类

全部博文(31)

文章存档

2014年(12)

2013年(19)

分类: Java

2014-03-02 00:09:38

备忘录模式也是GOF提出的23种设计模式中行为模式的一种,大家看到这个名字可能会觉得很生疏,相信很多同学没见过这种设计模式更没有用过它,其实备忘录模式是一种非常简单容易理解的设计模式。
备忘录模式是用于哪种场景的呢?它适用于对象在执行某些操作为防止意外而在执行操作前将对象状态备份的场景,有点类似于事务回滚的意思。
下面还是先看下GOF对它的定义:
在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可以就该对象恢复到原先保存的状态。
备忘录模式的机构图:

备忘录模式的角色:
(1)发起人(Originator):要被备份的成员,它提供一创建备忘录的方法,其实就是将它自身的某些信息拷贝一份到一个备忘录对象中。并提供另外一个方法将备忘录中的信息覆盖自身的信息。
(2)备忘录(Memento):备忘录对象中包含存储发起人状态的成员变量,它提供set,get或构造方法保存发起人状态及获取发起人状态。
(3)管理角色(Caretaker):用于管理备忘录对象的实现类。
下面通过代码来具体实现备忘录模式。
如下为代码层次结构图:

1、第一个文件发起人角色:Game.java

点击(此处)折叠或打开

  1. /**
  2.  * 游戏自身(备忘录模式中的发起人,备份的是游戏的状态)
  3.  */
  4. public class Game {

  5.     /**
  6.      * 英雄状态属性
  7.      */
  8.     private HeroState hState;

  9.     /**
  10.      * 场景状态属性
  11.      */
  12.     private SceneState sState;

  13.     public HeroState gethState() {
  14.         return hState;
  15.     }

  16.     public void sethState(HeroState hState) {
  17.         this.hState = hState;
  18.     }

  19.     public SceneState getsState() {
  20.         return sState;
  21.     }

  22.     public void setsState(SceneState sState) {
  23.         this.sState = sState;
  24.     }

  25.     /**
  26.      * 备份游戏
  27.      */
  28.     public GameMemento createMemento(){
  29.         return new GameMemento(hState,sState);
  30.     }
  31.     
  32.     /**
  33.      * 玩游戏
  34.      * @throws InterruptedException
  35.      */
  36.     public void play(){
  37.         hState.setHP(0);
  38.         hState.setMP(0);
  39.         sState.setCoin(0);
  40.         sState.setWood(0);
  41.     }
  42.     
  43.     /**
  44.      * 游戏还原
  45.      */
  46.     public void restore(GameMemento memento){
  47.         this.hState = memento.gethState();
  48.         this.sState = memento.getsState();
  49.     }
  50. }
2、第二个文件要备份的状态实体:HeroState.java 

点击(此处)折叠或打开

  1. /**
  2.  * 游戏英雄人物状态实体
  3.  */
  4. public class HeroState implements Cloneable{

  5.     /**
  6.      * 英雄生命值
  7.      */
  8.     private int HP;
  9.     
  10.     /**
  11.      * 英雄魔法值
  12.      */
  13.     private int MP;
  14.     
  15.     /**
  16.      * 状态保存时间
  17.      */
  18.     private Date stateDate;

  19.     public int getHP() {
  20.         return HP;
  21.     }

  22.     public void setHP(int hP) {
  23.         HP = hP;
  24.     }

  25.     public int getMP() {
  26.         return MP;
  27.     }

  28.     public void setMP(int mP) {
  29.         MP = mP;
  30.     }

  31.     public Date getStateDate() {
  32.         return stateDate;
  33.     }

  34.     public void setStateDate(Date stateDate) {
  35.         this.stateDate = stateDate;
  36.     }
  37.     
  38.     public HeroState clone(){
  39.         try {
  40.             return (HeroState) super.clone();
  41.         } catch (CloneNotSupportedException e) {
  42.             // TODO Auto-generated catch block
  43.             e.printStackTrace();
  44.         }
  45.         return null;
  46.     }
  47. }
3、要备份的状态实体:SceneState.java 

点击(此处)折叠或打开

  1. /**
  2.  * 游戏场景状态实体
  3.  */
  4. public class SceneState implements Cloneable{

  5.     /**
  6.      * 金币数量
  7.      */
  8.     private int coin;
  9.     
  10.     /**
  11.      * 木材数量
  12.      */
  13.     private int wood;
  14.     
  15.     /**
  16.      * 地图名称
  17.      */
  18.     private String mapName;

  19.     public int getCoin() {
  20.         return coin;
  21.     }

  22.     public void setCoin(int coin) {
  23.         this.coin = coin;
  24.     }

  25.     public int getWood() {
  26.         return wood;
  27.     }

  28.     public void setWood(int wood) {
  29.         this.wood = wood;
  30.     }

  31.     public String getMapName() {
  32.         return mapName;
  33.     }

  34.     public void setMapName(String mapName) {
  35.         this.mapName = mapName;
  36.     }
  37.     
  38.     public SceneState clone(){
  39.         try {
  40.             return (SceneState) super.clone();
  41.         } catch (CloneNotSupportedException e) {
  42.             // TODO Auto-generated catch block
  43.             e.printStackTrace();
  44.         }
  45.         return null;
  46.     }
  47. }
4、第四个文件备忘录角色:GameMemcento.java 

点击(此处)折叠或打开

  1. /**
  2.  * 游戏备忘录角色
  3.  */
  4. public class GameMemento{

  5.     /**
  6.      * 英雄状态
  7.      */
  8.     private HeroState hState;
  9.     
  10.     /**
  11.      * 场景状态
  12.      */
  13.     private SceneState sState;
  14.     
  15.     /**
  16.      * 构造方法
  17.      * @param hState
  18.      * @param sState
  19.      */
  20.     public GameMemento(HeroState hState,SceneState sState){
  21.         this.hState = hState.clone();
  22.         this.sState = sState.clone();
  23.     }

  24.     /**
  25.      * 获取备份状态
  26.      * @return
  27.      */
  28.     public HeroState gethState() {
  29.         return hState;
  30.     }

  31.     /**
  32.      * 获取备份状态
  33.      * @return
  34.      */
  35.     public SceneState getsState() {
  36.         return sState;
  37.     }
  38. }
5、第五个文件备忘录管理角色:Caretaker.java 

点击(此处)折叠或打开

  1. /**
  2.  * 备忘录管理器
  3.  */
  4. public class Caretaker {

  5.     /**
  6.      * 备忘录实体
  7.      */
  8.     private GameMemento memento;

  9.     public GameMemento getMemento() {
  10.         return memento;
  11.     }

  12.     public void setMemento(GameMemento memento) {
  13.         this.memento = memento;
  14.     }
  15. }
6、第六个文件:TestMain.java 

点击(此处)折叠或打开

  1. /**
  2.  * 测试Main方法
  3.  */
  4. public class TestMain {

  5.     public static void main(String [] args){
  6.         Game game = new Game();
  7.         HeroState hState = new HeroState();
  8.         hState.setHP(100);
  9.         hState.setMP(100);
  10.         SceneState sState = new SceneState();
  11.         sState.setCoin(1000);
  12.         sState.setWood(1000);
  13.         game.sethState(hState);
  14.         game.setsState(sState);
  15.         System.out.println("游戏状态备份开始");
  16.         GameMemento memento = game.createMemento();
  17.         Caretaker ct = new Caretaker();
  18.         ct.setMemento(memento);
  19.         System.out.println("游戏状态备份完成");
  20.         System.out.println("开始游戏,当前英雄生命值:" + game.gethState().getHP());
  21.         game.play();
  22.         System.out.println("游戏结束,当前英雄生命值:" + game.gethState().getHP());
  23.         System.out.println("游戏状态还原开始");
  24.         game.restore(ct.getMemento());
  25.         System.out.println("游戏状态还原结束");
  26.         System.out.println("当前英雄生命值:" + game.gethState().getHP());
  27.     }
  28. }
很多问题看了代码自然明了,备忘录模式还是很简单的,使用非常方便。
备忘录模式的优点:
(1)将对象状态备份,便于在出现意外时进行状态回滚。
(2)对象状态的副本交由管理器(Caretaker)管理,发起人无需管理状态的备份。而在还原状态时,又由发起人自己执行状态还原方法,外界无法获取发起人的备份状态信息从而保证了备份数据的安全性。
备忘录模式的缺点:
将对象状态备份会占用较多的系统资源。
阅读(9145) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~