Memo Mode C + + implementation 1 Definition memento pattern
captures the internal state of an object without compromising encapsulation, and saves the state outside that object. You can then restore the object to its original state
2 class Diagram
3 implementation
Class originator
{
Private
string State;
Public
String GetState ();
void SetState (string &x);
Memento Creatmemento ()
{
return new Memento (state);
}
void Restorememento (Memento men)
{
SetState (Men.getstate ());
}
};
Class Memento
{
Public
Memento (String st)
: State (ST)
{}
String GetState ()
{
return state;
}
void SetState (String st)
{
state = st;
}
};
Memo Administrator Role
Class caretaker
{
Private
Memento Memento;
Memento Getmemento ()
{return memento;}
void Setmemento (String st)
{memento = st;}
};
Class Client
{
Public
void operator () ()
{
Originator or= new originator ();
Caretaker CA = new Caretaker ();
Admin Memo record set to user defined as Memo status
Ca.setmemento (Or.creatememento ());
The user is re-retired to a state that is logged by the Administrator
Or.restorememento (Ca.getmemento ());
}
};
4 applications
Usage Scenarios
Need to provide relevant state scenarios for saving and recovering data
Provides a rollback rollback operation
A copy scenario that needs to be monitored.
Transaction management for database connections is the memo mode
Precautions:
Statement Period of the memo: function in the nearest code. Delete immediately without using ... The memo is not Hua Tuo alive, dead
Performance of memos: cannot be used in for, cannot be established frequently. Consuming too much resources can be a precursor to design refactoring
5 Extensions
A memorandum of clone mode
Realize
Class originator:p ublic cloneable
{
Private
string State;
Public
void SetState (String st);
Originator Creatormemento ()
{
return Clone ();
}
Restorememento (originator or)
{
SetState (or.getstate);
}
Originator Clone ()
{
is actually a copy of the construction of a
}
};
Class caretaker
{
Private
Originator or;
Originator Getoriginator ()
{
return or;
}
VOID Setoriginator (originator originator)
{
or = originator;
}
}
Since you can merge memo roles, administrators can also merge
∴
Class originator:p ublic cloneable
{
Private
Originator* _backup;
String _state;
Public
Originator (const originator& or)
{}
void SetState ();
void Creatmemento ()
{
_bcakup = new Originator (*this);
}
void Restorememento ()
{
ASSERT (_backup);
Setstat (_backup->getstate);
}
};
Note: Because the clone mode memo may be complex due to the problem of dark-and-dark copies, the Clone mode memo is a simple scenario
② Multi-State memo
Encapsulates multiple states in originator, while Memento saves a hashmap state
③ Multi-Backup memo
Note: The Administrator role encapsulates one of the HashMap encapsulates a memo rather than the memo state
eg
Class caretaker
{
Private
Hashmap<string,memento> Memmap;
Public
Memento Getmemento (stirng index)
{}
void Setmemento (String index,memento me)
{
Memmap.put (Index,me);
}
};
void Test
{
Originator or = new originator ();
Caretaker CA = new Caretaker ();
Ca.setmemento ("001", Or.creatememento ());
Ca.setmemento ("002", Or.creatememento ());
Or.restorememento (Ca.getmemento ("001"));
}
Note: This mode should pay attention to the maximum memo, control the memory
Elevation: Providing permissions issues
Class originator
{
Private
string State;
Public
void SetState (String st);
Imemento Creatememento ()
{
return new Memento (state);
}
void Restorememento (Imemento me)
{
SetState (Me.getstate ());
}
Private
Built-in classes to implement
Class Memento:p ublic imemento
{
Private
string State;
Memento (String st)
{
state = st;
}
String GetState ();
void SetState (String st);
};
};
Class Imemento
{
Public
Virtual ~imemento () = 0;
};
Class caretaker
{
Imemento Memento;
Public
Imemento Getmemento ()
{
return memento;
}
void Setmemento (Imemento mem)
{
Memento = mem;
}
};
Note: The design here: Dual interface design, one is the normal interface of the business, the implementation of the necessary business logic, a wide interface, the other is an empty interface, what methods are not, the purpose is to provide access to the module outside the subsystem, narrow interface. There is no way to manipulate data in a narrow interface, so it is relatively safe.
Design mode--Memo mode C + + implementation