java
//Caretaker.java
package shiyan20;
public class Caretaker
{
private Memento[] array = null;
private int index;
private Memento memento;
public Memento getMemento()
{
return memento;
}
public void setMemento(Memento memento)
{
this.memento=memento;
}
public Caretaker(){
array = new Memento[10];
index = 0;
}
public void addMemento(Memento Memento){
array[index++] = Memento;
}
public Memento preMemento(){
Memento pop = null;
try {
pop = array[--index];
} catch (Exception e) {
e.printStackTrace();
}
return pop;
}
}
//Memento.java
package shiyan20;
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 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;
}
}
//UserInfoDTO.java
package shiyan20;
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("账号是" + this.account);
System.out.println("密码是" + this.password);
System.out.println("电话是" + this.telNo);
}
}
//main.java
package shiyan20;
public class main {
public static void main(String args[]) {
UserInfoDTO user = new UserInfoDTO();
Caretaker c = new Caretaker();
user.setAccount("zhangsan");
user.setPassword("123456");
user.setTelNo("130000");
System.out.println("状态一:");
user.show();
c.addMemento(user.saveMemento());
System.out.println("-----------------");
user.setPassword("111111");
user.setTelNo("130111");
System.out.println("状态二:");
user.show();
c.addMemento(user.saveMemento());
System.out.println("-----------------");
user.setPassword("222222");
user.setTelNo("130222");
System.out.println("状态三:");
user.show();
System.out.println("-----------------");
user.restoreMemento(c.preMemento());
System.out.println("回到状态二:");
user.show();
System.out.println("-----------------");
user.restoreMemento(c.preMemento());
System.out.println("回到状态一:");
user.show();
System.out.println("-----------------");
}
}
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;
}