撤銷功能的實現——備忘錄模式(三)
21.3 完整解決方案
為了實現撤銷功能,Sunny公司開發人員決定使用備忘錄模式來設計中國象棋軟體,其基本結構如圖21-4所示:
在圖21-4中,Chessman充當原發器,ChessmanMemento充當備忘錄,MementoCaretaker充當負責人,在MementoCaretaker中定義了一個ChessmanMemento型別的物件,用於儲存備忘錄。完整程式碼如下所示:
//象棋棋子類:原發器
class Chessman {
private String label;
private int x;
private int y;
public Chessman(String label,int x,int y) {
this.label = label;
this.x = x;
this.y = y;
}
public void setLabel(String label) {
this.label = label;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public String getLabel() {
return (this.label);
}
public int getX() {
return (this.x);
}
public int getY() {
return (this.y);
}
//儲存狀態
public ChessmanMemento save() {
return new ChessmanMemento(this.label,this.x,this.y);
}
//恢復狀態
public void restore(ChessmanMemento memento) {
this.label = memento.getLabel();
this.x = memento.getX();
this.y = memento.getY();
}
}
//象棋棋子備忘錄類:備忘錄
class ChessmanMemento {
private String label;
private int x;
private int y;
public ChessmanMemento(String label,int x,int y) {
this.label = label;
this.x = x;
this.y = y;
}
public void setLabel(String label) {
this.label = label;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public String getLabel() {
return (this.label);
}
public int getX() {
return (this.x);
}
public int getY() {
return (this.y);
}
}
//象棋棋子備忘錄管理類:負責人
class MementoCaretaker {
private ChessmanMemento memento;
public ChessmanMemento getMemento() {
return memento;
}
public void setMemento(ChessmanMemento memento) {
this.memento = memento;
}
}
編寫如下客戶端測試程式碼:
class Client {
public static void main(String args[]) {
MementoCaretaker mc = new MementoCaretaker();
Chessman chess = new Chessman("車",1,1);
display(chess);
mc.setMemento(chess.save()); //儲存狀態
chess.setY(4);
display(chess);
mc.setMemento(chess.save()); //儲存狀態
display(chess);
chess.setX(5);
display(chess);
System.out.println("******悔棋******");
chess.restore(mc.getMemento()); //恢復狀態
display(chess);
}
public static void display(Chessman chess) {
System.out.println("棋子" + chess.getLabel() + "當前位置為:" + "第" + chess.getX() + "行" + "第" + chess.getY() + "列。");
}
}
編譯並執行程式,輸出結果如下:
棋子車當前位置為:第1行第1列。 棋子車當前位置為:第1行第4列。 棋子車當前位置為:第1行第4列。 棋子車當前位置為:第5行第4列。 ******悔棋****** 棋子車當前位置為:第1行第4列。 |
【作者:劉偉 http://blog.csdn.net/lovelion】
相關文章
- 實驗 20:備忘錄模式模式
- 設計模式——命令模式實現撤銷設計模式
- 在Swift中實現撤銷功能Swift
- 11.21實驗 20:備忘錄模式模式
- 備忘錄模式(Memento)模式
- 設計模式----備忘錄模式設計模式
- 設計模式學習筆記(十八)備忘錄模式及其實現設計模式筆記
- 19_備忘錄模式模式
- 行為型模式:備忘錄模式模式
- 設計模式 - 備忘錄模式 ( Memento )設計模式
- 設計模式之備忘錄模式設計模式
- Android備忘錄《單例模式》Android單例模式
- 第 22 章 備忘錄模式模式
- 簡說設計模式——備忘錄模式設計模式
- 極簡設計模式-備忘錄模式設計模式
- GoLang設計模式11 - 備忘錄模式Golang設計模式
- 探究 canvas 繪圖中撤銷(undo)功能的實現方式Canvas繪圖
- 撤銷和回退的實現
- 『現學現忘』Git基礎 — 23、Git中的撤銷操作Git
- 設計模式漫談之備忘錄模式設計模式
- 設計模式 | 備忘錄模式及典型應用設計模式
- 折騰Java設計模式之備忘錄模式Java設計模式
- 19.java設計模式之備忘錄模式Java設計模式
- Android原始碼分析之備忘錄模式Android原始碼模式
- 備忘錄設計模式知識概括設計模式
- 備忘錄
- 【備忘錄】
- 行為型設計模式 - 備忘錄模式詳解設計模式
- Rust語言之GoF設計模式:備忘錄Memento模式RustGo設計模式
- 抽絲剝繭——備忘錄設計模式設計模式
- Qt中的撤銷/重做功能QT
- Swift 中的設計模式 #2 觀察者模式與備忘錄模式Swift設計模式
- Eigen備忘錄
- Python 備忘錄Python
- RabbitMQ備忘錄MQ
- Command(命令)——物件行為型模式(通過Command設計模式實現WinForm表單維護的撤銷與重做功能)物件設計模式ORM
- 設計模式(Swift) - 2.單例模式、備忘錄模式和策略模式設計模式Swift單例
- 無廢話設計模式(16)行為型模式--備忘錄模式設計模式
- 軟體設計模式學習(二十二)備忘錄模式設計模式