24.12.04
實(shí)驗(yàn) 20:備忘錄模式
本次實(shí)驗(yàn)屬于模仿型實(shí)驗(yàn),通過(guò)本次實(shí)驗(yàn)學(xué)生將掌握以下內(nèi)容:
1、理解備忘錄模式的動(dòng)機(jī),掌握該模式的結(jié)構(gòu);
2、能夠利用備忘錄模式解決實(shí)際問(wèn)題。
[實(shí)驗(yàn)任務(wù)一]:多次撤銷
改進(jìn)課堂上的“用戶信息操作撤銷”實(shí)例,使得系統(tǒng)可以實(shí)現(xiàn)多次撤銷(可以使用HashMap、ArrayList等集合數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn))。
實(shí)驗(yàn)要求:
1. 畫(huà)出對(duì)應(yīng)的類圖;
2. 提交源代碼;
3. 注意編程規(guī)范。
- 類圖

- 源代碼:
import java.util.ArrayList;
import java.util.List;
// Memento類
class Memento {
private String account;
private String password;
private String telNo;
public Memento(String account, String password, String telNo) {
this.account = account;
this.password = password;
this.telNo = telNo;
}
public String getAccount() {
return account;
}
public String getPassword() {
return password;
}
public String getTelNo() {
return telNo;
}
}
// Caretaker類
class Caretaker {
private List<Memento> mementoList = new ArrayList<>();
private int currentIndex = -1; // 當(dāng)前狀態(tài)索引
public void addMemento(Memento memento) {
// 如果當(dāng)前狀態(tài)不是最新?tīng)顟B(tài),則移除當(dāng)前索引后的歷史記錄
if (currentIndex != mementoList.size() - 1) {
mementoList = mementoList.subList(0, currentIndex + 1);
}
mementoList.add(memento);
currentIndex++;
}
public Memento undo() {
if (currentIndex <= 0) {
System.out.println("無(wú)法撤銷,已是初始狀態(tài)。");
return null;
}
currentIndex--;
return mementoList.get(currentIndex);
}
public Memento redo() {
if (currentIndex >= mementoList.size() - 1) {
System.out.println("無(wú)法恢復(fù),已是最新?tīng)顟B(tài)。");
return null;
}
currentIndex++;
return mementoList.get(currentIndex);
}
public Memento getCurrentMemento() {
if (currentIndex >= 0 && currentIndex < mementoList.size()) {
return mementoList.get(currentIndex);
}
return null;
}
}
// UserInfoDTO類
public class UserInfoDTO {
private String account;
private String password;
private String telNo;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelNo() {
return telNo;
}
public void setTelNo(String telNo) {
this.telNo = telNo;
}
public Memento saveMemento() {
return new Memento(account, password, telNo);
}
public void restoreMemento(Memento memento) {
if (memento != null) {
this.account = memento.getAccount();
this.password = memento.getPassword();
this.telNo = memento.getTelNo();
}
}
public void show() {
System.out.println("Account: " + account);
System.out.println("Password: " + password);
System.out.println("TelNo: " + telNo);
}
}
// Client類
public class Client {
public static void main(String[] args) {
UserInfoDTO user = new UserInfoDTO();
Caretaker caretaker = new Caretaker();
// 初始狀態(tài)
user.setAccount("zhangsan");
user.setPassword("123456");
user.setTelNo("13000000000");
caretaker.addMemento(user.saveMemento());
System.out.println("狀態(tài)一:");
user.show();
System.out.println("---------------------------");
// 修改狀態(tài)
user.setPassword("111111");
user.setTelNo("13100001111");
caretaker.addMemento(user.saveMemento());
System.out.println("狀態(tài)二:");
user.show();
System.out.println("---------------------------");
// 再次修改狀態(tài)
user.setPassword("222222");
user.setTelNo("13200002222");
caretaker.addMemento(user.saveMemento());
System.out.println("狀態(tài)三:");
user.show();
System.out.println("---------------------------");
// 多次撤銷
user.restoreMemento(caretaker.undo());
System.out.println("撤銷一次:");
user.show();
System.out.println("---------------------------");
user.restoreMemento(caretaker.undo());
System.out.println("撤銷兩次:");
user.show();
System.out.println("---------------------------");
// 恢復(fù)狀態(tài)
user.restoreMemento(caretaker.redo());
System.out.println("恢復(fù)一次:");
user.show();
System.out.println("---------------------------");
user.restoreMemento(caretaker.redo());
System.out.println("恢復(fù)到最新?tīng)顟B(tài):");
user.show();
System.out.println("---------------------------");
}
}
posted on 2024-12-04 21:36 Daniel350 閱讀(6) 評(píng)論(0) 收藏 舉報(bào)
浙公網(wǎng)安備 33010602011771號(hào)