The meaning of the memo pattern is that in the program we may encounter the problem of undoing the operation and returning the object to a state. A rollback of a similar transaction.
There are three main objects in a memo pattern
Initiator (originator): a member to back up that provides a way to create a memo, copies its own information into a memo, and provides another way to overwrite the information in the memo with its own information.
Memo (Memento): The Memo object contains a member variable that stores the initiator state, which provides the Set,get or constructor method to save the initiator state and get the initiator state.
Management Role (caretaker): an implementation class for managing memo objects.
Sponsor
Package memento;/** * @author Mercy * Initiator */public class Originator {private String state;//Create memo public Memento creat Emenmento () {return new Memento (state);} Restore public void RollBack (Memento Memento) {state = Memento.getstate ();} Public String GetState () {return state;} public void SetState (String state) {this.state = state; System.out.println ("Current status is:" + state);}}
Memo
Package memento;/** * @author Mercy * Memo */public class Memento {//Status private string State;public string GetState () {return State;} public void SetState (String state) {this.state = state;} Constructor initialization status public Memento (String state) {this.state=state;}}
Managers
Package memento;/** * @author Mercy * Manager, Management Memo */public class caretaker {private Memento memento;public memento Getmemento () {return memento;} public void Setmemento (Memento Memento) {this.memento = Memento;}}
Test
Package Memento;public class Test {public static void main (string[] args) {//Create initiator object originator or=new originator ();//Set Initial Status Or.setstate ("on");//Create Manager object caretaker ct=new caretaker ();//Set Backup Ct.setmemento (Or.createmenmento ());// Change the status or.setstate ("Off");//Restore Or.rollback (Ct.getmemento ()); System.out.println (Or.getstate ());}}
Results
The current state is: on the current state is: Offon
Java design mode----Memo Mode