Skip to content

Commit a163e11

Browse files
committed
Added mediator pattern
1 parent 0e7b586 commit a163e11

10 files changed

Lines changed: 269 additions & 76 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace SoftwarePatterns.Core.Mediator
2+
{
3+
public class Airbus577 : Aircraft
4+
{
5+
public Airbus577(string ident, double initialAltitude, IAircraftController controller)
6+
: base(ident, initialAltitude, controller)
7+
{
8+
}
9+
10+
protected override string Make
11+
{
12+
get { return "Airbus577"; }
13+
}
14+
}
15+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
3+
namespace SoftwarePatterns.Core.Mediator
4+
{
5+
public abstract class Aircraft : IAircraft
6+
{
7+
private readonly string _ident;
8+
private double _altitude;
9+
10+
protected Aircraft(string ident, double initialAltitude, IAircraftController controller)
11+
{
12+
_ident = ident;
13+
_altitude = initialAltitude;
14+
Controller = controller;
15+
controller.RegisterAircraft(this);
16+
}
17+
18+
protected abstract string Make { get; }
19+
20+
public double Altitude
21+
{
22+
get { return _altitude; }
23+
set
24+
{
25+
_altitude = value;
26+
Controller.RecieveAircraftLocation(this);
27+
}
28+
}
29+
30+
public IAircraftController Controller { get; set; }
31+
32+
public string Identifer
33+
{
34+
get
35+
{
36+
return _ident;
37+
}
38+
}
39+
40+
public void WarnOfAirspaceIntrusion(IAircraft aircraftIntruding)
41+
{
42+
Intrusion = true;
43+
Console.WriteLine("Aircraft {0} just intruded into airspace of {1}", Identifer, aircraftIntruding.Identifer);
44+
}
45+
46+
public bool Intrusion { get; set; }
47+
48+
public void WarnOfIntrusionIntoAirspace(IAircraft intrudingBy)
49+
{
50+
Intrusion = true;
51+
Console.WriteLine("Aircraft " + Identifer + " just had airspace intruded on by " + intrudingBy.Identifer);
52+
}
53+
}
54+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace SoftwarePatterns.Core.Mediator
6+
{
7+
public class AircraftController : IAircraftController
8+
{
9+
private readonly List<IAircraft> _aircraft = new List<IAircraft>();
10+
11+
public void RegisterAircraft(IAircraft aircraft)
12+
{
13+
if (!_aircraft.Contains(aircraft))
14+
{
15+
_aircraft.Add(aircraft);
16+
}
17+
}
18+
19+
public void RecieveAircraftLocation(IAircraft reportingAircraft)
20+
{
21+
foreach (var aircraft in _aircraft.Where(a => a != reportingAircraft)
22+
.Where(aircraft => Math.Abs(aircraft.Altitude - reportingAircraft.Altitude) > 100))
23+
{
24+
reportingAircraft.WarnOfAirspaceIntrusion(aircraft);
25+
aircraft.WarnOfIntrusionIntoAirspace(reportingAircraft);
26+
}
27+
}
28+
}
29+
30+
public interface IAircraftController
31+
{
32+
void RegisterAircraft(IAircraft aircraft);
33+
void RecieveAircraftLocation(IAircraft reportingAircraft);
34+
}
35+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace SoftwarePatterns.Core.Mediator
2+
{
3+
public class Boeing747 : Aircraft
4+
{
5+
public Boeing747(string ident, double initialAltitude, IAircraftController controller)
6+
: base(ident, initialAltitude, controller)
7+
{
8+
9+
}
10+
11+
protected override string Make
12+
{
13+
get { return "Boeing747"; }
14+
}
15+
}
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace SoftwarePatterns.Core.Mediator
2+
{
3+
public interface IAircraft
4+
{
5+
bool Intrusion { get; set; }
6+
double Altitude { get; set; }
7+
IAircraftController Controller { get; set; }
8+
string Identifer { get; }
9+
void WarnOfAirspaceIntrusion(IAircraft aircraftIntruding);
10+
void WarnOfIntrusionIntoAirspace(IAircraft intrudingBy);
11+
}
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Define:
2+
The mediator pattern defines an object that encapsulates how a set of objects interact. This pattern is considered to be a behavioural pattern due to the way it can alter the program's running behaviour.
3+
4+
Usage:
5+
Components need to communicate with each other
6+
They communicate via the mediator
7+
The components all have knowledge of the mediator
8+
9+
Advantages:
10+
Hides coordination between components
11+
Component no longer need to know about each other
12+
Mediator turns many to many relationships into one to many/many to one.
13+
14+
Disadvantages:
15+
The mediator can become large and very complicated

‎src/SoftwarePatterns.Core/SoftwarePatterns.Core.csproj‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@
132132
<Compile Include="LazyLoad\Customer.cs" />
133133
<Compile Include="LazyLoad\LazyInitialisation\Order.cs" />
134134
<Compile Include="LazyLoad\Proxy\Order.cs" />
135+
<Compile Include="Mediator\Airbus577.cs" />
136+
<Compile Include="Mediator\Aircraft.cs" />
137+
<Compile Include="Mediator\AircraftController.cs" />
138+
<Compile Include="Mediator\Boeing747.cs" />
139+
<Compile Include="Mediator\IAircraft.cs" />
135140
<Compile Include="Properties\AssemblyInfo.cs" />
136141
</ItemGroup>
137142
<ItemGroup>
@@ -150,6 +155,7 @@
150155
<Content Include="Interpreter\ReadMe.txt" />
151156
<Content Include="Iterator\ReadMe.txt" />
152157
<Content Include="LazyLoad\ReadMe.txt" />
158+
<Content Include="Mediator\ReadMe.txt" />
153159
</ItemGroup>
154160
<ItemGroup />
155161
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using SoftwarePatterns.Core.Mediator;
2+
using Xunit;
3+
4+
namespace SoftwarePatterns.Tests.Mediator
5+
{
6+
public class AircraftControllerTests
7+
{
8+
[Fact]
9+
public void IntrusionStateWhenAtSameAltitude()
10+
{
11+
var controller = new AircraftController();
12+
13+
var aircraft = new Boeing747("1234", 10000, controller);
14+
var aircraft1 = new Airbus577("4567", 8000, controller);
15+
16+
aircraft.Altitude += 1000;
17+
18+
Assert.True(aircraft.Intrusion);
19+
Assert.True(aircraft1.Intrusion);
20+
}
21+
}
22+
}

‎src/SoftwarePatterns.Tests/SoftwarePatterns.Tests.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<Compile Include="Flyweight\TileFactoryTests.cs" />
8181
<Compile Include="Iterator\BinaryTreeTests.cs" />
8282
<Compile Include="LazyLoad\OrderTests.cs" />
83+
<Compile Include="Mediator\AircraftControllerTests.cs" />
8384
<Compile Include="Properties\AssemblyInfo.cs" />
8485
</ItemGroup>
8586
<ItemGroup>

0 commit comments

Comments
 (0)