C # version of the design pattern of the memo mode __c#

Source: Internet
Author: User

Several days did not write a blog, a few days ago on the internet to see a design pattern called memo mode. Think it is more interesting, I wrote a copy of the computer to imitate a small example of backup. Let's look at the introduction of the memo mode first.
The memo mode (Memento pattern), also known as snapshot mode (Snapshot pattern) or token mode, is one of the 23 design patterns of Gof, which belongs to the behavioral pattern.definition(From Gof "Design pattern"): captures the internal state of an object and saves it outside of the object without breaking the closure. The object can then be restored to its previously saved state.Involved Roles: 1.Originator (Initiator): is responsible for creating a memo memento to record the internal state of the current moment itself, and can use the memo to restore the internal state.   Originator can determine which internal state the memento stores itself on demand. 2.Memento (Memo): Responsible for storing the internal state of the originator object and preventing access to the memo to objects other than originator. The memo has two interfaces: caretaker can only see the narrow interface of the memo, he can only pass the memo to other objects.   Originator can see the wide interface of the memo, allowing it to access all the data it needs to return to its previous state. 3.Caretaker (manager): In charge of Memo memento, you cannot access or manipulate memento content.advantages and disadvantages of Memo modeOne, the advantage of the memo mode 1, sometimes some of the initiator object's internal information must be kept outside the initiator object, but must be read by the initiator object itself, at this time, the use of Memo mode can be complex initiator internal information to other objects, so that the packaging can properly maintain the boundaries. 2. This model simplifies the initiation of human beings.   The initiator no longer needs to manage and save versions of its internal state, and the client can manage the versions of those States that they need.   3, when the status of the promoter role changes, it is possible that this state is invalid, this time can be used to temporarily save the memo to restore the state.   Second, the shortcomings of the memo model: 1. If the status of the initiator role needs to be stored in the memo object completely, the memo object will be expensive on resource consumption.   2. When the person responsible for the role of a memo stored, the owner may not know how much storage space this state will occupy, thus unable to remind users of a very expensive operation. 3. When the status of the promoter role changes, it is possible that this agreement is invalid. If the success rate of state change is not high, it is better to take the "if" protocol mode.
Here's the last example I wrote
1. Memo Category

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Namespace Recover.org.lxh
{
    //Memo role Public
   class Memento
    {
        private string status;

        public string status
        {get
            {return 
                Status;
            }
            set {
                Console.WriteLine ("System in:" +this.status);
                status = value;
            }
        }

       Public Memento (String status)
       {
           this.status = status;
       }
    }
}

2. Initiating human
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Namespace Recover.org.lxh
{
   //Initiator public
   class Windowssystem
    {
        private string status;

        public string status
        {get
            {return
                Status; 
            }
            set { 
                status = value; 
            }
        }

      
       Public Memento Createothersystem () {return
           new Memento (status);
       }
       public void Recoversystem (Memento m)
       {
           this.status = m.status;
       }
    }
}

3. Person in charge (memo manager)
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Namespace Recover.org.lxh
{
   //person in charge (memo manager) Public
   class Usercomputer
    {
        private Memento Memento;

        Public Memento Recovermemento ()
        {
            //Recovery system return
            This.memento;
        }

        public void Creatememento (Memento Memento)
        { 
            //save system
            This.memento = Memento;
        }
}}

4. Test class
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using Recover.org.lxh;

Namespace Recover
{
    class program
    {
        static void Main (string[] args)
        {
            Windowssystem Winxp = New Windowssystem (); WINXP system
            Usercomputer user = new Usercomputer ()//user
            winxp.status= "good state";//WINXP in good running condition
            User.creatememento (Winxp.createothersystem ()); The user backs up the system, Winxp the system to produce
            the backup file winxp.status= "bad state";//WINXP in a bad running state
            Winxp.recoversystem ( User.recovermemento ()); The user sends the RESTORE command, the system restores
            Console.WriteLine ("The current system is in" + winxp.status);
            Console.readkey ();}}



Related Article

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.