0% found this document useful (0 votes)
274 views5 pages

Memento Pattern - CodeProject

Memento Pattern c#

Uploaded by

Igor Gjorgjiev
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
274 views5 pages

Memento Pattern - CodeProject

Memento Pattern c#

Uploaded by

Igor Gjorgjiev
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Memento Pattern - CodeProject

10,140,678 members (40,336 online)

2 Member 3810050

2.3K Sign out

home

articles

quick answers

discussions

features

community

help
Next

Articles Web Development Wiki.ASP.NET articles General

Article Browse Code Stats Revisions (5) Alternatives Comments & Discussions

Memento Pattern
By ASP.NET Community, 12 Oct 2013
0.00 (No votes)
Like 2 0

About Article
Rate: The memento design pattern is a pattern that helps to save the object internal state in an external place enabling us to restore the state later when Type Article CPOL 1 Oct 2008 517

Editorial Note
This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant. The memento design pattern is a pattern that helps to save the object internal state in an external place enabling us to restore the state later when needed. The memento pattern doesnt violate encapsulation of the internal state. The pattern is rarely used but its very helpful in scientific computing or in computer games (saving of check points in the game for example). Use Cases for the Memento Pattern You should use the pattern in the following cases: You need to save objects state and use the saved state later in order to restore the saved state. You dont want to expose the internal state of your object. UML Diagram

Licence First Posted Views

Add your own alternative version

Bookmarked 1 time pattern object computing internal , +

http://www.codeproject.com/Articles/667701/Memento-Pattern[12/10/2013 22:24:03]

Memento Pattern - CodeProject

Memento Pattern UML

Example in C# The following code is an example of how to implement the pattern:


Collapse | Copy Code

#region Originator public class Originator<T> { #region Properties public T State { get; set; } #endregion #region Methods /// <summary> /// Creates a new memento to hold the current /// state /// </summary> /// <returns>The created memento</returns> public Memento<T> SaveMemento() { return (new Memento<T>(State)); } /// /// /// /// <summary> Restores the state which is saved in the given memento </summary> <param name="memento" />The given memento

Top News
Does .NET have a future?
Get the Insider News free each morning.

public void RestoreMemento(Memento<T> memento) { State = memento.State; } http://www.codeproject.com/Articles/667701/Memento-Pattern[12/10/2013 22:24:03]

Related Videos

Memento Pattern - CodeProject #endregion } #endregion #region Memento public class Memento<T> { #region Properties public T State { get; private set; } #endregion #region Ctor /// <summary> /// Construct a new memento object with the /// given state /// </summary> /// <param name="state" />The given state public Memento(T state) { State = state; } #endregion } #endregion #region Caretaker public class Caretaker<T> { #region Properties public Memento<T> Memento { get; set; } #endregion } #endregion http://www.codeproject.com/Articles/667701/Memento-Pattern[12/10/2013 22:24:03]

Related Articles
Multilevel Undo and Redo Implementation in C# - Part III (Using Memento Pattern) Generic Memento Pattern for Undo-Redo in C# Mementos Introduction to CQRS An Undo/Redo Buffer Framework Design pattern FAQ Part 2 (Design pattern training series) Design Patterns 3 of 3 Behavioral Design Patterns Illustrated GOF Design Patterns in C# Part V: Behavioral II Memento Design Pattern Using AOP to Implement Functional Requirements Closing Dirty Forms Practical approach to Memento design pattern StateProto - Saving and

Memento Pattern - CodeProject

The given example shows the three parts of the pattern: the Originator, the Memento and the Caretaker. The Caretaker is the repository for the Memento. You can also see that once the Memento object is created you cant change the saved state and in order to save a new Memento you need to create it again. Lets look at an example of how to use the pattern in code:
Collapse | Copy Code

Restoring the State Machine Six common uses of the Template Design Pattern: Design Pattern series Design Patterns The best way to remember design patterns Design Patterns: Command Pattern Design pattern FAQ Part 1 (Training) Design pattern FAQ Part 3 ( Design pattern training series) Generic implementation of IEditableObject via TypeDescriptor and Reflection Multilevel Undo and Redo Implementation in C# - Part II (Using Command Pattern)

Originator<string> org = new Originator<string>(); org.State = "Old State"; // Store internal state in the caretaker object Caretaker<string> caretaker = new Caretaker<string>(); caretaker.Memento = org.SaveMemento(); Console.WriteLine("This is the old state: {0}", org.State); org.State = "New state"; Console.WriteLine("This is the new state: {0}", org.State); // Restore saved state from the caretaker org.RestoreMemento(caretaker.Memento); Console.WriteLine("Old state was restored: {0}", org.State); // Wait for user Console.Read();

As you can see the saved state is inserted to the Caretaker object and than you can change the state of the Originator to whatever you need. In order to restore the Originator state you use the restore method and pass it the Caretakers saved Memento. Summary To sum up the post, the memento pattern is is used for saving encapsulated object state in an external object. The state can then be restored by demand. As I wrote the places that youll want to use the pattern are in scientific computing or in computer games but it can be helpful also in other places.

License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

http://www.codeproject.com/Articles/667701/Memento-Pattern[12/10/2013 22:24:03]

Memento Pattern - CodeProject

ASP.NET Community
United States

The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost. The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community. Group type: Collaborative Group 1 members Apply to join this group Article Top

Comments and Discussions


Permalink | Advertise | Privacy | Mobile Web01 | 2.7.1310010.1 | Last Updated 12 Oct 2013 Layout: fixed | fluid Article Copyright 2008 by ASP.NET Community Everything else Copyright CodeProject, 1999-2013 Terms of Use

http://www.codeproject.com/Articles/667701/Memento-Pattern[12/10/2013 22:24:03]

You might also like