23 Design Modes (18)-Memo mode

Source: Internet
Author: User
23 Design Modes (18)-Memo mode

Definition: captures the internal state of an object without compromising encapsulation, and saves the state outside that object. This allows the object to be restored to its previously saved state.


Type: Behavior class


Class Diagram:

When we are programming, we often need to save the middle state of the object, and when necessary, we can revert to this state. For example, when we are programming with eclipse, if we write a mistake (such as accidentally accidentally deleting a few lines of code), we want to return to the state before deletion, we can use CTRL + Z to return. At this point we can use the memo mode to achieve.


Structure of the memo pattern

Initiator: Records the internal state of the current moment and is responsible for defining which state is part of the backup scope and responsible for creating and recovering Memo data.

Memo: Responsible for storing the internal state of the initiator object, providing the internal state required by the initiator when needed.

Administrative roles: Manage memos, save and provide memos.


Common Code implementations

Class Originator {
Private String state = "";

Public String getState () {
return state;
}
public void SetState (String state) {
This.state = State;
}
Public Memento Creatememento () {
return new Memento (this.state);
}
public void Restorememento (Memento Memento) {
This.setstate (Memento.getstate ());
}
}

Class Memento {
Private String state = "";
Public Memento (String state) {
This.state = State;
}
Public String getState () {
return state;
}
public void SetState (String state) {
This.state = State;
}
}
Class Caretaker {
Private Memento Memento;
Public Memento Getmemento () {
return memento;
}
public void Setmemento (Memento Memento) {
This.memento = Memento;
}
}
public class Client {
public static void Main (string[] args) {
Originator originator = new originator ();
Originator.setstate ("State 1");
SYSTEM.OUT.PRINTLN ("Initial state:" +originator.getstate ());
Caretaker caretaker = new Caretaker ();
Caretaker.setmemento (Originator.creatememento ());
Originator.setstate ("State 2");
SYSTEM.OUT.PRINTLN ("changed state:" +originator.getstate ());
Originator.restorememento (Caretaker.getmemento ());
System.out.println ("Post-Restore Status:" +originator.getstate ());
}
}


The code demonstrates an example of single-state single-backup, which is very simple: the state variable in the originator class needs to be backed up so that it can be restored when needed; in the memento class, there is also a status variable that stores the temporary states of the originator class. Whereas the caretaker class is used to manage memo classes to write state to or retrieve state from a memo object.

Multi-State multi-backup memo

In the example of a generic code demonstration, the originator class has only one state variable that needs to be backed up, and typically, the initiator role is usually a javabean, with more than one variable in the object that needs to be backed up, and more than one backup, which is a multi-state multi-backup memo.

There are many ways to implement a memo, the memo pattern has a lot of deformation and processing methods, such as general code in a way that is generally not used, in most cases, the memo mode is multi-state multi-backup. In fact, multi-state multi-backup is also very simple, the most common method is that we add a map container in the memento to store all the state, in the caretaker class also use a map container to store all the backups. Here we give an example of a multi-state multi-backup:

Class Originator {
Private String state1 = "";
Private String State2 = "";
Private String State3 = "";

Public String getState1 () {
return state1;
}
public void SetState1 (String state1) {
This.state1 = state1;
}
Public String GetState2 () {
return state2;
}
public void SetState2 (String state2) {
This.state2 = State2;
}
Public String GetState3 () {
return state3;
}
public void SetState3 (String state3) {
This.state3 = State3;
}
Public Memento Creatememento () {
return new Memento (Beanutils.backupprop (this));
}

public void Restorememento (Memento Memento) {
Beanutils.restoreprop (this, Memento.getstatemap ());
}
Public String toString () {
Return "state1=" +state1+ "state2=" +state2+ "state3=" +STATE3;
}
}
Class Memento {
Private map<string, object> Statemap;

Public Memento (map<string, object> Map) {
This.statemap = map;
}

Public map<string, Object> Getstatemap () {
return statemap;
}

public void Setstatemap (map<string, object> statemap) {
This.statemap = Statemap;
}
}
Class Beanutils {
public static map<string, object> Backupprop (Object Bean) {
map<string, object> result = new hashmap<string, object> ();
try{
BeanInfo BeanInfo = Introspector.getbeaninfo (Bean.getclass ());
propertydescriptor[] descriptors = beaninfo.getpropertydescriptors ();
for (PropertyDescriptor des:descriptors) {
String fieldName = Des.getname ();
Method getter = Des.getreadmethod ();

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.