Java design mode----Memo Mode

Source: Internet
Author: User

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

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.