Add another air traffic controller example for mediator

This commit is contained in:
Petrutiu Mihai
2016-06-23 19:45:10 +03:00
parent 06d201c917
commit 0a130b0405
19 changed files with 241 additions and 28 deletions

View File

@@ -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);
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -0,0 +1,8 @@
namespace MediatorPattern.FlightAirTrafficControl
{
public interface IAirTrafficControlTower
{
void StartMonitor(Plane plane);
void UpdateLocation(Plane plane);
}
}

View 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);
}
}
}

View File

@@ -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
{

View File

@@ -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()
{

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MediatorPattern.AirTrafficControl
namespace MediatorPattern.GroundAirTrafficControl
{
/// <summary>
/// Mediator

View File

@@ -1,4 +1,4 @@
namespace MediatorPattern.AirTrafficControl
namespace MediatorPattern.GroundAirTrafficControl
{
public class Lane
{

View File

@@ -1,4 +1,4 @@
namespace MediatorPattern.AirTrafficControl
namespace MediatorPattern.GroundAirTrafficControl
{
public class MaintainerTeam
{

View File

@@ -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>
{

View File

@@ -1,6 +1,6 @@
using System;
namespace MediatorPattern.AirTrafficControl
namespace MediatorPattern.GroundAirTrafficControl
{
public class Plane
{

View File

@@ -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()