Add basic memento pattern

This commit is contained in:
Petrutiu Mihai
2016-06-23 20:43:37 +03:00
parent 0a130b0405
commit 37151a23c7
10 changed files with 210 additions and 28 deletions

View File

@@ -1,16 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MememntoPattern
{
// This project can output the Class library as a NuGet Package.
// To enable this option, right-click on the project and select the Properties menu item. In the Build tab select "Produce outputs on build".
public class Class1
{
public Class1()
{
}
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MememntoPattern.Employee
{
/// <summary>
/// Caretaker
/// </summary>
public class Caretaker
{
private Stack<EmployeeMemento> employeeHistory;
public Caretaker()
{
employeeHistory = new Stack<EmployeeMemento>();
}
public void Save(Employee emp)
{
employeeHistory.Push(emp.Save());
}
public void Revert(Employee emp)
{
emp.Revert(employeeHistory.Pop());
}
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MememntoPattern.Employee
{
/// <summary>
/// Originator
/// </summary>
public class Employee
{
public string Name { get; set; }
public string Address { get; set; }
public List<String> Phones { get; set; }
public EmployeeMemento Save()
{
return new EmployeeMemento(Name, Address);
}
public void Revert(EmployeeMemento memento)
{
Name = memento.Name;
Address = memento.Address;
}
}
}

View File

@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MememntoPattern.Employee
{
public class EmployeeExample
{
public void Run()
{
Caretaker caretaker = new Caretaker();
Employee e = new Employee();
e.Name = "Ghiuri";
e.Address = "Stairway to heaven";
Console.WriteLine("First saved address: {0}", e.Address);
caretaker.Save(e);
e.Address = "Highway to hell";
Console.WriteLine("Last saved address: {0}", e.Address);
caretaker.Save(e);
e.Address = "Home of the brave"; // No save, no home, no address
Console.WriteLine("Address before revert: {0}", e.Address);
caretaker.Revert(e);
Console.WriteLine("First reverted Address: {0}", e.Address);
caretaker.Revert(e);
Console.WriteLine("Second reverted Address: {0}", e.Address);
}
}
}

View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MememntoPattern.Employee
{
/// <summary>
/// Memento object
/// </summary>
public class EmployeeMemento
{
public string Name { get; private set; }
public string Address { get; private set; }
public EmployeeMemento(string name, string address)
{
Name = name;
Address = address;
}
}
}

View File

@@ -0,0 +1,60 @@
using MememntoPattern.Employee;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MememntoPattern
{
// This project can output the Class library as a NuGet Package.
// To enable this option, right-click on the project and select the Properties menu item. In the Build tab select "Produce outputs on build".
public class MementoPatternExamples
{
public MementoPatternExamples()
{
}
public void Run()
{
Console.WriteLine(GetWhenToUse());
Console.WriteLine(GetActors());
Console.WriteLine(GetAlternatives());
GoToNextStep();
EmployeeExample empExample = new EmployeeExample();
empExample.Run();
}
string GetWhenToUse()
{
return @"When to use:
When you need to be able to track the state of an object,or/and restore previous states as needed
When you cannot use simple operation undo/redo, by saving the commands (when there are side effects to the operations) - example translation";
}
string GetAlternatives()
{
return @"Alternatives:
Command undo - when operations maybe undone
Iterative Memento - Save the changes, instead of storing the entire state again like GIT";
}
string GetActors()
{
return @"Actors:
Originator: object that we want to save. It will create the actual memento.
Caretaker: keeps the mementos
Memento: (Magic cookie) internal state of the object";
}
private static void GoToNextStep()
{
Console.ReadKey();
Console.Clear();
}
}
}

View File

@@ -1,8 +1,9 @@
{
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.5.0-rc2-24027"
"NETStandard.Library": "1.5.0-rc2-24027",
"Newtonsoft.Json": "8.0.3"
},
"frameworks": {