Quick understanding of design patterns series: 19. Memento Pattern)

Source: Internet
Author: User

Four roles: Originator, Memento, Caretaker, and Client)

Originator: a prototype that requires a memorandum to remember its internal state. You can save the status of this category to the memorandum, or restore the status through the memorandum.

Memento: maintains the internal status of the primary machine,

Caretaker: The memorandum of care, which cannot be modified.

Client: Execute the master node to save the Memorandum and restore it.

Implementation: The client produces the primary machine. When the primary machine saves the memorandum to the viewer, the primary machine continues to change its status, when necessary, obtain the saved status from the memo of the viewer to update the status of the primary server.

Class diagram:

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1R13L101-0.gif "/>

 

Application scenarios: Phone memo saved.

Analysis: When someone's phone number is saved to the memo, if it needs to be restored, the custodian can call the memo to restore the original class.

In the console, we will demonstrate how to use Memento Pattern:

1. Originator ):

 

 
 
  1. // Originator
  2. Class PhoneOriginator
  3. {
  4. Public string Name {get; set ;}
  5. Public string Number {get; set ;}
  6. Public PhoneMemento SaveMemento ()
  7. {
  8. Return new PhoneMemento (Name, Number );
  9. }
  10. Public void ResumeMemento (phonemento PhoneMemento)
  11. {
  12. This. Name = phonemento. Name;
  13. This. Number = phonemento. Number;
  14. }
  15. Public void Display ()
  16. {
  17. Console. WriteLine ("------------------");
  18. Console. WriteLine (Name );
  19. Console. WriteLine (Number );
  20. }
  21. }

Ii. Memorandum (Memento ):

 

 
 
  1. // Memento Memorandum
  2. Class PhoneMemento
  3. {
  4. Public string Name {get; set ;}
  5. Public string Number {get; set ;}
  6. Public PhoneMemento (string name, string number)
  7. {
  8. This. Name = name;
  9. This. Number = number;
  10. }
  11. }

3. Caretaker ):

 

 
 
  1. // Caretaker Viewer
  2. Class PhoneCaretaker
  3. {
  4. Public PhoneMemento Memento {set; get ;}
  5. }

4. Client ):

 

 
 
  1. // Client
  2. Class Program
  3. {
  4. Static void Main (string [] args)
  5. {
  6. PhoneOriginator po = new PhoneOriginator ();
  7. Po. Name = "James ";
  8. Po. Number = "13880191995 ";
  9. Po. Display ();
  10. PhoneCaretaker pcaretaker = new PhoneCaretaker ();
  11. Pcaretaker. Memento = po. savemento ();
  12.  
  13. Po. Name = "Lili ";
  14. Po. Number = "13669595999 ";
  15. Po. Display ();
  16.  
  17. Po. ResumeMemento (pcaretaker. Memento );
  18. Po. Display ();
  19. Console. ReadLine ();
  20.  
  21. }
  22. }

To download the source code, click MementoPattern.rar.

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.