SlideShare una empresa de Scribd logo
1 de 18
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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (17)

Vs c# lecture11
Vs c# lecture11Vs c# lecture11
Vs c# lecture11
 
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to Hooks
 
React tips
React tipsReact tips
React tips
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
Presentation cs313 (1)
Presentation cs313 (1)Presentation cs313 (1)
Presentation cs313 (1)
 
Lesson3
Lesson3Lesson3
Lesson3
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascript
 
React lecture
React lectureReact lecture
React lecture
 
AutoComplete
AutoCompleteAutoComplete
AutoComplete
 
16 18
16 1816 18
16 18
 
Rxjs swetugg
Rxjs swetuggRxjs swetugg
Rxjs swetugg
 
React js use contexts and useContext hook
React js use contexts and useContext hookReact js use contexts and useContext hook
React js use contexts and useContext hook
 
Deceptive simplicity of async and await
Deceptive simplicity of async and awaitDeceptive simplicity of async and await
Deceptive simplicity of async and await
 
Dependency Injection for Android
Dependency Injection for AndroidDependency Injection for Android
Dependency Injection for Android
 
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
 
React hooks beyond hype
React hooks beyond hypeReact hooks beyond hype
React hooks beyond hype
 
Ajax Part II
Ajax Part IIAjax Part II
Ajax Part II
 

Destacado

Practical Econ, U1, Packet 1
Practical Econ, U1, Packet 1Practical Econ, U1, Packet 1
Practical Econ, U1, Packet 1
NHSDAnderson
 
Mortel Scooter Campaign
Mortel Scooter CampaignMortel Scooter Campaign
Mortel Scooter Campaign
sdelastic
 
Wiki Technology By It Rocks
Wiki Technology By It RocksWiki Technology By It Rocks
Wiki Technology By It Rocks
naveenv
 

Destacado (20)

Europe
EuropeEurope
Europe
 
Tally Sheet Results - Technology Habits
Tally Sheet Results - Technology HabitsTally Sheet Results - Technology Habits
Tally Sheet Results - Technology Habits
 
Estandares aprendizaje mcs
Estandares aprendizaje mcsEstandares aprendizaje mcs
Estandares aprendizaje mcs
 
Retrospective 2008 The Surfer’s Year
Retrospective 2008 The Surfer’s YearRetrospective 2008 The Surfer’s Year
Retrospective 2008 The Surfer’s Year
 
Java Eye Architecture
Java Eye ArchitectureJava Eye Architecture
Java Eye Architecture
 
The practices, challenges and opportunities of board composition and leadersh...
The practices, challenges and opportunities of board composition and leadersh...The practices, challenges and opportunities of board composition and leadersh...
The practices, challenges and opportunities of board composition and leadersh...
 
Collaborative Assessment
Collaborative AssessmentCollaborative Assessment
Collaborative Assessment
 
Practical Econ, U1, Packet 1
Practical Econ, U1, Packet 1Practical Econ, U1, Packet 1
Practical Econ, U1, Packet 1
 
Debt Taxes
Debt TaxesDebt Taxes
Debt Taxes
 
Unit3
Unit3Unit3
Unit3
 
Èdip per a 4rt de la ESO
Èdip per a 4rt de la ESOÈdip per a 4rt de la ESO
Èdip per a 4rt de la ESO
 
Mortel Scooter Campaign
Mortel Scooter CampaignMortel Scooter Campaign
Mortel Scooter Campaign
 
geek_lifestyle
geek_lifestylegeek_lifestyle
geek_lifestyle
 
Ruby In Enterprise Development
Ruby In Enterprise DevelopmentRuby In Enterprise Development
Ruby In Enterprise Development
 
Cymphonix Launches iPhone App and New Version of Network Composer Software
Cymphonix Launches iPhone App and New Version of Network Composer SoftwareCymphonix Launches iPhone App and New Version of Network Composer Software
Cymphonix Launches iPhone App and New Version of Network Composer Software
 
0 to enterprise
0 to enterprise0 to enterprise
0 to enterprise
 
