mediator
TRANSCRIPT
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.
Mediator
Mediator promotes loose coupling by keeping objects from referring to each other explicitly
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”.
Unmediated
Mediated
Mediator
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.
Balance the principle of decoupling with the principle of distributing responsibility evenly.
Be careful not to create a “controller” or “god” object.
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
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("Message sent : " + message);}
}
WorkerObject senderObject = new WorkerObject("message0");WorkerObject workerObject1 = new WorkerObject("message1");WorkerObject workerObject2 = new WorkerObject("message2");WorkerObject workerObject3 = new WorkerObject("message3");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);}
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
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 registeredif(!workerObject.Message.Equals(senderObject.Message))
workerObject.SendMessage(messageToSend);}
}}
WorkerObject senderObject = new WorkerObject("message0");WorkerObject workerObject1 = new WorkerObject("message1");WorkerObject workerObject2 = new WorkerObject("message2");WorkerObject workerObject3 = new WorkerObject("message3");DoSomeMediation.Register(senderObject);DoSomeMediation.Register(workerObject1);DoSomeMediation.Register(workerObject2);DoSomeMediation.Register(workerObject3);DoSomeMediation.SendMessage(senderObject);
Flow diagramWorkerObj1
Worker Obj2
SenderObjMediator
CCS Model
SenderObject(reg,sendmedmsg,sendobjmsg,printmsg)= reg.sendmedmsg’.SenderObject<reg,sendmedmsg,sendobjmsg,printmsg>
Mediator(sendmedmsg,sendobj1msg,sendobj2msg)= sendmedmsg.sendobj1msg’.Mediator<sendmedmsg,sendobj1msg,sendobj2msg> + sendmedmsg.sendobj2msg’.Mediator<sendmedmsg,sendobj1msg,sendobj2msg>
Workerobj1(reg,sendmedmsg,sendobjmsg,printmsg)=sendobjmsg.printmsg’.Workerobj1<reg,sendmedmsg,sendobjmsg,printmsg>
Workerobj2(reg,sendmedmsg,sendobjmsg,printmsg)=sendobjmsg.printmsg’.Workerobj2<reg,sendmedmsg,sendobjmsg,printmsg>
Simulation on MWB