改进课堂上的“用户信息操作撤销”实例,使得系统可以实现多次撤销(可以使用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++实现备忘录模式--撤销操作的更多相关文章

  1. 折腾Java设计模式之备忘录模式

    原文地址:折腾Java设计模式之备忘录模式 备忘录模式 Without violating encapsulation, capture and externalize an object's int ...

  2. java设计模式9.备忘录模式、访问者模式、调停者模式

    备忘录模式 备忘录模式又叫快照模式,备忘录对象是一个用来存储另外一个对象内部状态快照的对象.备忘录的用意是在不破坏封装的条件下,将一个对象的状态捕捉,并外部化存储起来,从而可以在将来合适的时候把这个对 ...

  3. java设计模式之备忘录模式

    备忘录模式 备忘录模式是一种软件设计模式:在不破坏封闭的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态.一听到备忘录这个字的时候想起了小小时打的游 ...

  4. 19.java设计模式之备忘录模式

    基本需求 游戏的角色有攻击力和防御力,在大战Boss之前保存自身的状态(攻击力和防御力),当大战Boss之后攻击力和防御力下降,从备忘录对象恢复到大战前的状态 传统方案 一个对象,就对应一个保存对象状 ...

  5. 观世音甘泉活树的故事竟然是Java设计模式:备忘录模式

    目录 定义 意图 主要解决问题 何时使用 优缺点 结构 白箱实现 黑箱实现 多重检查点 观世音甘泉活树的故事 定义 备忘录模式是对象的行为型模式,备忘录对象是一个用来存储另外一个对象内部状态的快照的对 ...

  6. Java设计模式应用——备忘录模式

    备忘录模式主要用于存档.游戏中我们打boss前总会存档,如果打boss失败,则读取存档,重新挑战boss. 可以看出来,备忘录模式一般包括如下数据结构 1. 存档文件:用于恢复备份场景的必要数据: 2 ...

  7. Java/C++实现模板方法模式---数据库操作

    对数据库的操作一般包括连接.打开.使用.关闭等步骤,在数据库操作模板类中我们定义了connDB().openDB().useDB().closeDB()四个方法分别对应这四个步骤.对于不同类型的数据库 ...

  8. Java设计模式学习记录-备忘录模式

    前言 这次要介绍的是备忘录模式,也是行为模式的一种 .现在人们的智能手机上都会有备忘录这样一个功能,大家也都会用,就是为了记住某件事情,防止以后自己忘记了.那么备忘录模式又是什么样子的呢?是不是和手机 ...

  9. 重学 Java 设计模式:实战备忘录模式「模拟互联网系统上线过程中,配置文件回滚场景」

    作者:小傅哥 博客:https://bugstack.cn - 原创系列专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 实现不了是研发的借口? 实现不了,有时候是功能复杂度较高难以实 ...

随机推荐

  1. Python获取当前时间或者当前时间戳【转】

    取得时间相关的信息的话,要用到python time模块,python time模块里面有很多非常好用的功能,你可以去官方文档了解下,要取的当前时间的话,要取得当前时间的时间戳,时间戳好像是1970年 ...

  2. JZ-051-构建乘积数组

    构建乘积数组 题目描述 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]A[1]...A[i-1]A[i+1]...A[n-1]. ...

  3. 自动化安装操作系统(Centos7+PXE+Cobbler+kickstart)

    一.简介 PXE称作是一种引导方式而不是安装方式似乎更加准确,PXE(Pre-boot Execution Environment)是由Intel设计的协议,它可以使计算机通过网络启动,但是有一个前提 ...

  4. 二进制部署1.23.4版本k8s集群-5-部署Master节点服务

    1.安装Docker 在21.22.200三台机器上安装Docker.安装命令: 在21.22.200三台主机上部署Docker. ~]# curl -fsSL https://get.docker. ...

  5. Lua中如何实现类似gdb的断点调试—08支持通过包名称添加断点

    在前一篇中我们支持了通过函数名称来添加断点,我们同时也提到了在Lua中一个函数的名称的并不是确定的.准确的说,Lua中的函数并没有名称,所谓名称其实是保存这个函数值的变量的名称. 于是通过函数名称添加 ...

  6. Net中委托之三委托的高级应用

    1. 使用委托来解决逻辑分离,解除耦合 2.委托的高级应用实例 using System; using System.Collections.Generic; using System.Linq; u ...

  7. mysql数据库-8.0安装及环境搭建

           1.MySQL8.0 For Windows zip包下载地址 https://dev.mysql.com/downloads/file/?id=476233,进入页面后点击底部&quo ...

  8. 在Ubuntu 18.04上使用msmtp和gmail,git-sendemail发送patch给kernel

    这是我第一次发送patch给kernel,虽然仅仅是一个test的patch,但我也觉着很新奇,这也是自己的一个小小的进步. 如下是从各种网站上学到的如何发送patch的总结. 1. Create g ...

  9. RDMA——libibverbs 代码分析(1)

    下载libibverbs最新代码,https://downloads.openfabrics.org/verbs/README.html 为1.2.0版本.后面开始逐步分析libibverbs源码. ...

  10. SINAMICS S120的核心控制单元CU320使用教程,电机模块接线

    SINAMICS是西门子公司新一代的驱动产品,它正在逐步取代现有的MASTERDRIVES及SIMODRIVE系列的驱动系统.SINAMICS S120是集V/f控制.矢量控制和伺服控制于一体的多轴驱 ...