20. Memorandum Mode

Source: Internet
Author: User

Memorandum mode:

Memento: capture the internal state of an object without compromising encapsulation, and save the state outside the object. In order to restore the object to the original save state.

 

Demo:

// Memorandum Mode

Class Program

{

Static void main (string [] ARGs)

{

Originator o = new originator ();

O. State = "on ";

O. Show ();

// Instantiate the manager and save the status

Caretaker c = new caretaker ();

C. mement0 = O. creatememento ();

 

O. State = "off ";

O. Show ();

// Restore status

O. setmemento (C. mement0 );

O. Show ();

 

Console. readkey ();

}

}

 

Class Originator

{

Private string state; // saved attributes

Public String state

{

Get {return state ;}

Set {state = value ;}

}

Public memento creatememento () // create a memorandum to import and instantiate a memento object.

{

Return (new memento (State ));

}

Public void setmemento (memento) // restore Memorandum

{

State = memento. State;

}

Public void show ()

{

Console. writeline ("state =" + State );

}

}

 

// Memorandum

Class memento

{

Private string state;

Public memento (string state)

{

This. State = State;

}

Public String state

{

Get {return state ;}

}

}

 

// Manager class

Class caretaker

{

Private memento;

Public memento mement0 // get or set the Memorandum

{

Get {return memento ;}

Set {memento = value ;}

}

}

 

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.