软件设计实验20

实验 20:备忘录模式

[实验任务一]:多次撤销

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

直接放源码:

#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("aaa");
    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 @ 2022-10-30 19:05  Lindseyyip  阅读(24)  评论(0编辑  收藏  举报