撤销功能的实现——备忘录模式(三)

21.3 完整解决方案

      为了实现撤销功能,Sunny公司开发人员决定使用备忘录模式来设计中国象棋软件,其基本结构如图21-4所示:

      在图21-4中,Chessman充当原发器,ChessmanMemento充当备忘录,MementoCaretaker充当负责人,在MementoCaretaker中定义了一个ChessmanMemento类型的对象,用于存储备忘录。完整代码如下所示:

[java]  view plain copy
  1. //象棋棋子类:原发器  
  2. class Chessman {  
  3.     private String label;  
  4.     private int x;  
  5.     private int y;  
  6.   
  7.     public Chessman(String label,int x,int y) {  
  8.         this.label = label;  
  9.         this.x = x;  
  10.         this.y = y;  
  11.     }  
  12.   
  13.     public void setLabel(String label) {  
  14.         this.label = label;   
  15.     }  
  16.   
  17.     public void setX(int x) {  
  18.         this.x = x;   
  19.     }  
  20.   
  21.     public void setY(int y) {  
  22.         this.y = y;   
  23.     }  
  24.   
  25.     public String getLabel() {  
  26.         return (this.label);   
  27.     }  
  28.   
  29.     public int getX() {  
  30.         return (this.x);   
  31.     }  
  32.   
  33.     public int getY() {  
  34.         return (this.y);   
  35.     }  
  36.       
  37.     //保存状态  
  38.     public ChessmanMemento save() {  
  39.         return new ChessmanMemento(this.label,this.x,this.y);  
  40.     }  
  41.       
  42.     //恢复状态  
  43.     public void restore(ChessmanMemento memento) {  
  44.         this.label = memento.getLabel();  
  45.         this.x = memento.getX();  
  46.         this.y = memento.getY();  
  47.     }  
  48. }  
  49.   
  50. //象棋棋子备忘录类:备忘录  
  51. class ChessmanMemento {  
  52.     private String label;  
  53.     private int x;  
  54.     private int y;  
  55.   
  56.     public ChessmanMemento(String label,int x,int y) {  
  57.         this.label = label;  
  58.         this.x = x;  
  59.         this.y = y;  
  60.     }  
  61.   
  62.     public void setLabel(String label) {  
  63.         this.label = label;   
  64.     }  
  65.   
  66.     public void setX(int x) {  
  67.         this.x = x;   
  68.     }  
  69.   
  70.     public void setY(int y) {  
  71.         this.y = y;   
  72.     }  
  73.   
  74.     public String getLabel() {  
  75.         return (this.label);   
  76.     }  
  77.   
  78.     public int getX() {  
  79.         return (this.x);   
  80.     }  
  81.   
  82.     public int getY() {  
  83.         return (this.y);   
  84.     }     
  85. }  
  86.   
  87. //象棋棋子备忘录管理类:负责人  
  88. class MementoCaretaker {  
  89.     private ChessmanMemento memento;  
  90.   
  91.     public ChessmanMemento getMemento() {  
  92.         return memento;  
  93.     }  
  94.   
  95.     public void setMemento(ChessmanMemento memento) {  
  96.         this.memento = memento;  
  97.     }  
  98. }  

      编写如下客户端测试代码:

[java]  view plain copy
  1. class Client {  
  2.     public static void main(String args[]) {  
  3.         MementoCaretaker mc = new MementoCaretaker();  
  4.         Chessman chess = new Chessman("车",1,1);  
  5.         display(chess);  
  6.         mc.setMemento(chess.save()); //保存状态       
  7.         chess.setY(4);  
  8.         display(chess);  
  9.         mc.setMemento(chess.save()); //保存状态  
  10.         display(chess);  
  11.         chess.setX(5);  
  12.         display(chess);  
  13.         System.out.println("******悔棋******");     
  14.         chess.restore(mc.getMemento()); //恢复状态  
  15.         display(chess);  
  16.     }  
  17.       
  18.     public static void display(Chessman chess) {  
  19.         System.out.println("棋子" + chess.getLabel() + "当前位置为:" + "第" + chess.getX() + "行" + "第" + chess.getY() + "列。");  
  20.     }  
  21. }  

      编译并运行程序,输出结果如下: 

棋子车当前位置为:第1行第1列。

棋子车当前位置为:第1行第4列。

棋子车当前位置为:第1行第4列。

棋子车当前位置为:第5行第4列。

******悔棋******

棋子车当前位置为:第1行第4列。


【作者:刘伟  http://blog.csdn.net/lovelion

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值