|
| 1 | +# Mediator |
| 2 | + |
| 3 | +Use the Mediator Pattern to centralize complex communications and control between related objects. |
| 4 | + |
| 5 | +## Problem |
| 6 | + |
| 7 | +* Tight coupling between a set of interacting objects should be avoided. |
| 8 | +* It should be possible to change the interaction between a set of objects independently. |
| 9 | + |
| 10 | +*Tightly coupled objects* are hard to implement, change, test and reuse. |
| 11 | + |
| 12 | +## Solution |
| 13 | + |
| 14 | +* Define a `Mediator` object that encapsulates the interaction between a set of objects. |
| 15 | +* Objects delegate their interaction to a mediator object instead of interacting with each other directly. |
| 16 | + |
| 17 | +This makes the objects *loosely coupled*. |
| 18 | + |
| 19 | +The Mediator is commonly used to coordinate related GUI components. |
| 20 | + |
| 21 | +## Benefits |
| 22 | + |
| 23 | +* Colleagues classes may become more reusable and are decoupled from the system. |
| 24 | +* Simplifies maintenance of the system by centralizing control logic. |
| 25 | +* Simplifies and reduces the variety of messages sent between objects in the system. |
| 26 | + |
| 27 | +## Drawbacks |
| 28 | + |
| 29 | +* Without proper design, the mediator object can become overly complex. |
| 30 | +* Single point of failure |
| 31 | +* Hard to test --> Objects should be mocked |
| 32 | + |
| 33 | +## Example |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +Mediator |
| 38 | +```cs |
| 39 | + public interface IChatRoom |
| 40 | +{ |
| 41 | + void RegisterParticipant(IParticipant participant); |
| 42 | + void Send(String from, String to, string message); |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +ConcreteMediator |
| 47 | +```cs |
| 48 | +public class ChatRoom : IChatRoom |
| 49 | +{ |
| 50 | + private readonly IDictionary<string, IParticipant> _participants = new Dictionary<string, IParticipant>(); |
| 51 | + |
| 52 | + public void RegisterParticipant(IParticipant participant) |
| 53 | + { |
| 54 | + this._participants.Add(participant.GetName(), participant); |
| 55 | + } |
| 56 | + |
| 57 | + public void Send(string from, string to, string message) |
| 58 | + { |
| 59 | + if (this._participants.ContainsKey(to)) |
| 60 | + { |
| 61 | + this._participants[to].Receive(from, message); |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + throw new ArgumentException("{0} not found", to); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +Colleague |
| 72 | +```cs |
| 73 | +public interface IParticipant |
| 74 | +{ |
| 75 | + string GetName(); |
| 76 | + void Send(string to, string message); |
| 77 | + void Receive(string from, string message); |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +ConcreteColleague |
| 82 | +```cs |
| 83 | +public class Participant : IParticipant |
| 84 | +{ |
| 85 | + private readonly string _name; |
| 86 | + private readonly IChatRoom _chatRoom; |
| 87 | + |
| 88 | + public Participant(string name, IChatRoom chatRoom) |
| 89 | + { |
| 90 | + this._name = name; |
| 91 | + this._chatRoom = chatRoom; |
| 92 | + |
| 93 | + this._chatRoom.RegisterParticipant(this); |
| 94 | + } |
| 95 | + public string GetName() |
| 96 | + { |
| 97 | + return this._name; |
| 98 | + } |
| 99 | + |
| 100 | + public void Send(string to, string message) |
| 101 | + { |
| 102 | + this._chatRoom.Send(this._name, to, message); |
| 103 | + } |
| 104 | + |
| 105 | + public void Receive(string from, string message) |
| 106 | + { |
| 107 | + Console.WriteLine("{0} to {1}: {2}", from, this._name, message); |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +Usage |
| 113 | +```cs |
| 114 | +IChatRoom chatRoom = new ChatRoom(); |
| 115 | + |
| 116 | +IParticipant einstein = new Participant("Einstein", chatRoom); |
| 117 | +IParticipant newton = new Participant("Newton", chatRoom); |
| 118 | +IParticipant galileo = new Participant("Galileo", chatRoom); |
| 119 | + |
| 120 | +newton.Send(galileo.GetName(), "I discoverd laws of motion"); |
| 121 | +einstein.Send(newton.GetName(), "I discovered how gravity works"); |
| 122 | +``` |
| 123 | + |
| 124 | +## Common Structure |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +* Mediator (IChatRoom) |
| 129 | + * defines an interface for communicating with Colleague objects |
| 130 | +* ConcreteMediator (ChatRoom) |
| 131 | + * implements cooperative behavior by coordinating Colleague objects. |
| 132 | + * knows and maintains its colleagues. |
| 133 | +* Colleague (IParticipant) |
| 134 | + * defines an interface for using Colleague objects. |
| 135 | +* ConcreateColleague (Participant) |
| 136 | + * each colleague knows its Mediator object |
| 137 | + * each colleague communicates with its mediator whenever it would have otherwise communicated with another colleague. |
| 138 | + |
| 139 | +_[Source: http://www.dofactory.com/net/mediator-design-patternttp://www.dofactory.com/net/adapter-design-pattern]_ |
0 commit comments