• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
戈瑾
博客园    首页    新随笔    联系   管理    订阅  订阅
备忘录模式——C++实现

问题描述:

改进课堂上的“用户信息操作撤销”实例,使得系统可以实现多次撤销(可以使用HashMap、ArrayList等集合数据结构实现)。

C++代码:

#include<iostream>
#include<string>
#include<vector>
using namespace std;
//备忘录
class Memento {
private:
	string account;
	string password;
	string telNo;
public:
	Memento(string account, string password, string telNo) {
		this->account = account;
		this->password = password;
		this->telNo = telNo;
	}

	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;
	}
};
//发起者
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() {
		Memento memento(account, password, telNo);
		return memento;
	}

	void restoreMenmento(Memento memento) {
		this->account = memento.getAccount();
		this->password = memento.getPassword();
		this->telNo = memento.getTelNo();
	}
	void show() {
		cout << "帐号:" + this->account << endl;
		cout << "密码:" + this->password << endl;
		cout << "电话:" + this->telNo << endl;
	}
};
//管理者
class Caretaker {
private:
	vector<Memento>mementos;
public:
	Memento getMemento(int i) {
		return mementos.at(i);
	}
	void setMemento(Memento memento) {
		mementos.push_back(memento);
	}
};
//测试函数
int main() {
	int index = -1;
	UserInfoDTO user;
	Caretaker taker;

	user.setAccount("张三");
	user.setPassword("123456");
	user.setTelNo("18800000000");
	cout << "状态一:" << endl;
	taker.setMemento(user.saveMemento());
	index++;
	user.show();
	cout << "----------------------------------" << endl;

	user.setPassword("111111");
	user.setTelNo("18800001111");
	cout << "状态二:" << endl;
	taker.setMemento(user.saveMemento());
	index++;
	user.show();
	cout << "----------------------------------" << endl;

	user.setPassword("222222");
	user.setTelNo("18800002222");
	cout << "状态三:" << endl;
	user.show();
	cout << "----------------------------------" << endl;

	cout << "回到状态二:" << endl;
	index--;
	user.restoreMenmento(taker.getMemento(1));
	user.show();
	cout << "----------------------------------" << endl;

	cout << "回到状态一:" << endl;
	index --;
	user.restoreMenmento(taker.getMemento(0));
	user.show();
	cout << "----------------------------------" << endl;

}

  

运行结果:

 

posted on 2021-11-15 23:46  戈瑾  阅读(100)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3