Wiki Technology By It Rocks
Wiki Technology By It RocksWiki Technology By It Rocks
Wiki Technology By It Rocks
 
Vision
VisionVision
Vision
 
что нас объединяет
что нас объединяетчто нас объединяет
что нас объединяет
 
第12週-2
第12週-2第12週-2
第12週-2
 

Similar a mediator

Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Mario Fusco
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objects
Phúc Đỗ
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
Adieu
 

Similar a mediator (20)

mediator2
mediator2mediator2
mediator2
 
Uncommon Design Patterns
Uncommon Design PatternsUncommon Design Patterns
Uncommon Design Patterns
 
OBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptOBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .ppt
 
Improving Correctness with Types Kats Conf
Improving Correctness with Types Kats ConfImproving Correctness with Types Kats Conf
Improving Correctness with Types Kats Conf
 
25-inheritance-polymorphism.ppt
25-inheritance-polymorphism.ppt25-inheritance-polymorphism.ppt
25-inheritance-polymorphism.ppt
 
Java 8 - Nuts and Bold - SFEIR Benelux
Java 8 - Nuts and Bold - SFEIR BeneluxJava 8 - Nuts and Bold - SFEIR Benelux
Java 8 - Nuts and Bold - SFEIR Benelux
 
Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oop
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Functional Core, Reactive Shell
Functional Core, Reactive ShellFunctional Core, Reactive Shell
Functional Core, Reactive Shell
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 
Javascript foundations: Introducing OO
Javascript foundations: Introducing OOJavascript foundations: Introducing OO
Javascript foundations: Introducing OO
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objects
 
Javascript
JavascriptJavascript
Javascript
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Oojs 1.1
Oojs 1.1Oojs 1.1
Oojs 1.1
 
Java script
Java scriptJava script
Java script
 
properties-how-do-i - Transcript.pdf
properties-how-do-i - Transcript.pdfproperties-how-do-i - Transcript.pdf
properties-how-do-i - Transcript.pdf
 

Último

➥🔝 7737669865 🔝▻ Tirupati Call-girls in Women Seeking Men 🔝Tirupati🔝 Escor...
➥🔝 7737669865 🔝▻ Tirupati Call-girls in Women Seeking Men  🔝Tirupati🔝   Escor...➥🔝 7737669865 🔝▻ Tirupati Call-girls in Women Seeking Men  🔝Tirupati🔝   Escor...
➥🔝 7737669865 🔝▻ Tirupati Call-girls in Women Seeking Men 🔝Tirupati🔝 Escor...
amitlee9823
 
Gabriel_Carter_EXPOLRATIONpp.pptx........
Gabriel_Carter_EXPOLRATIONpp.pptx........Gabriel_Carter_EXPOLRATIONpp.pptx........
Gabriel_Carter_EXPOLRATIONpp.pptx........
deejay178
 
Jual obat aborsi Jakarta ( 085657271886 )Cytote pil telat bulan penggugur kan...
Jual obat aborsi Jakarta ( 085657271886 )Cytote pil telat bulan penggugur kan...Jual obat aborsi Jakarta ( 085657271886 )Cytote pil telat bulan penggugur kan...
Jual obat aborsi Jakarta ( 085657271886 )Cytote pil telat bulan penggugur kan...
ZurliaSoop
 
0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf
0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf
0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf
ssuserded2d4
 
一比一原版(毕业证书)意大利米兰理工大学毕业证学位证可查学历认证
一比一原版(毕业证书)意大利米兰理工大学毕业证学位证可查学历认证一比一原版(毕业证书)意大利米兰理工大学毕业证学位证可查学历认证
一比一原版(毕业证书)意大利米兰理工大学毕业证学位证可查学历认证
epodumf6
 
Call Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
amitlee9823
 
Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
amitlee9823
 
Call Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
amitlee9823
 
Call Girls Bidadi ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Call Girls Bidadi ☎ 7737669865☎ Book Your One night Stand (Bangalore)Call Girls Bidadi ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Call Girls Bidadi ☎ 7737669865☎ Book Your One night Stand (Bangalore)
amitlee9823
 
Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
amitlee9823
 
Call Girls Hosur Road Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hosur Road Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hosur Road Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hosur Road Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
amitlee9823
 

Último (20)

WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Salarpur Sector 81 ( Noida)
 
➥🔝 7737669865 🔝▻ Tirupati Call-girls in Women Seeking Men 🔝Tirupati🔝 Escor...
➥🔝 7737669865 🔝▻ Tirupati Call-girls in Women Seeking Men  🔝Tirupati🔝   Escor...➥🔝 7737669865 🔝▻ Tirupati Call-girls in Women Seeking Men  🔝Tirupati🔝   Escor...
➥🔝 7737669865 🔝▻ Tirupati Call-girls in Women Seeking Men 🔝Tirupati🔝 Escor...
 
Dubai Call Girls Kiki O525547819 Call Girls Dubai Koko
Dubai Call Girls Kiki O525547819 Call Girls Dubai KokoDubai Call Girls Kiki O525547819 Call Girls Dubai Koko
Dubai Call Girls Kiki O525547819 Call Girls Dubai Koko
 
Hyderabad 💫✅💃 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATIS...
Hyderabad 💫✅💃 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATIS...Hyderabad 💫✅💃 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATIS...
Hyderabad 💫✅💃 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATIS...
 
Rearing technique of lac insect and their management
Rearing technique of lac insect and their managementRearing technique of lac insect and their management
Rearing technique of lac insect and their management
 
Gabriel_Carter_EXPOLRATIONpp.pptx........
Gabriel_Carter_EXPOLRATIONpp.pptx........Gabriel_Carter_EXPOLRATIONpp.pptx........
Gabriel_Carter_EXPOLRATIONpp.pptx........
 
Jual obat aborsi Jakarta ( 085657271886 )Cytote pil telat bulan penggugur kan...
Jual obat aborsi Jakarta ( 085657271886 )Cytote pil telat bulan penggugur kan...Jual obat aborsi Jakarta ( 085657271886 )Cytote pil telat bulan penggugur kan...
Jual obat aborsi Jakarta ( 085657271886 )Cytote pil telat bulan penggugur kan...
 
0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf
0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf
0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf0425-GDSC-TMU.pdf
 
Personal Brand Exploration - Fernando Negron
Personal Brand Exploration - Fernando NegronPersonal Brand Exploration - Fernando Negron
Personal Brand Exploration - Fernando Negron
 
一比一原版(毕业证书)意大利米兰理工大学毕业证学位证可查学历认证
一比一原版(毕业证书)意大利米兰理工大学毕业证学位证可查学历认证一比一原版(毕业证书)意大利米兰理工大学毕业证学位证可查学历认证
一比一原版(毕业证书)意大利米兰理工大学毕业证学位证可查学历认证
 
Brand Analysis for reggaeton artist Jahzel.
Brand Analysis for reggaeton artist Jahzel.Brand Analysis for reggaeton artist Jahzel.
Brand Analysis for reggaeton artist Jahzel.
 
Solution Manual for First Course in Abstract Algebra A, 8th Edition by John B...
Solution Manual for First Course in Abstract Algebra A, 8th Edition by John B...Solution Manual for First Course in Abstract Algebra A, 8th Edition by John B...
Solution Manual for First Course in Abstract Algebra A, 8th Edition by John B...
 
Call Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hosur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Chikkabanavara Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
 
Toxicokinetics studies.. (toxicokinetics evaluation in preclinical studies)
Toxicokinetics studies.. (toxicokinetics evaluation in preclinical studies)Toxicokinetics studies.. (toxicokinetics evaluation in preclinical studies)
Toxicokinetics studies.. (toxicokinetics evaluation in preclinical studies)
 
Call Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hoodi Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
Call Girls Bidadi ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Call Girls Bidadi ☎ 7737669865☎ Book Your One night Stand (Bangalore)Call Girls Bidadi ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Call Girls Bidadi ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jayanagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
 
Call Girls Hosur Road Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hosur Road Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hosur Road Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hosur Road Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...
TEST BANK For Evidence-Based Practice for Nurses Appraisal and Application of...
 

mediator

  • 1.
  • 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>