Add another air traffic controller example for mediator
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -242,4 +242,6 @@ ModelManifest.xml
|
||||
.paket/paket.exe
|
||||
|
||||
# FAKE - F# Make
|
||||
.fake/
|
||||
.fake/
|
||||
/patterns-library
|
||||
/patterns-library.zip
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Plane> planesMonitored;
|
||||
public AirTrafficControlTower()
|
||||
{
|
||||
planesMonitored = new List<Plane>();
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace MediatorPattern.FlightAirTrafficControl
|
||||
{
|
||||
public interface IAirTrafficControlTower
|
||||
{
|
||||
void StartMonitor(Plane plane);
|
||||
void UpdateLocation(Plane plane);
|
||||
}
|
||||
}
|
||||
32
src/MediatorPattern/FlightAirTrafficControl/Plane.cs
Normal file
32
src/MediatorPattern/FlightAirTrafficControl/Plane.cs
Normal file
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
@@ -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()
|
||||
{
|
||||
@@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediatorPattern.AirTrafficControl
|
||||
namespace MediatorPattern.GroundAirTrafficControl
|
||||
{
|
||||
/// <summary>
|
||||
/// Mediator
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace MediatorPattern.AirTrafficControl
|
||||
namespace MediatorPattern.GroundAirTrafficControl
|
||||
{
|
||||
public class Lane
|
||||
{
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace MediatorPattern.AirTrafficControl
|
||||
namespace MediatorPattern.GroundAirTrafficControl
|
||||
{
|
||||
public class MaintainerTeam
|
||||
{
|
||||
@@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediatorPattern.AirTrafficControl
|
||||
namespace MediatorPattern.GroundAirTrafficControl
|
||||
{
|
||||
public class Optional<T>
|
||||
{
|
||||
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
|
||||
namespace MediatorPattern.AirTrafficControl
|
||||
namespace MediatorPattern.GroundAirTrafficControl
|
||||
{
|
||||
public class Plane
|
||||
{
|
||||
@@ -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()
|
||||
|
||||
16
src/MememntoPattern/Class1.cs
Normal file
16
src/MememntoPattern/Class1.cs
Normal file
@@ -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()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
21
src/MememntoPattern/MememntoPattern.xproj
Normal file
21
src/MememntoPattern/MememntoPattern.xproj
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>1de3fdd3-d025-49d8-bef4-d5f0688f89e8</ProjectGuid>
|
||||
<RootNamespace>MememntoPattern</RootNamespace>
|
||||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
|
||||
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
</Project>
|
||||
19
src/MememntoPattern/Properties/AssemblyInfo.cs
Normal file
19
src/MememntoPattern/Properties/AssemblyInfo.cs
Normal file
@@ -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")]
|
||||
13
src/MememntoPattern/project.json
Normal file
13
src/MememntoPattern/project.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"version": "1.0.0-*",
|
||||
|
||||
"dependencies": {
|
||||
"NETStandard.Library": "1.5.0-rc2-24027"
|
||||
},
|
||||
|
||||
"frameworks": {
|
||||
"netstandard1.5": {
|
||||
"imports": "dnxcore50"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user