Memo Mode of design mode

Source: Internet
Author: User

Memo mode, translated into marker mode is better, because this design pattern is designed to back out: Gof the definition of the memo pattern is to capture the internal state of an object without destroying the encapsulation, and to save the state outside of that object. The object can then be restored to its previously saved state.

In the previous command pattern, we mentioned that the function of undo and redo can be realized by using the middle command role. It can be seen from the definition that the memo mode is dedicated to storing the object's historical state, which is very helpful for implementing the undo and redo functions very well. So in command mode, the undo and redo functions can be implemented with the memo mode.

It is simple to implement the ability to save the state of an object at a certain point in time, but in order for the Backup object to access the attributes in the original object, it means that the private properties of the object may be exposed, which can break the encapsulation. So how does the memo pattern maintain encapsulation?

Take a look at the structure of the memo pattern:

1) Memo (Memento) Role: Memo role store internal state of "memo initiator role". The memo initiation role determines which internal states of the memos role store "Memo initiation role" as needed. To prevent the memo from being accessed by objects other than "Memo initiation role". A memo actually has two interfaces, and the memo manager role sees only the narrow interface provided by the memo-the attributes that are stored in the memo role are not visible. The memo initiation role is able to see a wide interface --the ability to get the attributes that you put into the memo role.
2) Memo Initiation (originator) Role: "Memo initiates role" creates a memo to record its internal state at the current moment. Use memos to restore internal state when needed.
3) Memo Manager (caretaker) role: responsible for keeping memos. The contents of the memo cannot be manipulated or checked.

The memo role is kept intact as required by the definition. The best case scenario is that the memo role should only expose the interface for the operation's internal storage properties to the "Memo initiation role." For other roles, it is not visible.

Here are three ways to save packages in Java.
The first is to use two different interface classes to restrict access. In these two interface classes, a method that provides a more complete operation state, we call it a wide interface, and the other can be just an indicator, which we call a narrow interface. The memo role implements both interface classes. This allows for the "Memo initiation role" to be accessed with a wide interface, while for other roles or objects it is accessed with a narrow interface. This implementation is simple, but requires an artificial specification constraint-often without force.
The second approach solves the first flaw: the use of internal classes to control access rights. A private inner class that takes the memo role as the "Memo initiation role". The benefits I don't explain in detail, look at the code to understand. The following code is a complete memo mode for the teaching program. It uses the second method to implement the memo pattern.

1 classoriginator{2     //This is the state to save.3    Private intState= 90;4     //object that maintains a memo manager role5    PrivateCaretaker C =Newcaretaker ();6     //read the memo role to restore the previous state7     Public voidSetmemento () {8Memento Memento =(Memento) C.getmemento ();9State =memento.getstate ();TenSystem.out.println ("The state was" +state+ "Now"); One     } A     //Create a memo role and deposit the current status attribute to the memo manager role.  -     Public voidCreatememento () { -C.savememento (NewMemento (state)); the     } -     //This is the other business methods ... -     //They maybe modify the attribute state -      Public voidModifystate4test (intm) { +State =m; -System.out.println ("The state was" +state+ "Now"); +     } A     //as a memo role for the private inner class, it implements a narrow interface, and you can see that the wide interface is no longer needed in the second method at    //Note: The properties and methods inside are private. -    Private classMementoImplementsmementoif{ -         Private intState ; -         PrivateMemento (intState ) { -             This. State =State ; -         } in         Private intgetState () { -            returnState ; to          } +    } - } the //test Code--client program *  Public classtestinnerclass{ $      Public Static voidMain (string[] args) {Panax NotoginsengOriginator o =Neworiginator (); - O.creatememento (); theO.modifystate4test (80); + O.setmemento (); A     } the } + //Narrow Interface - Interfacemementoif{} $     //"Memo Manager role" $     classcaretaker{ -         Privatemementoif m; -          Public voidSavememento (mementoif m) { the          This. m =m; -     }Wuyi      Publicmementoif Getmemento () { the         returnm; -     } Wu}

The third Way is not recommended: Use the Clone method to simplify the memo pattern. Because Java provides the clone mechanism, it makes it easier to replicate an object. Using the clone mechanism of the memo mode, the memo role can basically be omitted, and can be very good to maintain the encapsulation of objects. But be careful when implementing the Clone method for your class.

Memo Mode of design 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.