"Design Mode" Memo mode

Source: Internet
Author: User

Memo Mode (Memento pattern) saves a state of an object so that it can be restored at the appropriate time. The memo pattern belongs to the behavioral pattern.

Introduced

Intent: to capture the internal state of an object without destroying the encapsulation, and to save the state outside the object.

Main solution: the so-called memo mode is to capture an object's internal state without destroying the package, and to save the state outside of the object, so that the object can be restored to its previously saved state.

when to use: many times we always need to record the internal state of an object in order to allow the user to cancel an indeterminate or wrong operation, to revert to his original state, and to have a "regret pill" to eat.

How to resolve: Store object state exclusively through a memo class.

Key code: The customer is not coupled with the memo class and is coupled with the memo management class.

Application Example: 1, regret medicine. 2, when playing the game's archive. 3. Ctri + Z in Windows. 4, IE in the back. 4, the database transaction management.

Advantages: 1, to provide users with a mechanism to restore the state, you can make it easier for users to return to a history of the state. 2, the realization of the encapsulation of information, so that users do not need to care about the state of preservation details.

disadvantage: consume resources. If the class has too many member variables, it is bound to occupy a larger resource, and each time the save consumes a certain amount of memory.

usage Scenario: 1, the relevant state scene that needs to save/restore data. 2, provide a rollback operation.

Note: 1, in order to comply with the Dimitri principle, but also to add a management memo class. 2, in order to save memory, you can use the prototype mode + Memo mode.

Realize

Memo mode uses three classes of Memento,originator , and caretaker. The Memento contains the state of the object to be restored. Originator creates and stores state in the Memento object. The caretaker object is responsible for recovering the state of an object from the Memento.

Mementopatterndemo, our demo class uses caretaker and originator objects to display the state recovery of an object.

Step 1

Create the Memento class.

 Public class Memento {   private  String state;     Public Memento (String state) {      the. State = State ;   }      Public String getState () {      return state ;   }    }
Step 2

Create the originator class.

 Public classOriginator {PrivateString State;  Public voidsetState (String state) { This. State =State ; }    PublicString getState () {returnState ; }    PublicMemento Savestatetomemento () {return NewMemento (state); }    Public voidGetstatefrommemento (Memento Memento) { state=memento.getstate (); }}
Step 3

Create the Caretaker class.

Import java.util.ArrayList; Import java.util.List;  Public class Caretaker {   privatenew arraylist<memento>();     Public void Add (Memento state) {      mementolist.add (state);   }     Public Memento get (int  index) {      return  mementolist.get (index);}   }
Step 4

Use caretaker and originator objects.

 Public classMementopatterndemo { Public Static voidMain (string[] args) {originator originator=Neworiginator (); Caretaker caretaker=Newcaretaker (); Originator.setstate ("State #1"); Originator.setstate ("State #2");      Caretaker.add (Originator.savestatetomemento ()); Originator.setstate ("State #3");      Caretaker.add (Originator.savestatetomemento ()); Originator.setstate ("State #4"); System.out.println ("Current state:" +originator.getstate ()); Originator.getstatefrommemento (Caretaker.get (0)); System.out.println ("First saved state:" +originator.getstate ()); Originator.getstatefrommemento (Caretaker.get (1)); System.out.println ("Second saved state:" +originator.getstate ()); }}
Step 5

Verify the output.

Current State:state #4 Firstsaved State:state #2Second saved State:state #3

"Design Mode" Memo mode

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.