Java/C++实现备忘录模式--撤销操作
改进课堂上的“用户信息操作撤销”实例,使得系统可以实现多次撤销(可以使用HashMap、ArrayList等集合数据结构实现)。
类图:

Java代码:
import java.util.ArrayList;
import java.util.List; public class Caretaker {
private List<Memento> list=new ArrayList<>();
public Memento getMemento() {
Memento mm=list.get(list.size()-2);
list.remove(list.size()-2);
return mm;
}
public void setMemento(Memento memento) {
list.add(memento);
}
} public class Memento {
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(String account, String password, String telNo) {
this.account = account;
this.password = password;
this.telNo = telNo;
} } 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) {
this.account=memento.getAccount();
this.password=memento.getPassword();
this.telNo=memento.getTelNo();
}
public void show() {
System.out.println("Account:"+this.account);
System.out.println("Password:"+this.password);
System.out.println("TelNo:"+this.telNo);
} } public class Client { public static void main(String[] args) {
// TODO Auto-generated method stub
UserInfoDTO user=new UserInfoDTO();
Caretaker c=new Caretaker(); user.setAccount("zhangsan");
user.setPassword("123456");
user.setTelNo("1310000000");
System.out.println("状态一:");
user.show();
c.setMemento(user.saveMemento());
System.out.println("-----------------------------"); user.setPassword("111111");
user.setTelNo("1310001111");
System.out.println("状态二:");
user.show();
c.setMemento(user.saveMemento());
System.out.println("-----------------------------"); user.setPassword("zyx666");
user.setTelNo("15733333333");
System.out.println("状态三:");
user.show();
c.setMemento(user.saveMemento());
System.out.println("-----------------------------"); user.setPassword("777777");
user.setTelNo("15511111111");
System.out.println("状态四:");
user.show();
c.setMemento(user.saveMemento());
System.out.println("-----------------------------"); user.setPassword("666666");
user.setTelNo("17455555555");
System.out.println("状态五:");
user.show();
c.setMemento(user.saveMemento());
System.out.println("-----------------------------"); user.restoreMemento(c.getMemento());
System.out.println("回到状态四:");
user.show();
System.out.println("-----------------------------"); user.restoreMemento(c.getMemento());
System.out.println("回到状态三:");
user.show();
System.out.println("-----------------------------"); user.restoreMemento(c.getMemento());
System.out.println("回到状态二:");
user.show();
System.out.println("-----------------------------"); user.restoreMemento(c.getMemento());
System.out.println("回到状态一:");
user.show();
System.out.println("-----------------------------");
} }
C++代码:
#include<iostream>
#include <list>
using namespace std;
class Memento {
private:
string account;
string password;
string telNo;
public:
string getAccount() {
return account;
}
void setAccount(string account) {
this->account = account;
}
string getPassword() {
return password;
}
void setPassword(string password) {
this->password = password;
}
string getTelNo() {
return telNo;
}
void setTelNo(string telNo) {
this->telNo = telNo;
}
Memento(string account, string password, string telNo) {
this->account = account;
this->password = password;
this->telNo = telNo;
}
};
class UserInfoDTO {
private:
string account;
string password;
string telNo;
public:
string getAccount() {
return account;
}
void setAccount(string account) {
this->account = account;
}
string getPassword() {
return password;
}
void setPassword(string password) {
this->password = password;
}
string getTelNo() {
return telNo;
}
void setTelNo(string telNo) {
this->telNo = telNo;
} Memento* saveMemento() {
return new Memento(account,password,telNo);
}
void restoreMemento(Memento *memento) {
this->account=memento->getAccount();
this->password=memento->getPassword();
this->telNo=memento->getTelNo();
}
void show() {
cout<<"Account:"<<this->account<<endl;
cout<<"Password:"<<this->password<<endl;
cout<<"TelNo:"<<this->telNo<<endl;
}
};
class Caretaker {
private:
list<Memento*> ll;
public:
Memento* getMemento() {
ll.pop_front();
Memento* mm=ll.front();
return mm;
}
void setMemento(Memento *memento) {
ll.push_front(memento);
}
};
int main(){
UserInfoDTO *user=new UserInfoDTO();
Caretaker *c=new Caretaker(); user->setAccount("zhangsan");
user->setPassword("123456");
user->setTelNo("1310000000");
cout<<"状态一:"<<endl;
user->show();
c->setMemento(user->saveMemento());
cout<<"-----------------------------"<<endl; user->setPassword("111111");
user->setTelNo("1310001111");
cout<<"状态二:"<<endl;
user->show();
c->setMemento(user->saveMemento());
cout<<"-----------------------------"<<endl; user->setPassword("zyx666");
user->setTelNo("15733333333");
cout<<"状态三:"<<endl;
user->show();
c->setMemento(user->saveMemento());
cout<<"-----------------------------"<<endl; user->setPassword("777777");
user->setTelNo("15511111111");
cout<<"状态四:"<<endl;
user->show();
c->setMemento(user->saveMemento());
cout<<"-----------------------------"<<endl; user->setPassword("666666");
user->setTelNo("17455555555");
cout<<"状态五:"<<endl;
user->show();
c->setMemento(user->saveMemento());
cout<<"-----------------------------"<<endl; user->restoreMemento(c->getMemento());
cout<<"回到状态四:"<<endl;
user->show();
cout<<"-----------------------------"<<endl; user->restoreMemento(c->getMemento());
cout<<"回到状态三:"<<endl;
user->show();
cout<<"-----------------------------"<<endl; user->restoreMemento(c->getMemento());
cout<<"回到状态二:"<<endl;
user->show();
cout<<"-----------------------------"<<endl; user->restoreMemento(c->getMemento());
cout<<"回到状态一:"<<endl;
user->show();
cout<<"-----------------------------"<<endl;
}
运行结果:


