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

@@ -3,6 +3,7 @@ using ChainOfResponssibility.PurchaseExample;
using CommandPattern;
using IteratorPattern;
using MediatorPattern;
using MememntoPattern;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@@ -16,6 +17,9 @@ namespace BehavioralPatterns
public static void Main(string[] args)
{
MementoPatternExamples mementoPattern = new MementoPatternExamples();
mementoPattern.Run();
MediatorPatternExamples.Run();
//Chain of responsibillity
//This is usefull when you have a request and you don't know who should process it

View File

@@ -12,6 +12,7 @@
"CommandPattern": "1.0.0-*",
"IteratorPattern": "1.0.0-*",
"MediatorPattern": "1.0.0-*",
"MememntoPattern": "1.0.0-*",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc2-3002702"

View File

@@ -9,22 +9,25 @@ namespace MediatorPattern
{
public static void Run()
{
//Console.WriteLine(GetPatternDescription());
//Console.WriteLine(GetActors());
//Console.WriteLine(WhenToUseIt());
//GoToNextStep();
Console.WriteLine(GetPatternDescription());
Console.WriteLine(GetActors());
Console.WriteLine(WhenToUseIt());
Console.WriteLine(GetDisadvances());
GoToNextStep();
//StockExchangeExample stockExample = new StockExchangeExample();
//stockExample.Run();
StockExchangeExample stockExample = new StockExchangeExample();
stockExample.Run();
//GoToNextStep();
//GroundAirTrafficControlExample groundAirControl = new GroundAirTrafficControlExample();
//groundAirControl.Run();
GoToNextStep();
GroundAirTrafficControlExample groundAirControl = new GroundAirTrafficControlExample();
groundAirControl.Run();
//GoToNextStep();
GoToNextStep();
FlightAirTrafficControlExample flightAirControl = new FlightAirTrafficControlExample();
flightAirControl.Run();
}
static string GetPatternDescription()
@@ -54,6 +57,12 @@ But, dont play God!
";
}
static string GetDisadvances()
{
return @"Disatvantages:
Mediator can become ver complicated as more colleagues are handled";
}
private static void GoToNextStep()
{
Console.ReadKey();

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": {