Design Pattern 19-Memorandum pattern of behavior pattern

Source: Internet
Author: User

Definition: MementoPattern captures the internal state of an object without compromising encapsulation, and saves the State outside the object. In this way, the object can be restored to the previously saved state.

Type: object behavior mode.

Overview:

The usage of the memorandum mode is narrow, that is, the complex state needs to be restored to the previous state. In reality, only a very simple state needs to be restored. If you use the memorandum mode, you will feel a little useless. It's like using a cannon to fight mosquitoes. In Head First Design Patterns, the memorandum mode is also listed as one of the uncommon Patterns.

The memorandum mode is relatively simple, that is, it abstracts the States that need to be saved in an object with a complex state and needs to be restored to Originator again, and provides some operation interfaces to encapsulate them into a Memento class. This greatly simplifies the complexity of the Originator class. Because Memento interfaces are in operational status, they should not be open to more people. Therefore, a Caretaker class managing Memento classes is encapsulated here, and provides some simple external interfaces.

In fact, people who have played stand-alone drama games will immediately think of the "ARCHIVE" function. In a sense, the archiving function is an application of memos. Here is an example of my favorite game Warcraft III.

Class diagram:

Participants:

  1. Warcraft, or Originator, must be recorded and restored.
    1. Memento abstracts the internal status of Warcraft and provides operation methods.
      1. Caretaker manages Memento and provides methods for recording and restoring the status, but does not know the specific execution process.

        Sample Code:

        Using System; using System. collections. generic; using System. text; namespace Pattern19 {class Warcrift {private string role; public string Role {get {return role;} set {role = value ;}} public Memento createmento () {return (new Memento (role);} public void SetMemento (Memento memento) {role = memento. role;} public void Start () {Console. writeLine ("There is a" + role) ;}} class Memento {private string role; public Memento (string _ role) {this. role = _ role;} public string Role {get {return role; }}} class Caretaker {private Memento memento; public Memento {get {return memento ;} set {memento = value ;}} class Program {static void Main (string [] args) {// The initial role is hero Warcrift war3 = new Warcrift (); war3.Role = "Hero"; war3.Start (); Caretaker caretaker = new Caretaker (); caretaker. memento = war3.CreateMemento (); // replace the role with Soldier war3.Role = "Soldier"; war3.Start (); // return to the initial state war3.SetMemento (caretaker. memento); war3.Start ();}}}
        Applicability:
        1. You must save the state of an object at a certain time point (partial) so that it can be restored to the previous state later.
          1. If an interface is used to directly obtain these States for other objects, the implementation details of the object will be exposed and the object encapsulation will be broken.

            Advantages and disadvantages:

            1. It reduces the coupling between objects and the complexity of Object Management.
              1. Disadvantages: If you need to store a lot of information, you will be in favor of a very large overhead.

                References:

                1. Design Patterns-reusable Object-Oriented Software basics
                  1. Big talk Design Model


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.