Notes-Liar design mode-18 Memo Mode

Source: Internet
Author: User

Memo Mode (Memento), which captures the internal state of an object without compromising encapsulation, and saves the state outside of that object. The object can then be restored to its previously saved state.
Originator (INITIATOR): Responsible for creating a memo memento that records its internal state at the current moment and can use the memo to restore its internal state. Originator can determine which internal states of the memento storage originator as needed.
Memento (Memo): is responsible for storing the internal state of the originator object and prevents objects other than originator from accessing the memo Memento. The memo has two interfaces, and caretaker can only see the narrow interface of the memo, which can only pass the memo to other objects. Originator can see a wide interface that allows it to access all the data needed to return to the previous state.
Caretaker (manager): Responsible for the preservation of the memorandum Memento, the contents of the memo can not be manipulated or checked.

Memo mode disadvantage: The role state needs to be fully stored in the memo object, and if the state data is much larger, the memo object is very memory-intensive on resource consumption.

Demo1:

    classOriginator {Private stringState ;  Public stringState {Get{returnState ;} Set{state =value;} }         PublicMemento Creatememento () {Console.WriteLine ("Create a Memo"); return NewMemento (state); }         Public voidSetmemento (Memento Memento) {Console.WriteLine ("Storage Memo"); State=Memento.        State; }         Public voidShowstate () {Console.WriteLine ("state={0}", state); }    }
    class Memento    {        privatestring state ;          Public string State        {            getreturn state ;}        }          Public Memento (string State )        {            the. State = State ;        }    } 
    class Caretaker    {        private  Memento Memento;          Public Memento Memento        {            getreturn  Memento;}             Set {memento = value;}        }    }

Test:

            New originator ();             "  on " ;            O.showstate ();             New caretaker ();             = O.creatememento ();             " Off " ;            O.showstate ();            O.setmemento (C.memento);            O.showstate ();

Notes-Liar design mode-18 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.