What is the need for Memento Pattern in C#?

C# provides you an option to externalize internal state of an object for restoring it at a later point of time. This is made possible by implementing Memento Pattern. To implement this pattern, three classes are required. The mementoClass will store the object’s internal state.



The ownerClass will have privileges to restore the object’s state. The safeKeepingClass will ensure safety of the information in mementoClass. This class will not have rights to access the contents of mementoClass. Here is a sample code demonstrating it:

public class mementoClass {
private string objState;
//Include constructor with state as parameter and include property to get/set objState
}
public class safeKeepingClass {
private mementoClass memObj;
//Include methods or properties to get/set memObj
}
public class ownerClass {
private string objState;
//Include property to get/set objState
public mementoClass createInstance() {
return(new mementoClass(objState));
}
public void restoreState(mementoClass memObj) {
objState = memObj.State //State is the property of memObj returning objState
}
}

| What is the need for Bridge Pattern in C#? | What is the need for Builder Pattern in C#? | What is the need for Chain of Responsibility Pattern in C#? | What is the need for Command Pattern in C#? | What is the need for Composite Pattern in C#? | What is the need for Decorator Pattern in C#? | What is the need for Flyweight Pattern in C#? | What is the need for Interpreter Pattern in C#? | What is the need for Iterator Pattern in C#? | What is the need for Mediator Pattern in C#? | What is the need for Memento Pattern in C#? | What is the need for Prototype Pattern in C#? | What is the need for State Pattern in C#? | What is the need for Strategy Pattern in C#? | What is the need for Template Method Pattern in C#? | What is the need for unsafe code in C#? | What is the purpose of assert() in C#? | What is the purpose of AutoResetEvent in .NET? | What is the purpose of Console.ReadLine() in C#? | What is the purpose of machine.config file in .NET? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.