Java/C++实现备忘录模式--撤销操作的更多相关文章
- 折腾Java设计模式之备忘录模式
原文地址:折腾Java设计模式之备忘录模式 备忘录模式 Without violating encapsulation, capture and externalize an object's int ...
- java设计模式9.备忘录模式、访问者模式、调停者模式
备忘录模式 备忘录模式又叫快照模式,备忘录对象是一个用来存储另外一个对象内部状态快照的对象.备忘录的用意是在不破坏封装的条件下,将一个对象的状态捕捉,并外部化存储起来,从而可以在将来合适的时候把这个对 ...
- java设计模式之备忘录模式
备忘录模式 备忘录模式是一种软件设计模式:在不破坏封闭的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态.一听到备忘录这个字的时候想起了小小时打的游 ...
- 19.java设计模式之备忘录模式
基本需求 游戏的角色有攻击力和防御力,在大战Boss之前保存自身的状态(攻击力和防御力),当大战Boss之后攻击力和防御力下降,从备忘录对象恢复到大战前的状态 传统方案 一个对象,就对应一个保存对象状 ...
- 观世音甘泉活树的故事竟然是Java设计模式:备忘录模式
目录 定义 意图 主要解决问题 何时使用 优缺点 结构 白箱实现 黑箱实现 多重检查点 观世音甘泉活树的故事 定义 备忘录模式是对象的行为型模式,备忘录对象是一个用来存储另外一个对象内部状态的快照的对 ...
- Java设计模式应用——备忘录模式
备忘录模式主要用于存档.游戏中我们打boss前总会存档,如果打boss失败,则读取存档,重新挑战boss. 可以看出来,备忘录模式一般包括如下数据结构 1. 存档文件:用于恢复备份场景的必要数据: 2 ...
- Java/C++实现模板方法模式---数据库操作
对数据库的操作一般包括连接.打开.使用.关闭等步骤,在数据库操作模板类中我们定义了connDB().openDB().useDB().closeDB()四个方法分别对应这四个步骤.对于不同类型的数据库 ...
- Java设计模式学习记录-备忘录模式
前言 这次要介绍的是备忘录模式,也是行为模式的一种 .现在人们的智能手机上都会有备忘录这样一个功能,大家也都会用,就是为了记住某件事情,防止以后自己忘记了.那么备忘录模式又是什么样子的呢?是不是和手机 ...
- 重学 Java 设计模式:实战备忘录模式「模拟互联网系统上线过程中,配置文件回滚场景」
作者:小傅哥 博客:https://bugstack.cn - 原创系列专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 实现不了是研发的借口? 实现不了,有时候是功能复杂度较高难以实 ...
随机推荐
- 自己的markdown笔记
markdown一些语法 记录自己会用的一些markdown语法,不定期更新,用的软件是hroopad,hroopad下载地址点击跳转.这个书写软件对新手还有中文用户比较友好,左边是markdown语 ...
- BSOJ6387题解
算是刷新了我对树上问题的认知 首先第一问随便做一个 \(O(nk)\) 的 DP 就可以草过去,考虑第二问. 我们将问题分为两个部分:走儿子边的答案和走父亲边的答案.最后拼接一下就好了. 设 \(fd ...
- BSOJ5086题解
题意略. 我们设 \([x^k]G_n(x)\) 代表深度为 \(n\) 的树,距离为 \(k\) 的点对数量,\([x^k]F_n(x)\) 为深度为 $ n $ 的树中,深度为 \(k\) 的节点 ...
- git同步代码到另一分支
将dev分支的代码同步到master 方法一:用git命令 1.git checkout master 2.git merge dev 3.git push --set-upstream origin ...
- Ubuntu18.04..5 配置国内镜像源:解决E: Failed to fetch
镜像下载.域名解析.时间同步请点击 阿里云开源镜像站 问题描述 使用 sudo apt get-install 出现 E: Failed to fetch问题. 更换镜像源 错误原因:绝大多数情况下, ...
- Debian11系统安装
镜像下载.域名解析.时间同步请点击 阿里云开源镜像站 1. 启动镜像 启动镜像,进入安装界面,默认选择第一个图形化安装界面,回车 2. 选择语言 这里选择English语言,然后点击Continue ...
- .htaccess文件构成的PHP后门
1..htaccess文件 2.文件上传绕过 一般.htaccess可以用来留后门和针对黑名单绕过 创建一个txt写入(png解析为php) AddType application/x-httpd-p ...
- ctf之POST
题目信息如下 可知该题考察post请求知识 直接将what=flag以post传参格式进行传参即可获得flag
- GET、POST请求
GET和POST的区别主要有以下几个方面: 1.URL可见性: GET:参数URL可见: POST:URL参数不可见: 2.数据传输 GET:通过拼接URL进行传递参数: POST:通过body体传输 ...
- 【原创】浅谈指针(十一)alloca函数
前言 好几天没写了,最近网课,事情也比较多,今天多写点东西. 目录 前言 alloca函数 1.简介 2.反汇编看alloca 3.手工调用alloca函数 4.注意事项 alloca函数 1.简介 ...