From 37151a23c790248c209ac719415ef1e4123d6113 Mon Sep 17 00:00:00 2001 From: Petrutiu Mihai Date: Thu, 23 Jun 2016 20:43:37 +0300 Subject: [PATCH] Add basic memento pattern --- src/BehavioralPatterns/Program.cs | 4 ++ src/BehavioralPatterns/project.json | 1 + .../MediatorPatternExamples.cs | 29 +++++---- src/MememntoPattern/Class1.cs | 16 ----- src/MememntoPattern/Employee/Caretaker.cs | 30 ++++++++++ src/MememntoPattern/Employee/Employee.cs | 30 ++++++++++ .../Employee/EmployeeExample.cs | 41 +++++++++++++ .../Employee/EmployeeMemento.cs | 22 +++++++ src/MememntoPattern/MementoPatternExamples.cs | 60 +++++++++++++++++++ src/MememntoPattern/project.json | 5 +- 10 files changed, 210 insertions(+), 28 deletions(-) delete mode 100644 src/MememntoPattern/Class1.cs create mode 100644 src/MememntoPattern/Employee/Caretaker.cs create mode 100644 src/MememntoPattern/Employee/Employee.cs create mode 100644 src/MememntoPattern/Employee/EmployeeExample.cs create mode 100644 src/MememntoPattern/Employee/EmployeeMemento.cs create mode 100644 src/MememntoPattern/MementoPatternExamples.cs diff --git a/src/BehavioralPatterns/Program.cs b/src/BehavioralPatterns/Program.cs index 0745c66..4247641 100644 --- a/src/BehavioralPatterns/Program.cs +++ b/src/BehavioralPatterns/Program.cs @@ -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 diff --git a/src/BehavioralPatterns/project.json b/src/BehavioralPatterns/project.json index f0ea6b1..a9167d0 100644 --- a/src/BehavioralPatterns/project.json +++ b/src/BehavioralPatterns/project.json @@ -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" diff --git a/src/MediatorPattern/MediatorPatternExamples.cs b/src/MediatorPattern/MediatorPatternExamples.cs index bbe6d62..787cf80 100644 --- a/src/MediatorPattern/MediatorPatternExamples.cs +++ b/src/MediatorPattern/MediatorPatternExamples.cs @@ -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, don’t play God! "; } + static string GetDisadvances() + { + return @"Disatvantages: +Mediator can become ver complicated as more colleagues are handled"; + } + private static void GoToNextStep() { Console.ReadKey(); diff --git a/src/MememntoPattern/Class1.cs b/src/MememntoPattern/Class1.cs deleted file mode 100644 index 0240509..0000000 --- a/src/MememntoPattern/Class1.cs +++ /dev/null @@ -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() - { - } - } -} diff --git a/src/MememntoPattern/Employee/Caretaker.cs b/src/MememntoPattern/Employee/Caretaker.cs new file mode 100644 index 0000000..4be9586 --- /dev/null +++ b/src/MememntoPattern/Employee/Caretaker.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MememntoPattern.Employee +{ + /// + /// Caretaker + /// + public class Caretaker + { + private Stack employeeHistory; + + public Caretaker() + { + employeeHistory = new Stack(); + } + + public void Save(Employee emp) + { + employeeHistory.Push(emp.Save()); + } + + public void Revert(Employee emp) + { + emp.Revert(employeeHistory.Pop()); + } + } +} diff --git a/src/MememntoPattern/Employee/Employee.cs b/src/MememntoPattern/Employee/Employee.cs new file mode 100644 index 0000000..2759761 --- /dev/null +++ b/src/MememntoPattern/Employee/Employee.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MememntoPattern.Employee +{ + /// + /// Originator + /// + public class Employee + { + public string Name { get; set; } + + public string Address { get; set; } + + public List Phones { get; set; } + + public EmployeeMemento Save() + { + return new EmployeeMemento(Name, Address); + } + + public void Revert(EmployeeMemento memento) + { + Name = memento.Name; + Address = memento.Address; + } + } +} diff --git a/src/MememntoPattern/Employee/EmployeeExample.cs b/src/MememntoPattern/Employee/EmployeeExample.cs new file mode 100644 index 0000000..ecce6fc --- /dev/null +++ b/src/MememntoPattern/Employee/EmployeeExample.cs @@ -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); + + } + + } +} diff --git a/src/MememntoPattern/Employee/EmployeeMemento.cs b/src/MememntoPattern/Employee/EmployeeMemento.cs new file mode 100644 index 0000000..2a495ff --- /dev/null +++ b/src/MememntoPattern/Employee/EmployeeMemento.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MememntoPattern.Employee +{ + /// + /// Memento object + /// + 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; + } + } +} diff --git a/src/MememntoPattern/MementoPatternExamples.cs b/src/MememntoPattern/MementoPatternExamples.cs new file mode 100644 index 0000000..fb49ae5 --- /dev/null +++ b/src/MememntoPattern/MementoPatternExamples.cs @@ -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(); + } + } +} diff --git a/src/MememntoPattern/project.json b/src/MememntoPattern/project.json index ed8608d..2402f0c 100644 --- a/src/MememntoPattern/project.json +++ b/src/MememntoPattern/project.json @@ -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": {