SlideShare a Scribd company logo
mediator
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(quot;Message sent : quot; + message);
        }
    }
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);
}
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 registered
                                   if(!workerObject.Message.Equals(senderObject.Message))
                                        workerObject.SendMessage(messageToSend);
                }
            }
    }
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);
Flow diagram
                        WorkerObj1




             Mediator
 SenderObj




                           Worker
                            Obj2
CCS Model

 SenderObject(reg,sendmedmsg,sendobjmsg,printmsg)=
  reg.sendmedmsg’.SenderObject<reg,sendmedmsg,sendobjmsg,printms
  g>

 Mediator(sendmedmsg,sendobj1msg,sendobj2msg)=
  sendmedmsg.sendobj1msg’.Mediator<sendmedmsg,sendobj1msg,sendo
  bj2msg> +
  sendmedmsg.sendobj2msg’.Mediator<sendmedmsg,sendobj1msg,send
  obj2msg>
 Workerobj1(reg,sendmedmsg,sendobjmsg,printmsg)=sendobjmsg.prin
  tmsg’.Workerobj1<reg,sendmedmsg,sendobjmsg,printmsg>
 Workerobj2(reg,sendmedmsg,sendobjmsg,printmsg)=sendobjmsg.prin
  tmsg’.Workerobj2<reg,sendmedmsg,sendobjmsg,printmsg>
Simulation on MWB

More Related Content

mediator

  • 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.
  • 3. Mediator Mediator promotes loose coupling by keeping objects from referring to each other explicitly
  • 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”.
  • 8. 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.
  • 9.  Balance the principle of decoupling with the principle of distributing responsibility evenly.  Be careful not to create a “controller” or “god” object.
  • 10. 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
  • 11. 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); } }
  • 12. 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); }
  • 13. 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
  • 14. 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); } } }
  • 15. 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);
  • 16. Flow diagram WorkerObj1 Mediator SenderObj Worker Obj2
  • 17. CCS Model  SenderObject(reg,sendmedmsg,sendobjmsg,printmsg)= reg.sendmedmsg’.SenderObject<reg,sendmedmsg,sendobjmsg,printms g>  Mediator(sendmedmsg,sendobj1msg,sendobj2msg)= sendmedmsg.sendobj1msg’.Mediator<sendmedmsg,sendobj1msg,sendo bj2msg> + sendmedmsg.sendobj2msg’.Mediator<sendmedmsg,sendobj1msg,send obj2msg>  Workerobj1(reg,sendmedmsg,sendobjmsg,printmsg)=sendobjmsg.prin tmsg’.Workerobj1<reg,sendmedmsg,sendobjmsg,printmsg>  Workerobj2(reg,sendmedmsg,sendobjmsg,printmsg)=sendobjmsg.prin tmsg’.Workerobj2<reg,sendmedmsg,sendobjmsg,printmsg>