码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式之十四:备忘录模式(Memento)

时间:2015-06-09 17:13:49      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:设计模式   备忘录模式   

备忘录模式:
在不破换封装性的前提下,捕获一个对象的内部状态并将这个状态保存到对象外部,这样这个对象之后可以恢复到保存的状态。
Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later

UML图:
技术分享

主要包括:

  1. Memento(Memento):存储Originator的内部状态。并可防止Originator以外的其他对象访问备忘录Memento。
  2. Originator(SalesProspect):创建一个包含当前状态快照的备忘录Memento,并可以使用Memento来恢复之前保存的状态。
  3. Caretaker(ProspectMemory):负责保存好备忘录Memento。

这个设计模式很简单,就是面向对象封装这个特性的具体使用。

C++代码:

#include <iostream>
#include <string>
using namespace std;
class Memento
{
        public:
        Memento(string s)
        {
            state=s;    
        }
        string getState()
        {
            return state;
        }
        private:
                string state;


};

class Originator
{
        public:
                void setMemento(Memento *m)
                {
                    state=m->getState();
                }
                Memento * createMemento(){
                    return new Memento(state);
                }
                void setState(string s)
                {
                    state=s;
                }
                string getState()
                {
                    return state;
                }
        private:
                string state;
};


class Caretaker
{
    public:
            Memento* getMemento()
            {
                return memento;
            }
            void setMemento(Memento *m)
            {
                memento=m;
            }
    private:
            Memento * memento;

};
int main()
{
    Originator * originator=new Originator();
    originator->setState("origin state");
    std::cout<<"state 1:"<<originator->getState()<<std::endl;
    Caretaker * caretaker=new Caretaker();
    caretaker->setMemento(originator->createMemento());
    originator->setState("change to state");
    std::cout<<"state 2:"<<originator->getState()<<std::endl;
    originator->setMemento(caretaker->getMemento());
    std::cout<<"state 3:"<<originator->getState()<<std::endl;

    return 0;
}

执行输出:

技术分享

下面是一个以具体的例子。

  1. Originator为SalesProspect,即代理经销商,他有姓名,号码,经费这些属性
  2. Memento为Memento,为保存经销商属性的备忘录
  3. Caretaker为ProspectMemory,即保存经销商备忘录的类

UML类图:
技术分享

C++代码如下:

#include <string>
#include <iostream>

using namespace std;

class Memento
{
        public:
                Memento(string n,string p,double b)
                {
                    name=n;
                    phone=p;
                    budget=b;
                }
                string getName()
                {
                    return name;
                }
                string getPhone()
                {
                    return phone;
                }
                double getBudget()
                {
                    return budget;
                }
        private:
                string name;
                string phone;
                double budget;
};

class SalesProspect
{
        public:
                void setName(string n)
                {
                    name=n;
                }
                void setPhone(string p)
                {
                    phone=p;
                }
                void setBudget(double b)
                {
                    budget=b;
                }
                string getName()
                {
                    return name;
                }
                string getPhone()
                {
                    return phone;
                }
                double getBudget()
                {
                    return budget;
                }
                Memento* saveMemento()
                {
                    return new Memento(name,phone,budget);
                }
                void restoreMemento(Memento * m)
                {
                    name=m->getName();
                    phone=m->getPhone();
                    budget=m->getBudget();
                }
        private:
                string name;
                string phone;
                double budget;
};

class ProspectMemory
{
        public:
            Memento * getMemento()
            {
                return memento;
            }
            void setMemento(Memento * m)
            {
                memento=m;
            }
        private:
            Memento * memento;
};

int main()
{
    SalesProspect * sale=new SalesProspect();
    sale->setName("John");
    sale->setPhone("15245869785");
    sale->setBudget(1200);

    std::cout<<"base sale:"<<std::endl<<sale->getName()<<std::endl<<sale->getPhone()<<std::endl<<sale->getBudget()<<std::endl<<std::endl;

    ProspectMemory * prospect=new ProspectMemory();
    prospect->setMemento(sale->saveMemento());

    sale->setName("Mike");
    sale->setPhone("13685478523");
    sale->setBudget(1500);

    std::cout<<"change sale:"<<std::endl<<sale->getName()<<std::endl<<sale->getPhone()<<std::endl<<sale->getBudget()<<std::endl<<std::endl;

    sale->restoreMemento(prospect->getMemento());

    std::cout<<"restore sale:"<<std::endl<<sale->getName()<<std::endl<<sale->getPhone()<<std::endl<<sale->getBudget()<<std::endl<<std::endl;

    delete sale;
    delete prospect;
    return 0;
}

执行输出:

技术分享

设计模式之十四:备忘录模式(Memento)

标签:设计模式   备忘录模式   

原文地址:http://blog.csdn.net/u012501459/article/details/46428389

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!