备忘录模式和策略模式

发布时间:2020-07-16 23:02:15 作者:汇天下豪杰
来源:网络 阅读:426

1、备忘录模式

  保存对象的状态,在需要的时候在恢复出来即可!!!


2、具体实现

  (1)、代码实现

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

class Mem{ //备忘录类
    public:
        Mem(string name, int age){
            m_name = name;
            m_age = age;
        }   
        string getName(){
            return m_name;
        }   
        int getAge(){
            return m_age;
        }   
        void setName(string name){
            m_name = name;
        }   
        void setAge(int age){
            m_age = age;
        }   

    private:
        string m_name;
        int m_age;
};

class Person{
    public:
        Person(string name, int age){
            m_name = name;
            m_age = age;
        }
        string getName(){
            return m_name;
        }
        int getAge(){
            return m_age;
        }
        void setName(string name){
            m_name = name;
        }
        void setAge(int age){
            m_age = age;
        }

        //保存
        Mem *createMem(){
            return new Mem(m_name, m_age);
        }        
        //还原
        void setMem(Mem *mem){
            this->m_age = mem->getAge();
            this->m_name = mem->getName();
        }
        void printT(){
            cout<<"m_name: "<<m_name<<"m_age: "<<m_age<<endl;
        }
    private:
        string m_name;
        int m_age;
};

//增加一个备忘录的管理者!!!
class Manager{
    public:
        Manager(Mem *m){
            this->m = m;
        }
        Mem *getMem(){
            return m;
        }
        void setMem(Mem *m){
            this->m = m;        
        }
    private:
        Mem *m; //持有一个备忘录的引用!!!
};

int main(void){
    /*
    Mem *m = NULL;
    Person *p = new Person("张三",32);
    p->printT();
    //创建Person对象的一个状态
    cout<<"---------------------------"<<endl;
    m = p->createMem();
    p->setAge(42);
    p->printT();

    cout<<"还原旧的状态"<<endl;
    p->setMem(m);
    p->printT();

    delete p;
    */

    Manager *m1;      
    Person *p = new Person("张三",32);
    p->printT();
    //创建Person对象的一个状态
    cout<<"---------------------------"<<endl;
    m1 = new Manager(p->createMem());
    p->setAge(42);
    p->printT();

    cout<<"还原旧的状态"<<endl;
    p->setMem(m1->getMem());
    p->printT();

    delete p;

    return 0;
}

  (2)、结果如下

备忘录模式和策略模式


3、策略模式

 准备一组算法,并将每一个算法封装起来,使得它们可以互换;


4、具体实现

  (1)、代码如下

#include<iostream>
using namespace std;

//策略模式:准备一组算法,并将每一个算法封装起来,使得它们可以互换;
 
class Strategy{
    public:
        virtual void crypt() = 0;
    private:
};

class AES : public Strategy{
    public:
        virtual void crypt(){
            cout<<"AES加密算法"<<endl;
        }   
    private:

}; 

class DES : public Strategy{
    public:
        virtual void crypt(){
            cout<<"DES加密算法"<<endl;
        }   
    private:

};

class Context{  //组合算法的抽象类
    public:
        void setStrategy(Strategy *strategy){
            this->strategy = strategy;
        }
        void myoperator(){
            strategy->crypt();
        }

    private:
        Strategy *strategy;
};
int main(void){
    //1、直接调用的方法;
    /*
    DES *des = new DES;
    des->crypt();
    delete des;
    */

    //2、客户端可以自由的进行策略的更换;
    //Strategy *des = new DES;
    Strategy *des = new AES;    
    Context *context = new Context;
    context->setStrategy(des);
    context->myoperator();

    delete des;
    delete context;

    return 0;
}

  (2)、运行结果

备忘录模式和策略模式




推荐阅读:
  1. 怎么实现php策略模式
  2. 使用备忘录模式实现Undo和Redo

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

模式 策略模式 备忘录

上一篇:Ado.net EntityFramework的Update最简单方法

下一篇:实现VLAN间路由

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》