首页 > 其他分享 >20.备忘录模式

20.备忘录模式

时间:2022-12-06 11:37:15浏览次数:41  
标签:account password 20 String 模式 备忘录 userInfoDTO telNo public

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

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

代码

Java

Caretaker.java

package test20;

import test19.Member;

import java.util.ArrayList;
import java.util.List;

public class Caretaker {

    private List<Memento> memento = new ArrayList<>();

    public Memento getMemento() {
        Memento m = memento.get(memento.size()-1);
        memento.remove(memento.size()-1);
        return m;
    }

    public void setMemento(Memento memento) {
        this.memento.add(memento);
    }
}

Client.java

package test20;

public class Client {

    public static void main(String[] args) {
        UserInfoDTO userInfoDTO = new UserInfoDTO();
        Caretaker caretaker = new Caretaker();

        // 链式写法需要加注解
        userInfoDTO.setAccount("zhanghao");
        userInfoDTO.setPassword("123456");
        userInfoDTO.setTelNo("456");
        caretaker.setMemento(userInfoDTO.saveMemento());
        System.out.println("状态一");
        System.out.println(userInfoDTO);
        System.out.println("---------------------------------");

        userInfoDTO.setAccount("sdf");
        userInfoDTO.setPassword("213");
        userInfoDTO.setTelNo("456");
        caretaker.setMemento(userInfoDTO.saveMemento());
        System.out.println("状态二");
        System.out.println(userInfoDTO);
        System.out.println("---------------------------------");

        userInfoDTO.setAccount("ytj");
        userInfoDTO.setPassword("345");
        userInfoDTO.setTelNo("567");
        System.out.println("状态三");
        System.out.println(userInfoDTO);
        System.out.println("---------------------------------");

        userInfoDTO.restoreMemento(caretaker.getMemento());
        System.out.println("回到状态二");
        System.out.println(userInfoDTO);
        System.out.println("---------------------------------");
        userInfoDTO.restoreMemento(caretaker.getMemento());
        System.out.println("回到状态一");
        System.out.println(userInfoDTO);
        System.out.println("---------------------------------");
    }

}

Memento.java

package test20;

public 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 test20;

public class UserInfoDTO {

    private String account;
    private String password;
    private String telNo;

    @Override
    public String toString() {
        return "UserInfoDTO{" +
                "account='" + account + '\'' +
                ", password='" + password + '\'' +
                ", 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;
    }

    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("tTelNo:"+this.telNo);
    }

}

C++

#include <iostream>
#include <vector>
using namespace std;

class Memento;

class UserInfoDTO{
private:
    string account;
    string password;
    string telNo;
public:
    void setAccount(string account){ this->account = account;}
    void setPassword(string password){ this->password = password;}
    void setTelNo(string telNo){ this->telNo = telNo;}
    void show(){
        cout<<"Account:"<<account<<";Password:"<<password<<";TelNo:"<<telNo<<endl;
    }
    void restoreMemento(Memento *memento);
    Memento* saveMemento();
};
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 this->account;}
    string getPassword(){return this->password;}
    string getTelNo(){return this->telNo;}
};
void UserInfoDTO::restoreMemento(Memento *memento) {
    this->account = memento->getAccount();
    this->password = memento->getPassword();
    this->telNo = memento->getTelNo();
}
Memento* UserInfoDTO::saveMemento() {
    return new Memento(account,password,telNo);
}
class Caretaker{
private:
    vector<Memento*> mementos;
public:
    Memento* getMemento(){
        Memento* m = mementos.back();
        mementos.pop_back();
        return m;
    }
    void setMemento(Memento* memento){
        this->mementos.push_back(memento);
    }
};

int main(){
    UserInfoDTO *userInfoDTO = new UserInfoDTO();
    Caretaker *caretaker = new Caretaker();

    userInfoDTO->setAccount("zhanghao");
    userInfoDTO->setPassword("123456");
    userInfoDTO->setTelNo("456");
    caretaker->setMemento(userInfoDTO->saveMemento());
    cout<<"状态一"<<endl;
    userInfoDTO->show();
    cout<<"---------------------------------"<<endl;

    userInfoDTO->setAccount("sdf");
    userInfoDTO->setPassword("213");
    userInfoDTO->setTelNo("456");
    caretaker->setMemento(userInfoDTO->saveMemento());
    cout<<"状态二"<<endl;
    userInfoDTO->show();
    cout<<"---------------------------------"<<endl;

    userInfoDTO->setAccount("ytj");
    userInfoDTO->setPassword("345");
    userInfoDTO->setTelNo("567");
    cout<<"状态三"<<endl;
    userInfoDTO->show();
    cout<<"---------------------------------"<<endl;

    userInfoDTO->restoreMemento(caretaker->getMemento());
    cout<<"回到状态二"<<endl;
    userInfoDTO->show();
    cout<<"---------------------------------"<<endl;
    userInfoDTO->restoreMemento(caretaker->getMemento());
    cout<<"回到状态一"<<endl;
    userInfoDTO->show();
    cout<<"---------------------------------"<<endl;
}

标签:account,password,20,String,模式,备忘录,userInfoDTO,telNo,public
From: https://www.cnblogs.com/java-six/p/16954724.html

相关文章