备忘录模式(二)

#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("Lil ghost");
    user.setPassword("123456");
    user.setTelNo("110");
    cout << "状态一:" << endl;
    taker.setMemento(user.saveMemento());
    index++;
    user.show();
    cout << "----------------------------------" << endl;

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

    user.setPassword("666666");
    user.setTelNo("119");
    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-11-07 19:51  今天又双叒叕在敲代码  阅读(19)  评论(0编辑  收藏  举报