zoukankan      html  css  js  c++  java
  • 【设计模式】——备忘录模式

    备忘录(Memento):在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。

      Originator(发起人):负责创建一个备忘录Memento,用以记录当前时刻它的内部状态,并可使用备忘录恢复内部状态。Originator可根据需要决定Memento存储Originator的那些状态。Memento(备忘录):负责存储Originator对象的内部状态,并可防止Originator以外的对象访问备忘录Memento。备忘录有两个接口,Caretaker只能看到备忘录的窄接口,它只能将备忘录传递给其他对象。Originator能够看到一个宽接口,允许它访问返回到先前状态所需的所有数据。Caretaker(管理者):负责保存好备忘录Memento,不能对备忘录的内容进行操作或检查。这当中要保存的细节给封装在了Memento中,那一天要更改保存的细节也不用影响客户端了。

      代码基本模板

    #include <iostream>
    
    using namespace std;
    //备忘录(Memento)类
    class Memento
    {
    private:
        string m_state;
    public:
        Memento(string state):m_state(state)
        {
        }
        string getState()
        {
            return this->m_state;
        }
    };
    //发起人(Originator)类
    class Originator
    {
    private:
        string m_state;
    public:
        void setState(string state)
        {
            this->m_state=state;
        }
        string getState()
        {
            return this->m_state;
        }
        //创建备忘录,将当前需要保存的信息导入并实例化一个Memento对象
        Memento *CreateMemento()
        {
            return (new Memento(this->m_state));
        }
        //恢复备忘录,将Memento导入并将相关数据恢复
        void SetMemento(Memento *memento)
        {
            this->m_state=memento->getState();
        }
        void Show()
        {
            cout << "State=" << this->m_state << endl;
        }
    };
    //管理者(Caretaker)类
    class Caretaker
    {
    private:
        Memento *m_memento;
    public:
        //设置备忘录
        void setMemento(Memento *memento)
        {
            this->m_memento=memento;
        }
        //得到备忘录
        Memento *getMemento()
        {
            return this->m_memento;
        }
    };
    int main()
    {
        Originator *O=new Originator();
        O->setState("On");
        O->Show();
        Caretaker *c=new Caretaker();
        c->setMemento(O->CreateMemento());
        O->setState("Off");
        O->Show();
        O->SetMemento(c->getMemento());
        O->Show();
        return 0;
    }

    备忘录所用场合:Memento模式比较适用于功能比较复杂的,但需要维护或记录属性历史的类,或者需要保存的属性只是众多属性中的一小部分时,Originator可以根据保存的Memento信息还原到前一状态。如果在某个系统中使用命令模式时,需要实现命令的撤销功能,那么命令模式可以使用备忘录模式来保存可撤销操作的状态。有时一些对象的内部信息必须保存在对象以外的地方,但是必须要有对象自己读取,这时,使用备忘录可以把复杂的对象内部信息对其他的对象屏蔽起来,从而可以恰当地保持封装的边界。其实最大的作用是在当角色的状态改变的时候,有可能这个状态无效,这时候就可以使用暂时存储起来的备忘录将状态复原。

    游戏进度备忘

    #include <iostream>
    
    using namespace std;
    class RoleStateMemento
    {
     private:
        int m_vit;
        int m_atk;
        int m_def;
     public:
        RoleStateMemento(int vit,int atk,int def)
        {
            this->m_vit=vit;
            this->m_atk=atk;
            this->m_def=def;
        }
        int getVit()
        {
            return this->m_vit;
        }
        int getAtk()
        {
            return this->m_atk;
        }
        int getDef()
        {
            return this->m_def;
        }
    };
    class GameRoles
    {
    private:
        int m_vit;
        int m_atk;
        int m_def;
    public:
        RoleStateMemento *SaveState()
        {
            return (new RoleStateMemento(this->m_vit,this->m_atk,this->m_def));
        }
        void RecoveryState(RoleStateMemento *memento)
        {
            this->m_vit=memento->getVit();
            this->m_atk=memento->getAtk();
            this->m_def=memento->getDef();
        }
        void GetInitState()
        {
            this->m_vit=100;
            this->m_atk=100;
            this->m_def=100;
        }
        void Fight()
        {
            this->m_vit=0;
            this->m_atk=0;
            this->m_def=0;
        }
        void StateDisplay()
        {
            cout << "角色当前状态:" << endl;
            cout << "体力:" << this->m_vit << endl;
            cout << "攻击力:" << this->m_atk << endl;
            cout << "防御力:" << this->m_def << endl;
        }
    };
    class RoleStateCaretaker
    {
    private:
        RoleStateMemento *m_memento;
    public:
        void SetMemento(RoleStateMemento *memento)
        {
            this->m_memento=memento;
        }
        RoleStateMemento *GetMemento()
        {
            return this->m_memento;
        }
    };
    int main()
    {
        //大战Boss前
        GameRoles *lixiaoyao=new GameRoles();
        lixiaoyao->GetInitState();
        lixiaoyao->StateDisplay();
        //保存进度
        RoleStateCaretaker *stateAdmin=new RoleStateCaretaker();
        stateAdmin->SetMemento(lixiaoyao->SaveState());
        //大战Boss时,损耗严重
        lixiaoyao->Fight();
        lixiaoyao->StateDisplay();
        //恢复之前状态
        lixiaoyao->RecoveryState(stateAdmin->GetMemento());
        lixiaoyao->StateDisplay();
        return 0;
    }
  • 相关阅读:
    ios开发之--把秒转换为天时分秒
    网络爬虫的类型
    网络爬虫的组成
    为什么要学网络爬虫
    什么是网络爬虫
    Windows 下安装 Python3
    Linux 下安装 Python3
    HTTP 代理
    HTTP Cookies
    爬虫的基本原理
  • 原文地址:https://www.cnblogs.com/awy-blog/p/3828889.html
Copyright © 2011-2022 走看看