• 软件设计备忘录模式


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

    类图

    代码

    Java

    package rjsj.no20;
    
    public class Client {
        public static void main(String[] args) {
            User user = new User();
            Caretaker caretaker = new Caretaker();
    
            user.setId("20193288");
            user.setPassword("123456");
            caretaker.setMemo(user.saveState());
            System.out.println("״̬һ");
            user.show();
            System.out.println("************************************************************");
    
            user.setId("20193288");
            user.setPassword("qwerasdfzxcv");
            caretaker.setMemo(user.saveState());
            System.out.println("״̬¶þ");
            user.show();
            System.out.println("************************************************************");
    
            user.setId("20183288");
            user.setPassword("66666666");
            caretaker.setMemo(user.saveState());
            System.out.println("״̬Ƚ");
            user.show();
            System.out.println("************************************************************");
    
            System.out.println("ְ²½³·Ϻ²ٗ÷£º");
            System.out.println("»ָ´µ½״̬¶þ");
            user.restoreState(caretaker.getMemo());
            user.show();
            System.out.println("************************************************************");
    
            System.out.println("»ָ´µ½״̬һ");
            user.restoreState(caretaker.getMemo());
            user.show();
        }
    }
    package rjsj.no20;
    
    public class Memo {
        private String id;
        private String password;
    
        public Memo(String id, String password) {
            this.id = id;
            this.password = password;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }
    package rjsj.no20;
    
    public class User {
        private String id;
        private String password;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public Memo saveState(){
            return new Memo(id,password);
        }
    
        public void restoreState(Memo m){
            this.id = m.getId();
            this.password = m.getPassword();
        }
    
        public void show(){
            System.out.println("id:" + this.id);
            System.out.println("password:" + this.password);
        }
    }
    package rjsj.no20;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Caretaker {
        private List<Memo> list = new ArrayList<>();
    
        public void setMemo(Memo m){
            list.add(m);
        }
    
        public Memo getMemo(){
            Memo m = list.get( list.size() - 2 );
            list.remove( list.size() - 2 );
            return m;
        }
    }

    C++

    #include<iostream>
    #include <list>
    using namespace std;
    class Memento {
    private:
        string account;
        string password;
    public:
        string getAccount() {
            return account;
        }
        void setAccount(string account) {
            this->account = account;
        }
        string getPassword() {
            return password;
        }
        void setPassword(string password) {
            this->password = password;
        }
        Memento(string account, string password) {
            this->account = account;
            this->password = password;
        }
    };
    class UserInfoDTO {
    private:
        string account;
        string password;
    public:
        string getAccount() {
            return account;
        }
        void setAccount(string account) {
            this->account = account;
        }
        string getPassword() {
            return password;
        }
        void setPassword(string password) {
            this->password = password;
        }
    
        Memento* saveMemento() {
            return new Memento(account,password);
        }
        void restoreMemento(Memento *memento) {
            this->account=memento->getAccount();
            this->password=memento->getPassword();
        }
        void show() {
            cout<<"Account:"<<this->account<<endl;
            cout<<"Password:"<<this->password<<endl;
        }
    };
    class Caretaker {
    private:
        list<Memento*> ll;
    public:
        Memento* getMemento() {
            ll.pop_front();
            Memento* mm=ll.front();
            return mm;
        }
        void setMemento(Memento *memento) {
            ll.push_front(memento);
        }
    };
    int main(){
            UserInfoDTO *user=new UserInfoDTO();
            Caretaker *c=new Caretaker();
    
            user->setAccount("张三");
            user->setPassword("123456");
            cout<<"状态一:"<<endl;
            user->show();
            c->setMemento(user->saveMemento());
            cout<<"-----------------------------"<<endl;
    
            user->setAccount("李四");
            user->setPassword("lisi666");
            cout<<"状态二:"<<endl;
            user->show();
            c->setMemento(user->saveMemento());
            cout<<"-----------------------------"<<endl;
    
            user->setAccount("王五");
            user->setPassword("88888888");
            cout<<"状态三:"<<endl;
            user->show();
            c->setMemento(user->saveMemento());
            cout<<"-----------------------------"<<endl;
    
    
            user->restoreMemento(c->getMemento());
            cout<<"回到状态二:"<<endl;
            user->show();
            cout<<"-----------------------------"<<endl;
    
            user->restoreMemento(c->getMemento());
            cout<<"回到状态一:"<<endl;
            user->show();
            cout<<"-----------------------------"<<endl;
    }

    运行截图

  • 相关阅读:
    记一次5000并发的调试过程
    Thread Local Area内存溢出的处理方法
    关于不停机部署方案的选择
    Spring Cloud版本选择
    python学习记录-机器学习
    SAM宏观生态学空间分析帮助文档
    arcgis raster clip and mask difference 栅格 提取 clip 和 mask 方法的区别
    R语言 重命名目录下所有文件
    R语言并行运算示例 parallel 包
    R语言查看栅格值
  • 原文地址:https://www.cnblogs.com/Arisf/p/15685711.html
Copyright © 2020-2023  润新知