Advertisement
Dennisaa

Mediator 1

Sep 27th, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | None | 0 0
  1. using System.Collections.Generic;
  2.  
  3. namespace CA01 {
  4.     public interface IMediator<T> {
  5.         List<ICollaborator<T>> CollaboratorList { get; }
  6.         void DistributeMessage(ICollaborator<T> sender, string message);
  7.         void Register(ICollaborator<T> collaborator);
  8.  
  9.     }
  10. }
  11.  
  12. namespace CA01 {
  13.     public interface ICollaborator<T> {
  14.         void SendMessage(IMediator<T> mediator, string message);
  15.  
  16.         void ReceiveMessage(string message);
  17.     }
  18. }
  19.  
  20. using System;
  21.  
  22. namespace CA01 {
  23.     public sealed class NewsStation<T> : ICollaborator<T> {
  24.  
  25.         public string Name { get; private set; }
  26.  
  27.         public NewsStation(string name) {
  28.             Name = name;
  29.         }
  30.         void ICollaborator<T>.SendMessage(IMediator<T> mediator, string message) {
  31.             mediator.DistributeMessage(this, message);
  32.         }
  33.         void ICollaborator<T>.ReceiveMessage(string message) {
  34.             Console.WriteLine("\n" + Name + " received \n" + message.ToString());
  35.         }
  36.     }
  37. }
  38. using System.Collections.Generic;
  39.  
  40. namespace CA01 {
  41.  
  42.     /// <summary>
  43.     /// Singleton class to implement a (concrete) mediator. The _instance and the private constructor are used
  44.     /// for this. All other objects and methods cover the functional side
  45.     /// </summary>
  46.     /// <typeparam name="T"></typeparam>
  47.     public sealed class ConcreteMediator<T> : IMediator<T> {
  48.  
  49.         // Singleton techniques...
  50.         private static readonly ConcreteMediator<T> _instance = new ConcreteMediator<T>();
  51.  
  52.         private ConcreteMediator() { }
  53.  
  54.         public static ConcreteMediator<T> GetInstance() {
  55.             return _instance;
  56.         }
  57.  
  58.         private List<ICollaborator<T>> colleagueList = new List<ICollaborator<T>>();
  59.  
  60.         List<ICollaborator<T>> IMediator<T>.CollaboratorList {
  61.             get { return colleagueList; }
  62.         }
  63.  
  64.         void IMediator<T>.Register(ICollaborator<T> colleague) {
  65.             colleagueList.Add(colleague);
  66.         }
  67.  
  68.         void IMediator<T>.DistributeMessage(ICollaborator<T> sender, string message) {
  69.             foreach (var c in colleagueList) {
  70.                 if (c != sender) {  
  71.                     c.ReceiveMessage(message);
  72.                 }
  73.             }
  74.         }
  75.     }
  76.  
  77. }
  78.  
  79.  
  80. using System;
  81.  
  82. namespace CA01 {
  83.     public class Program {
  84.  
  85.         public static void Main() {
  86.             var p = new Program();
  87.             p.RunTheTest();
  88.         }
  89.         public void RunTheTest() {
  90.  
  91.             ICollaborator<string> BBCRadioLeicester = new NewsStation<string>("BBCRadioLeicester");
  92.             ICollaborator<string> CapitalRadio = new NewsStation<string>("CapitalRadio");
  93.             ICollaborator<string> LBC = new NewsStation<string>("LBC");
  94.  
  95.             IMediator<string> mediator = ConcreteMediator<string>.GetInstance();
  96.             mediator.Register(BBCRadioLeicester);
  97.             mediator.Register(CapitalRadio);
  98.             mediator.Register(LBC);
  99.  
  100.             BBCRadioLeicester.SendMessage(mediator, "[A cat has been found] broadcast from [BBCRadioLeicester]");
  101.             CapitalRadio.SendMessage(mediator, "[Price of Stuff has increased] broadcast from [CapitalRadio]");
  102.  
  103.             Console.WriteLine("That's all for now - press return twice");
  104.             Console.Read();
  105.         }
  106.     }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement