Created
May 14, 2012 09:12
-
-
Save dreikanter/2692884 to your computer and use it in GitHub Desktop.
Demo for two-class communication through the mediator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Diagnostics; | |
namespace MediatorDemo | |
{ | |
class Dispatcher | |
{ | |
private List<Command> commands; | |
private Queue<Command> queue; | |
private Mediator mediator; | |
public delegate void EnqueueCommandHandler(Command command); | |
private Command currentCommand; | |
public Dispatcher() | |
{ | |
this.commands = new List<Command>(); | |
this.queue = new Queue<Command>(); | |
this.mediator = new Mediator(new EnqueueCommandHandler(EnqueueCommand)); | |
} | |
public Command NewCommand(string name) | |
{ | |
Command command = new Command(name, this.mediator); | |
this.commands.Add(command); | |
mediator.UpdateStatus(command, "initializing"); | |
return command; | |
} | |
private void EnqueueCommand(Command command) | |
{ | |
queue.Enqueue(command); | |
mediator.UpdateStatus(command, "enqueued"); | |
} | |
public void ProcessNextCommand() | |
{ | |
if (currentCommand != null) | |
{ | |
mediator.UpdateStatus(currentCommand, "done"); | |
currentCommand = null; | |
} | |
if (queue.Count == 0) return; | |
currentCommand = queue.Dequeue(); | |
mediator.UpdateStatus(currentCommand, "processing"); | |
} | |
} | |
class Command | |
{ | |
public string Name { get; private set; } | |
public string Status { get; private set; } | |
private Mediator mediator; | |
public Command(string name, Mediator mediator) | |
{ | |
this.Name = name; | |
this.mediator = mediator; | |
mediator.RegisterStatusChangeHandler(this, new Mediator.StatusChangeHandler(UpdateStatus)); | |
} | |
private void UpdateStatus(string status) | |
{ | |
this.Status = status; | |
Trace.WriteLine("Status updated for " + Name + ": " + status); | |
} | |
public void Enqueue() | |
{ | |
mediator.Enqueue(this); | |
} | |
} | |
/// <summary> | |
/// В медиатор вынесена вся рутина про взаимодействие между классами | |
/// </summary> | |
class Mediator | |
{ | |
public delegate void StatusChangeHandler(string status); | |
private Dictionary<Command, StatusChangeHandler> statusChangeHandlers; | |
private Dispatcher.EnqueueCommandHandler enqueueCommandHandler; | |
public Mediator(Dispatcher.EnqueueCommandHandler enqueueCommandHandler) | |
{ | |
this.statusChangeHandlers = new Dictionary<Command, StatusChangeHandler>(); | |
this.enqueueCommandHandler = enqueueCommandHandler; | |
} | |
/// <summary> | |
/// Метод, через который команда может подписаться на обновление статуса | |
/// </summary> | |
public void RegisterStatusChangeHandler(Command command, StatusChangeHandler handler) | |
{ | |
this.statusChangeHandlers.Add(command, handler); | |
} | |
/// <summary> | |
/// Метод, которым Диспетчер может обновлять статусы команд | |
/// </summary> | |
public void UpdateStatus(Command command, string status) | |
{ | |
if (statusChangeHandlers.ContainsKey(command)) | |
{ | |
statusChangeHandlers[command](status); | |
} | |
} | |
public void Enqueue(Command command) | |
{ | |
if (this.enqueueCommandHandler != null) this.enqueueCommandHandler(command); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Dispatcher dispatcher = new Dispatcher(); | |
Command command = dispatcher.NewCommand("1"); | |
command.Enqueue(); | |
Command command1 = dispatcher.NewCommand("2"); | |
command1.Enqueue(); | |
dispatcher.ProcessNextCommand(); | |
dispatcher.ProcessNextCommand(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment