Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- namespace CA01 {
- public interface IMediator<T> {
- List<ICollaborator<T>> CollaboratorList { get; }
- void DistributeMessage(ICollaborator<T> sender, string message);
- void Register(ICollaborator<T> collaborator);
- }
- }
- namespace CA01 {
- public interface ICollaborator<T> {
- void SendMessage(IMediator<T> mediator, string message);
- void ReceiveMessage(string message);
- }
- }
- using System;
- namespace CA01 {
- public sealed class NewsStation<T> : ICollaborator<T> {
- public string Name { get; private set; }
- public NewsStation(string name) {
- Name = name;
- }
- void ICollaborator<T>.SendMessage(IMediator<T> mediator, string message) {
- mediator.DistributeMessage(this, message);
- }
- void ICollaborator<T>.ReceiveMessage(string message) {
- Console.WriteLine("\n" + Name + " received \n" + message.ToString());
- }
- }
- }
- using System.Collections.Generic;
- namespace CA01 {
- /// <summary>
- /// Singleton class to implement a (concrete) mediator. The _instance and the private constructor are used
- /// for this. All other objects and methods cover the functional side
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public sealed class ConcreteMediator<T> : IMediator<T> {
- // Singleton techniques...
- private static readonly ConcreteMediator<T> _instance = new ConcreteMediator<T>();
- private ConcreteMediator() { }
- public static ConcreteMediator<T> GetInstance() {
- return _instance;
- }
- private List<ICollaborator<T>> colleagueList = new List<ICollaborator<T>>();
- List<ICollaborator<T>> IMediator<T>.CollaboratorList {
- get { return colleagueList; }
- }
- void IMediator<T>.Register(ICollaborator<T> colleague) {
- colleagueList.Add(colleague);
- }
- void IMediator<T>.DistributeMessage(ICollaborator<T> sender, string message) {
- foreach (var c in colleagueList) {
- if (c != sender) {
- c.ReceiveMessage(message);
- }
- }
- }
- }
- }
- using System;
- namespace CA01 {
- public class Program {
- public static void Main() {
- var p = new Program();
- p.RunTheTest();
- }
- public void RunTheTest() {
- ICollaborator<string> BBCRadioLeicester = new NewsStation<string>("BBCRadioLeicester");
- ICollaborator<string> CapitalRadio = new NewsStation<string>("CapitalRadio");
- ICollaborator<string> LBC = new NewsStation<string>("LBC");
- IMediator<string> mediator = ConcreteMediator<string>.GetInstance();
- mediator.Register(BBCRadioLeicester);
- mediator.Register(CapitalRadio);
- mediator.Register(LBC);
- BBCRadioLeicester.SendMessage(mediator, "[A cat has been found] broadcast from [BBCRadioLeicester]");
- CapitalRadio.SendMessage(mediator, "[Price of Stuff has increased] broadcast from [CapitalRadio]");
- Console.WriteLine("That's all for now - press return twice");
- Console.Read();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement