2. Motivations
• lots of classes in the design of framework
• If certain principles are not applied the final
framework will end in a total mess
• to avoid tight coupled frameworks, we need a
mechanism to facilitate the interaction
between objects in a manner in that objects
are not aware of the existence of other
objects.
4. Intent
• Define an object that encapsulates how a set
of objects interact.
• Design an intermediary to decouple
many peers
• Promote the many-to-many relationships
between interacting peers to “full
object status”.
7. Check list
• Identify a collection of interacting objects that
would benefit from mutual decoupling.
• Encapsulate those interactions in the
abstraction of a new class.
• Create an instance of that new class and
rework all “peer” objects to interact with the
Mediator only.
8. • Balance the principle of decoupling with the
principle of distributing responsibility evenly.
• Be careful not to create a “controller” or
“god” object.
9. Mediator design example
• Let us consider design of a class in which one
object send messages to other objects by
means of if then if condition explicitly
10. public class WorkerObject
{
private string _message;
public WorkerObject(string message)
{
_message = message;
}
public string Message
{
get{return _message;}
set{_message = value;
}
public SendMessage(string message)
{
Console.WriteLine(quot;Message sent : quot; + message);
}
}
11. WorkerObject senderObject = new WorkerObject(quot;message0quot;);
WorkerObject workerObject1 = new WorkerObject(quot;message1quot;);
WorkerObject workerObject2 = new WorkerObject(quot;message2quot;);
WorkerObject workerObject3 = new WorkerObject(quot;message3quot;);
if(!workerObject1.Message.Equals(senderObject.Message)
{
workerObject1.SendMessage(senderObject.Message);
}
if(!workerObject2.Message.Equals(senderObject.Message)
{
workerObject2.SendMessage(senderObject.Message);
}
if(!workerObject3.Message.Equals(senderObject.Message)
{
workerObject3.SendMessage(senderObject.Message);
}
12. Mediator
• we create a mediator class,
DoSomeMediation.
• This class contains two methods Register and
SendMessage
• The Register method catalogs all the classes
we want to mediate between.
• The SendMessage method is where the
functional code actually exists
13. public class DoSomeMediation
{
private static ArrayList _workerObjects = new ArrayList();
public static int Register(WorkerObject workerObject)
{
return _workerObjects.Add(workerObject);
}
public static void SendMessage(WorkerObject senderObject)
{
if(senderObject == null) return;
string messageToSend = senderObject.Message;
foreach(WorkerObject workerObject in _workerObjects)
{
//send message to all other objects registered
if(!workerObject.Message.Equals(senderObject.Message))
workerObject.SendMessage(messageToSend);
}
}
}
14. WorkerObject senderObject = new WorkerObject(quot;message0quot;);
WorkerObject workerObject1 = new WorkerObject(quot;message1quot;);
WorkerObject workerObject2 = new WorkerObject(quot;message2quot;);
WorkerObject workerObject3 = new WorkerObject(quot;message3quot;);
DoSomeMediation.Register(senderObject);
DoSomeMediation.Register(workerObject1);
DoSomeMediation.Register(workerObject2);
DoSomeMediation.Register(workerObject3);
DoSomeMediation.SendMessage(senderObject);