Skip to content

Instantly share code, notes, and snippets.

@asarnaout
Last active May 4, 2018 04:27
Show Gist options
  • Save asarnaout/5ca1fc0753fe03aedea49d5eed06cb03 to your computer and use it in GitHub Desktop.
Save asarnaout/5ca1fc0753fe03aedea49d5eed06cb03 to your computer and use it in GitHub Desktop.
Mediator Design Pattern
namespace Mediator
{
class Program
{
/*
* With the mediator pattern, communication between objects is encapsulated within a mediator object. Objects no longer communicate
* directly with each other, but instead communicate through the mediator. This reduces the dependencies between communicating objects,
* thereby reducing coupling.
*/
static void Main(string[] args)
{
var serverMonitor = new ServerMonitor(new Mediator());
serverMonitor.BeginMonitoring();
}
}
}
namespace Mediator
{
public class ServerMonitor
{
private readonly IMediator _mediator;
public ServerMonitor(IMediator mediator)
{
_mediator = mediator;
}
public void BeginMonitoring()
{
Console.WriteLine("Monitoring the server now");
Thread.Sleep(1000);
Console.WriteLine("CPU Usage Threshold exceeded!");
/*
* Using the mediator pattern, the 'ServerMonitory' type has been decoupled from any handlers, and thus could send and
* retrieve data without having any dependencies except on the mediator.
*
* For a more sophisticated implementation of the Mediator pattern, check the MediatR repository
*/
_mediator.Send(new CpuThresholdExceeded
{
CpuLevel = 99,
DetectedOn = DateTime.UtcNow
});
Console.WriteLine("CPU Spike Resuolved");
Thread.Sleep(1000);
Console.WriteLine("Memory Leak detected!");
var memoryFreed = _mediator.Send(new MemoryThresholdExceeded
{
AvailableMemory = 3,
DetectedOn = DateTime.UtcNow
});
Console.WriteLine($"{memoryFreed} GB Freed");
}
}
}
namespace Mediator
{
public interface IMediator
{
void Send(CpuThresholdExceeded message);
double Send(MemoryThresholdExceeded message);
}
}
namespace Mediator
{
/*
* The mediator should merely act as a router which manages communication and shouldn't perform any logic
*/
public class Mediator : IMediator
{
readonly CpuSpikeHandler _cpuSpikeHandler;
readonly MemoryLeakHandler _memoryLeakHandler;
public Mediator()
{
_cpuSpikeHandler = new CpuSpikeHandler();
_memoryLeakHandler = new MemoryLeakHandler();
}
public void Send(CpuThresholdExceeded message)
{
_cpuSpikeHandler.Handle(message);
}
public double Send(MemoryThresholdExceeded message)
{
return _memoryLeakHandler.Handle(message);
}
}
}
namespace Mediator.Messages
{
public class CpuThresholdExceeded
{
public int CpuLevel { get; set; }
public DateTime DetectedOn { get; set; }
}
}
namespace Mediator.Messages
{
public class MemoryThresholdExceeded
{
public int AvailableMemory { get; set; }
public DateTime DetectedOn { get; set; }
}
}
namespace Mediator.Handlers
{
public class CpuSpikeHandler
{
public void Handle(CpuThresholdExceeded arg)
{
Console.WriteLine("CPU Spike Handler Starting");
Thread.Sleep(1000);
Console.WriteLine("CPU Spike Handler Completed");
}
}
}
namespace Mediator.Handlers
{
public class MemoryLeakHandler
{
public double Handle(MemoryThresholdExceeded arg)
{
Console.WriteLine("Memory Leak Handler Starting");
Thread.Sleep(1000);
Console.WriteLine("Memory Leak Handler Completed");
return 12.8;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment