Add basic memento pattern
This commit is contained in:
@@ -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()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
30
src/MememntoPattern/Employee/Caretaker.cs
Normal file
30
src/MememntoPattern/Employee/Caretaker.cs
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
30
src/MememntoPattern/Employee/Employee.cs
Normal file
30
src/MememntoPattern/Employee/Employee.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
41
src/MememntoPattern/Employee/EmployeeExample.cs
Normal file
41
src/MememntoPattern/Employee/EmployeeExample.cs
Normal 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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
22
src/MememntoPattern/Employee/EmployeeMemento.cs
Normal file
22
src/MememntoPattern/Employee/EmployeeMemento.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
60
src/MememntoPattern/MementoPatternExamples.cs
Normal file
60
src/MememntoPattern/MementoPatternExamples.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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": {
|
||||
|
||||
Reference in New Issue
Block a user