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:
- Memento (Memento): Stores the internal state of the originator. You can also prevent objects other than originator from accessing the memo memento.
- Originator (Salesprospect): Creates a memo memento that contains the current state snapshot and can use memento to restore the previously saved state.
- 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.
- Originator for Salesprospect, which is the agent dealer, he has the name, number, funding these attributes
- Memento for Memento, memo for saving reseller attributes
- 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)