diff --git a/.gitignore b/.gitignore index 3a2238d..5e94794 100644 --- a/.gitignore +++ b/.gitignore @@ -242,4 +242,6 @@ ModelManifest.xml .paket/paket.exe # FAKE - F# Make -.fake/ \ No newline at end of file +.fake/ +/patterns-library +/patterns-library.zip diff --git a/BehavioralPatterns.sln b/BehavioralPatterns.sln index 79c9a20..8eef195 100644 --- a/BehavioralPatterns.sln +++ b/BehavioralPatterns.sln @@ -20,6 +20,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "IteratorPattern", "src\Iter EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MediatorPattern", "src\MediatorPattern\MediatorPattern.xproj", "{2A63BD0A-9D07-4755-9B16-5DDBEB075B80}" EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MememntoPattern", "src\MememntoPattern\MememntoPattern.xproj", "{1DE3FDD3-D025-49D8-BEF4-D5F0688F89E8}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -46,6 +48,10 @@ Global {2A63BD0A-9D07-4755-9B16-5DDBEB075B80}.Debug|Any CPU.Build.0 = Debug|Any CPU {2A63BD0A-9D07-4755-9B16-5DDBEB075B80}.Release|Any CPU.ActiveCfg = Release|Any CPU {2A63BD0A-9D07-4755-9B16-5DDBEB075B80}.Release|Any CPU.Build.0 = Release|Any CPU + {1DE3FDD3-D025-49D8-BEF4-D5F0688F89E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1DE3FDD3-D025-49D8-BEF4-D5F0688F89E8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1DE3FDD3-D025-49D8-BEF4-D5F0688F89E8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1DE3FDD3-D025-49D8-BEF4-D5F0688F89E8}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -56,5 +62,6 @@ Global {454B2A43-8251-4667-8DE3-67E489908DB9} = {3820200F-354C-41E6-8F34-B301F5D621C2} {6F3B7F9A-4D9C-4506-A5F7-3FF5CF4376BD} = {3820200F-354C-41E6-8F34-B301F5D621C2} {2A63BD0A-9D07-4755-9B16-5DDBEB075B80} = {3820200F-354C-41E6-8F34-B301F5D621C2} + {1DE3FDD3-D025-49D8-BEF4-D5F0688F89E8} = {3820200F-354C-41E6-8F34-B301F5D621C2} EndGlobalSection EndGlobal diff --git a/src/BehavioralPatterns/Program.cs b/src/BehavioralPatterns/Program.cs index 4f0888d..0745c66 100644 --- a/src/BehavioralPatterns/Program.cs +++ b/src/BehavioralPatterns/Program.cs @@ -16,6 +16,7 @@ namespace BehavioralPatterns public static void Main(string[] args) { + MediatorPatternExamples.Run(); //Chain of responsibillity //This is usefull when you have a request and you don't know who should process it ChainOfResponsibillityExamples.Run(); @@ -29,8 +30,8 @@ namespace BehavioralPatterns IteratorPatternExamples.Run(); Console.ReadKey(); - - MediatorPatternExamples.Run(); + IteratorPatternExamples.Run(); + Console.ReadKey(); } } diff --git a/src/MediatorPattern/FlightAirTrafficControl/AirTrafficControlTower.cs b/src/MediatorPattern/FlightAirTrafficControl/AirTrafficControlTower.cs new file mode 100644 index 0000000..cf9886c --- /dev/null +++ b/src/MediatorPattern/FlightAirTrafficControl/AirTrafficControlTower.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MediatorPattern.FlightAirTrafficControl +{ + public class AirTrafficControlTower : IAirTrafficControlTower + { + int MINIMUM_DIFFERENCE_BETWEEN_PLANES = 2000; + List planesMonitored; + public AirTrafficControlTower() + { + planesMonitored = new List(); + } + public void StartMonitor(Plane plane) + { + if(!planesMonitored.Contains(plane)) + { + planesMonitored.Add(plane); + } + + UpdateLocation(plane); + } + + public void PrintPlanesAltitude() + { + Console.WriteLine("Planes monitored:"); + + foreach (var plane in planesMonitored) + { + Console.WriteLine("Plane {0} at altitude {1}", plane.ID, plane.Altitude); + } + } + + public void UpdateLocation(Plane plane) + { + var otherPlanesMonitored = planesMonitored.Where(p => p.ID != plane.ID); + + var planesNearby = otherPlanesMonitored.Where(p => GetDifferenceInAltitude(p, plane) < MINIMUM_DIFFERENCE_BETWEEN_PLANES).OrderBy(p => p.Altitude); + + if(!planesNearby.Any()) + { + Console.WriteLine("Thanks for update {0}, no planes nearby, please continue", plane.ID); + } + else + { + var planeAbove = planesNearby.FirstOrDefault(p => p.Altitude > plane.Altitude); + + if (planeAbove != null) + { + planeAbove.ChangeAltitude(MINIMUM_DIFFERENCE_BETWEEN_PLANES); + } + + var planeBellow = planesNearby.FirstOrDefault(p => p.Altitude <= plane.Altitude); + + if(planeBellow != null) + { + plane.ChangeAltitude(MINIMUM_DIFFERENCE_BETWEEN_PLANES); + } + } + + } + + private int GetDifferenceInAltitude(Plane p1, Plane p2) + { + return Math.Abs(p1.Altitude - p2.Altitude); + } + } +} diff --git a/src/MediatorPattern/FlightAirTrafficControl/FlightAirTrafficControlExample.cs b/src/MediatorPattern/FlightAirTrafficControl/FlightAirTrafficControlExample.cs new file mode 100644 index 0000000..e2b5ae2 --- /dev/null +++ b/src/MediatorPattern/FlightAirTrafficControl/FlightAirTrafficControlExample.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MediatorPattern.FlightAirTrafficControl +{ + public class FlightAirTrafficControlExample + { + public void Run() + { + var controlTower = new AirTrafficControlTower(); + + var fligh1 = new Plane("p1", 1000, controlTower); + var fligh2 = new Plane("p2", 2000, controlTower); + var fligh3 = new Plane("p3", 2000, controlTower); + var fligh4 = new Plane("p4", 500, controlTower); + + controlTower.PrintPlanesAltitude(); + + } + } +} diff --git a/src/MediatorPattern/FlightAirTrafficControl/IAirTrafficControlTower.cs b/src/MediatorPattern/FlightAirTrafficControl/IAirTrafficControlTower.cs new file mode 100644 index 0000000..fb2b97c --- /dev/null +++ b/src/MediatorPattern/FlightAirTrafficControl/IAirTrafficControlTower.cs @@ -0,0 +1,8 @@ +namespace MediatorPattern.FlightAirTrafficControl +{ + public interface IAirTrafficControlTower + { + void StartMonitor(Plane plane); + void UpdateLocation(Plane plane); + } +} \ No newline at end of file diff --git a/src/MediatorPattern/FlightAirTrafficControl/Plane.cs b/src/MediatorPattern/FlightAirTrafficControl/Plane.cs new file mode 100644 index 0000000..2fa4fa6 --- /dev/null +++ b/src/MediatorPattern/FlightAirTrafficControl/Plane.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MediatorPattern.FlightAirTrafficControl +{ + public class Plane + { + public string ID { get; private set; } + + IAirTrafficControlTower controlTower; + public Plane(string id, int altitude, IAirTrafficControlTower controlTower) + { + this.controlTower = controlTower; + ID = id; + Altitude = altitude; + controlTower.StartMonitor(this); + } + + public int Altitude { get; private set; } + + public void ChangeAltitude(int altitudeDifference) + { + int newExpectedAltitude = Altitude + altitudeDifference; + Console.WriteLine("Plane {0} is changing altitude from {1} to {2}", ID, Altitude, newExpectedAltitude); + Altitude = newExpectedAltitude; + controlTower.UpdateLocation(this); + } + + } +} diff --git a/src/MediatorPattern/AirTrafficControl/AirTrafficControl.cs b/src/MediatorPattern/GroundAirTrafficControl/AirTrafficControl.cs similarity index 93% rename from src/MediatorPattern/AirTrafficControl/AirTrafficControl.cs rename to src/MediatorPattern/GroundAirTrafficControl/AirTrafficControl.cs index 6247386..2923c16 100644 --- a/src/MediatorPattern/AirTrafficControl/AirTrafficControl.cs +++ b/src/MediatorPattern/GroundAirTrafficControl/AirTrafficControl.cs @@ -1,10 +1,8 @@ -using MediatorPattern.AirTrafficControl; -using System; +using System; using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; -namespace MediatorPattern.AirTrafficControl +namespace MediatorPattern.GroundAirTrafficControl { public class AirTrafficControl : IAirTrafficControlTower { diff --git a/src/MediatorPattern/AirTrafficControl/AirTrafficControlExample.cs b/src/MediatorPattern/GroundAirTrafficControl/GroundAirTrafficControlExample.cs similarity index 90% rename from src/MediatorPattern/AirTrafficControl/AirTrafficControlExample.cs rename to src/MediatorPattern/GroundAirTrafficControl/GroundAirTrafficControlExample.cs index ecc2510..9e901db 100644 --- a/src/MediatorPattern/AirTrafficControl/AirTrafficControlExample.cs +++ b/src/MediatorPattern/GroundAirTrafficControl/GroundAirTrafficControlExample.cs @@ -3,9 +3,9 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -namespace MediatorPattern.AirTrafficControl +namespace MediatorPattern.GroundAirTrafficControl { - public class AirTrafficControlExample + public class GroundAirTrafficControlExample { public void Run() { diff --git a/src/MediatorPattern/AirTrafficControl/IAirTrafficControlTower.cs b/src/MediatorPattern/GroundAirTrafficControl/IAirTrafficControlTower.cs similarity index 87% rename from src/MediatorPattern/AirTrafficControl/IAirTrafficControlTower.cs rename to src/MediatorPattern/GroundAirTrafficControl/IAirTrafficControlTower.cs index 79c80f8..3a72016 100644 --- a/src/MediatorPattern/AirTrafficControl/IAirTrafficControlTower.cs +++ b/src/MediatorPattern/GroundAirTrafficControl/IAirTrafficControlTower.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -namespace MediatorPattern.AirTrafficControl +namespace MediatorPattern.GroundAirTrafficControl { /// /// Mediator diff --git a/src/MediatorPattern/AirTrafficControl/Lane.cs b/src/MediatorPattern/GroundAirTrafficControl/Lane.cs similarity index 83% rename from src/MediatorPattern/AirTrafficControl/Lane.cs rename to src/MediatorPattern/GroundAirTrafficControl/Lane.cs index 00253ac..850b0c3 100644 --- a/src/MediatorPattern/AirTrafficControl/Lane.cs +++ b/src/MediatorPattern/GroundAirTrafficControl/Lane.cs @@ -1,4 +1,4 @@ -namespace MediatorPattern.AirTrafficControl +namespace MediatorPattern.GroundAirTrafficControl { public class Lane { diff --git a/src/MediatorPattern/AirTrafficControl/MaintainerTeam.cs b/src/MediatorPattern/GroundAirTrafficControl/MaintainerTeam.cs similarity index 87% rename from src/MediatorPattern/AirTrafficControl/MaintainerTeam.cs rename to src/MediatorPattern/GroundAirTrafficControl/MaintainerTeam.cs index 259438d..a511587 100644 --- a/src/MediatorPattern/AirTrafficControl/MaintainerTeam.cs +++ b/src/MediatorPattern/GroundAirTrafficControl/MaintainerTeam.cs @@ -1,4 +1,4 @@ -namespace MediatorPattern.AirTrafficControl +namespace MediatorPattern.GroundAirTrafficControl { public class MaintainerTeam { diff --git a/src/MediatorPattern/AirTrafficControl/Optional.cs b/src/MediatorPattern/GroundAirTrafficControl/Optional.cs similarity index 94% rename from src/MediatorPattern/AirTrafficControl/Optional.cs rename to src/MediatorPattern/GroundAirTrafficControl/Optional.cs index 5ef700a..dd54422 100644 --- a/src/MediatorPattern/AirTrafficControl/Optional.cs +++ b/src/MediatorPattern/GroundAirTrafficControl/Optional.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -namespace MediatorPattern.AirTrafficControl +namespace MediatorPattern.GroundAirTrafficControl { public class Optional { diff --git a/src/MediatorPattern/AirTrafficControl/Plane.cs b/src/MediatorPattern/GroundAirTrafficControl/Plane.cs similarity index 92% rename from src/MediatorPattern/AirTrafficControl/Plane.cs rename to src/MediatorPattern/GroundAirTrafficControl/Plane.cs index 839a06a..45f530e 100644 --- a/src/MediatorPattern/AirTrafficControl/Plane.cs +++ b/src/MediatorPattern/GroundAirTrafficControl/Plane.cs @@ -1,6 +1,6 @@ using System; -namespace MediatorPattern.AirTrafficControl +namespace MediatorPattern.GroundAirTrafficControl { public class Plane { diff --git a/src/MediatorPattern/MediatorPatternExamples.cs b/src/MediatorPattern/MediatorPatternExamples.cs index 84a11f5..bbe6d62 100644 --- a/src/MediatorPattern/MediatorPatternExamples.cs +++ b/src/MediatorPattern/MediatorPatternExamples.cs @@ -1,27 +1,30 @@ -using MediatorPattern.AirTrafficControl; +using MediatorPattern.FlightAirTrafficControl; +using MediatorPattern.GroundAirTrafficControl; using MediatorPattern.StockExchange; using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace MediatorPattern -{ +{ public class MediatorPatternExamples { public static void Run() { - Console.WriteLine(GetPatternDescription()); - Console.WriteLine(GetActors()); - Console.WriteLine(WhenToUseIt()); - GoToNextStep(); + //Console.WriteLine(GetPatternDescription()); + //Console.WriteLine(GetActors()); + //Console.WriteLine(WhenToUseIt()); + //GoToNextStep(); - StockExchangeExample stockExample = new StockExchangeExample(); - stockExample.Run(); + //StockExchangeExample stockExample = new StockExchangeExample(); + //stockExample.Run(); - GoToNextStep(); - AirTrafficControlExample airTraficExample = new AirTrafficControlExample(); - airTraficExample.Run(); + //GoToNextStep(); + //GroundAirTrafficControlExample groundAirControl = new GroundAirTrafficControlExample(); + //groundAirControl.Run(); + + //GoToNextStep(); + + FlightAirTrafficControlExample flightAirControl = new FlightAirTrafficControlExample(); + flightAirControl.Run(); } static string GetPatternDescription() diff --git a/src/MememntoPattern/Class1.cs b/src/MememntoPattern/Class1.cs new file mode 100644 index 0000000..0240509 --- /dev/null +++ b/src/MememntoPattern/Class1.cs @@ -0,0 +1,16 @@ +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/MememntoPattern.xproj b/src/MememntoPattern/MememntoPattern.xproj new file mode 100644 index 0000000..c229291 --- /dev/null +++ b/src/MememntoPattern/MememntoPattern.xproj @@ -0,0 +1,21 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + 1de3fdd3-d025-49d8-bef4-d5f0688f89e8 + MememntoPattern + .\obj + .\bin\ + v4.6.1 + + + + 2.0 + + + diff --git a/src/MememntoPattern/Properties/AssemblyInfo.cs b/src/MememntoPattern/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..abb1917 --- /dev/null +++ b/src/MememntoPattern/Properties/AssemblyInfo.cs @@ -0,0 +1,19 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Hewlett-Packard Company")] +[assembly: AssemblyProduct("MememntoPattern")] +[assembly: AssemblyTrademark("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("1de3fdd3-d025-49d8-bef4-d5f0688f89e8")] diff --git a/src/MememntoPattern/project.json b/src/MememntoPattern/project.json new file mode 100644 index 0000000..ed8608d --- /dev/null +++ b/src/MememntoPattern/project.json @@ -0,0 +1,13 @@ +{ + "version": "1.0.0-*", + + "dependencies": { + "NETStandard.Library": "1.5.0-rc2-24027" + }, + + "frameworks": { + "netstandard1.5": { + "imports": "dnxcore50" + } + } +}