Add basic memento pattern
This commit is contained in:
@@ -3,6 +3,7 @@ using ChainOfResponssibility.PurchaseExample;
|
|||||||
using CommandPattern;
|
using CommandPattern;
|
||||||
using IteratorPattern;
|
using IteratorPattern;
|
||||||
using MediatorPattern;
|
using MediatorPattern;
|
||||||
|
using MememntoPattern;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
@@ -16,6 +17,9 @@ namespace BehavioralPatterns
|
|||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
MementoPatternExamples mementoPattern = new MementoPatternExamples();
|
||||||
|
mementoPattern.Run();
|
||||||
|
|
||||||
MediatorPatternExamples.Run();
|
MediatorPatternExamples.Run();
|
||||||
//Chain of responsibillity
|
//Chain of responsibillity
|
||||||
//This is usefull when you have a request and you don't know who should process it
|
//This is usefull when you have a request and you don't know who should process it
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
"CommandPattern": "1.0.0-*",
|
"CommandPattern": "1.0.0-*",
|
||||||
"IteratorPattern": "1.0.0-*",
|
"IteratorPattern": "1.0.0-*",
|
||||||
"MediatorPattern": "1.0.0-*",
|
"MediatorPattern": "1.0.0-*",
|
||||||
|
"MememntoPattern": "1.0.0-*",
|
||||||
"Microsoft.NETCore.App": {
|
"Microsoft.NETCore.App": {
|
||||||
"type": "platform",
|
"type": "platform",
|
||||||
"version": "1.0.0-rc2-3002702"
|
"version": "1.0.0-rc2-3002702"
|
||||||
|
|||||||
@@ -9,22 +9,25 @@ namespace MediatorPattern
|
|||||||
{
|
{
|
||||||
public static void Run()
|
public static void Run()
|
||||||
{
|
{
|
||||||
//Console.WriteLine(GetPatternDescription());
|
Console.WriteLine(GetPatternDescription());
|
||||||
//Console.WriteLine(GetActors());
|
Console.WriteLine(GetActors());
|
||||||
//Console.WriteLine(WhenToUseIt());
|
Console.WriteLine(WhenToUseIt());
|
||||||
//GoToNextStep();
|
Console.WriteLine(GetDisadvances());
|
||||||
|
GoToNextStep();
|
||||||
|
|
||||||
//StockExchangeExample stockExample = new StockExchangeExample();
|
StockExchangeExample stockExample = new StockExchangeExample();
|
||||||
//stockExample.Run();
|
stockExample.Run();
|
||||||
|
|
||||||
//GoToNextStep();
|
GoToNextStep();
|
||||||
//GroundAirTrafficControlExample groundAirControl = new GroundAirTrafficControlExample();
|
GroundAirTrafficControlExample groundAirControl = new GroundAirTrafficControlExample();
|
||||||
//groundAirControl.Run();
|
groundAirControl.Run();
|
||||||
|
|
||||||
//GoToNextStep();
|
GoToNextStep();
|
||||||
|
|
||||||
FlightAirTrafficControlExample flightAirControl = new FlightAirTrafficControlExample();
|
FlightAirTrafficControlExample flightAirControl = new FlightAirTrafficControlExample();
|
||||||
flightAirControl.Run();
|
flightAirControl.Run();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static string GetPatternDescription()
|
static string GetPatternDescription()
|
||||||
@@ -54,6 +57,12 @@ But, don’t play God!
|
|||||||
";
|
";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static string GetDisadvances()
|
||||||
|
{
|
||||||
|
return @"Disatvantages:
|
||||||
|
Mediator can become ver complicated as more colleagues are handled";
|
||||||
|
}
|
||||||
|
|
||||||
private static void GoToNextStep()
|
private static void GoToNextStep()
|
||||||
{
|
{
|
||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
|
|||||||
@@ -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-*",
|
"version": "1.0.0-*",
|
||||||
|
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"NETStandard.Library": "1.5.0-rc2-24027"
|
"NETStandard.Library": "1.5.0-rc2-24027",
|
||||||
|
"Newtonsoft.Json": "8.0.3"
|
||||||
},
|
},
|
||||||
|
|
||||||
"frameworks": {
|
"frameworks": {
|
||||||
|
|||||||
Reference in New Issue
Block a user