Design mode 14: Memo Mode (Memento)

Source: Internet
Author: User

Memo mode:
Without breaking the package, capture the internal state of an object and save the state to the outside of the object so that the object can be restored to the saved state.
Without violating encapsulation, capture and externalize an object ' s internal state so, the object can restored to This state later

UML diagram:

Mainly include:

    1. Memento (Memento): Stores the internal state of the originator. You can also prevent objects other than originator from accessing the memo memento.
    2. Originator (Salesprospect): Creates a memo memento that contains the current state snapshot and can use memento to restore the previously saved state.
    3. Caretaker (Prospectmemory): Responsible for keeping the memorandum memento.

This design pattern is very simple, is the object-oriented encapsulation of the specific use of this feature.

C + + code:

#include <iostream>#include <string>using namespace STD;classmemento{ Public: Memento (strings) {state=s; }stringGetState () {returnState }Private:stringState;};classoriginator{ Public:voidSetmemento (Memento *m) {state=m->getstate (); } Memento * Creatememento () {return NewMemento (state); }voidSetState (strings) {state=s; }stringGetState () {returnState }Private:stringState;};classcaretaker{ Public: memento* Getmemento () {returnMemento }voidSetmemento (Memento *m) {memento=m; }Private: Memento * Memento;};intMain () {originator * originator=NewOriginator (); Originator->setstate ("Origin State");STD::cout<<"State 1:"<<originator->getstate () <<STD:: Endl; Caretaker * caretaker=NewCaretaker ();    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;}

Execution output:

The following is a concrete example.

    1. Originator for Salesprospect, which is the agent dealer, he has the name, number, funding these attributes
    2. Memento for Memento, memo for saving reseller attributes
    3. Caretaker for Prospectmemory, which is the class for saving reseller memos

UML Class Diagrams:

The C + + code is as follows:

#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 () {returnName } string Getphone () {returnPhone } double Getbudget () {returnBudget }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 () {returnName } string Getphone () {returnPhone } double Getbudget () {returnBudget }Memento* Savememento () {returnNewMemento(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 () {returnMemento } void Setmemento (Memento* m) {memento=m; }Private:            Memento* Memento;}; int main () {Salesprospect* Sale=newSalesprospect(); Sale->setname ("John"); Sale->setphone ("15245869785"); Sale->setbudget ( -);std::cout<<"Base sale:"<<std:: Endl<<sale->getname() <<std:: Endl<<sale->getphone() <<std:: Endl<<sale->getbudget() <<std:: Endl<<std:: Endl;prospectmemory* Prospect=newprospectmemory();    Prospect->setmemento (Sale->savememento ()); Sale->setname ("Mike."); Sale->setphone ("13685478523"); Sale->setbudget ( the);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;}

Execution output:

Design mode 14: Memo Mode (Memento)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.