Overview
Memo mode provides an implementation mechanism for state recovery that allows users to easily return to a specific historical step, and when the new state is invalid or problematic, the state can be restored using a temporarily stored memo, and many software now offer undo (undo) operations, in which a memo pattern is used.
Defined
Memo Mode (Memento pattern): captures the internal state of an object without destroying the package, and saves the state outside the object, so that the object can be restored to its previously saved state at a later time. It is an object-oriented pattern with the alias token.
Realize
Original Hair Generator
/// <summary> ///Chess Pieces Category: The original generator/// </summary> Public classChessman { Public stringLabel {Get;Set; } Public intx {Get;Set; } Public intY {Get;Set; } PublicChessman (stringLabelintXinty) { This. Label =label; This. x =x; This. y =y; } /// <summary> ///Save Current state/// </summary> /// <returns></returns> PublicChessmanmemento Save () {return NewChessmanmemento ( This. Label, This. x, This. Y); } /// <summary> ///revert to a specified state/// </summary> /// <param name= "Memento" ></param> Public voidRestore (list<chessmanmemento> Mementos,intretreatstep) {Chessmanmemento memento; if(Retreatstep >=mementos. Count) Memento=mementos. First (); ElseMemento= Mementos[mementos. Count ()-1-Retreatstep]; This. Label =Memento. Label; This. x =memento.x; This. y =memento.y; } }
Memo
/// <summary> ///Chess Pieces Memo Type: Memo/// </summary> Public classChessmanmemento { Public stringLabel {Get;Set; } Public intx {Get;Set; } Public intY {Get;Set; } PublicChessmanmemento (stringLabelintXinty) { This. Label =label; This. x =x; This. y =y; } }
Managers
/// <summary> ///Chess Pieces Memo Management Category: Responsible person/// </summary> Public classMementocaretaker {PrivateList<chessmanmemento> mementos =NewList<chessmanmemento>(); PublicList<chessmanmemento>Getmemento () {returnmementos; } Public voidAddmemento (Chessmanmemento Memento) {mementos. ADD (Memento); } }
Client
classProgram {Static voidMain (string[] args) { //MemoMementocaretaker MC =NewMementocaretaker (); //ChessChessman chess =NewChessman ("Horse",1,1); Display (chess); Mc. Addmemento (chess. Save ()); //Save state coordinatesChess.y =4; Display (chess); Mc. Addmemento (chess. Save ()); //Save state coordinates 1,4Chess.x =5; Display (chess); Mc. Addmemento (chess. Save ()); //Save state coordinates 5,4Chess.x =7; Chess.y= -; Display (chess); Mc. Addmemento (chess. Save ()); //Save state coordinates 7,13 intRetreatstep =2; Console.WriteLine ("****** undo "{0}" step ******", Retreatstep); Chess. Restore (MC. Getmemento (), retreatstep); //Restore state back Specify the number of stepsdisplay (chess); Console.ReadLine (); } Public Static voiddisplay (Chessman chess) {Console.WriteLine ("pieces ""+ Chess. Label +"The current location is:"+"Section"+ Chess.x +"Line"+"Section"+ Chess.y +"column. "); } }
Summarize
Key Benefits
1, it provides a state recovery implementation mechanism, so that users can easily return to a specific historical step, when the new state is invalid or there is a problem, you can use the temporarily stored memo to restore the state.
2, the memo implements the encapsulation of information, a memo object is a representation of the state of the original sender object, and will not be changed by other code. The memo holds the status of the original, and storing the memo object with a collection of lists, stacks, and so on can enable multiple undo operations.
Main disadvantages
Resource consumption is too large, if you need to save the original class member variable too many, it is inevitable to occupy a large amount of storage space, each time the state of the object is saved need to consume some system resources.
Applicable scenarios
1. Save the whole state or part state of an object at a certain point in time, so that it can revert to the previous state when needed, and implement the undo operation.
2. Prevent external objects from destroying the encapsulation of an object's historical state, and avoid exposing the implementation details of the object's historical state to the outside object.
The memo mode of behavioral pattern