Design mode: Memo Mode (Memento)

Source: Internet
Author: User

captures the internal state of an object without compromising encapsulation, and saves the state outside that object. The object can then be restored to its previously saved state.

Role of Memo mode:
1. origin (originator): Responsible for creating a memo that records the internal state of the current object, which can also be used to reply to the internal state using the memo. At the same time, the original generator can also decide the internal state of memento storage originator as needed.
2. Memo (Memento): Used to store the internal state of the originator, and can prevent objects other than originator from accessing Memento. There are two interfaces in memo memento, where caretaker can only see the narrow interface in the memo, and it can only pass the memo to other objects. Originator can see a wide interface, allowing it to access all data returned to its previous state.
3. person in charge (caretaker): Responsible for keeping the memo, unable to manipulate and access the contents of the memo, can only pass the memo to other objects.

A typical memo code:

publicclass Memento{    private String state;    publicMemento(Oraginator o)    {        state = o.state;    }    publicvoidsetState(String state){        this.state = state;    }    publicgetState()    {        returnthis.state;    }}
Case

People generally play games, even if you have not played the game has seen roommates, friends played games. A lot of games need to be archived, save the current bar and magic value, in case you dare to challenge the boss when the die can be re-read the document.
1 The original generator originator

 Public classoriginator{Private intBloodvalue;Private intMagicvalue; Public Originator(intBloodvalue,intMagicvalue) { This. Bloodvalue = Bloodvalue; This. Magicvalue = Magicvalue; } Public int Getbloodvalue()    {returnBloodvalue; } Public void Setbloodvalue(intBloodvalue) { This. Bloodvalue = Bloodvalue; } Public int Getmagicvalue()    {returnMagicvalue; } Public void Setmagicvalue(intMagicvalue) { This. Magicvalue = Magicvalue; } Public void Display() {System. out. println ("User Current status:"); System. out. println ("Blood Volume:"+getbloodvalue () +"; Blue Amount:"+getmagicvalue ()); } PublicMementoSavememento()    {return NewMemento (Getbloodvalue (), Getmagicvalue ()); } Public void Restorememento(Memento Memento) { This. Bloodvalue = Memento.getbloodvalue (); This. Magicvalue = Memento.getmagicvalue (); }}

2 Memo Memento

 Public classmemento{Private intBloodvalue;Private intMagicvalue; Public int Getbloodvalue()    {returnBloodvalue; } Public void Setbloodvalue(intBloodvalue) { This. Bloodvalue = Bloodvalue; } Public int Getmagicvalue()    {returnMagicvalue; } Public void Setmagicvalue(intMagicvalue) { This. Magicvalue = Magicvalue; } Public Memento(intBloodvalue,intMagicvalue) { This. Bloodvalue = Bloodvalue; This. Magicvalue = Magicvalue; }}

3 person in charge caretaker

publicclass Caretaker{    private Memento memento;    publicgetMemento()    {        return memento;    }    publicvoidsetMemento(Memento memento)    {        this.memento = memento;    }}

4 test Code

Originator originator = new Originator ( -, -);System. out. println("Before fighting BOSS ...");Originator. Display();Archive Caretaker caretaker = new Caretaker ();Caretaker. Setmemento(originator. Savememento());Fighting System. out. println("Fighting ...");Originator. Setbloodvalue( -);Originator. Setmagicvalue( -);Originator. Display();Reply to archive System. out. println("Restore ...");Originator. Restorememento(Caretaker. Getmemento());Originator. Display();

Output Result:

Before fighting BOSS...用户当前状态:血量:100;蓝量:100Fighting...用户当前状态:血量:20;蓝量:20Restore...用户当前状态:血量:100;蓝量:100

In the memo mode, the most important thing is that the memo memento. The memo stores some or all of the status information of the original, which cannot be accessed by other objects, that is, we are not able to store the state information in an object other than the memo, and if the internal state information is violated the encapsulation principle. Therefore, the memo is not accessible to other objects outside of the processing source.

Pros and cons
Advantages:
1. Provides a mechanism for users to recover their status. Can be a user can easily return to a history of the state.
2. The encapsulation of information is realized. So that users do not need to care about the state of the preservation details.
Disadvantages:
1. 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.

Applicable Scenarios
1. Need to save the state or part state of an object at a certain point in time
2. If an interface is used to make these states available to other objects, the object's implementation details are preserved and the object's encapsulation is destroyed, and an object does not want the outside world to directly access its internal state, and the owner can indirectly access its internal state.

Memo Mode in JDK:
Java.util.Date (The Date object implements the memo pattern through a long value inside itself)
Java.io.Serializable

Resources
1.23 Design modes
2. Design patterns in a small number of JDK
3. Design pattern Reading notes-Memo mode

Design mode: Memo Mode (Memento)

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.