SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
Mediator Pattern
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”.
mediator
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




SenderObj




                           Worker
                            Obj2
CCS Model

• SenderObject(reg,sendmedmsg,sendobjmsg,printmsg)=
  reg.sendmedmsg’.SenderObject<reg,sendmedmsg,sendobjmsg,printmsg>

• Mediator(sendmedmsg,sendobj1msg,sendobj2msg)=
  sendmedmsg.sendobj1’msg.Mediator<sendmedmsg,sendobj1msg,sendob
  j2msg> +
  sendmedmsg.sendobj2msg.Mediator<sendmedmsg,sendobj1msg,sendobj
  2msg>
• Workerobj1(reg,sendmedmsg,sendobjmsg,printmsg)=sendobjmsg.printms
  g’.Workerobj1<reg,sendmedmsg,sendobjmsg,printmsg>
• Workerobj2(reg,sendmedmsg,sendobjmsg,printmsg)=sendobjmsg.printms
  g’.Workerobj2<reg,sendmedmsg,sendobjmsg,printmsg>
Simulation on MWB

Contenu connexe

Tendances

Colegio nacional dario guevara mayorga
Colegio nacional dario guevara mayorgaColegio nacional dario guevara mayorga
Colegio nacional dario guevara mayorgaJefferson
 
Cordova training : Day 4 - Advanced Javascript
Cordova training : Day 4 - Advanced JavascriptCordova training : Day 4 - Advanced Javascript
Cordova training : Day 4 - Advanced JavascriptBinu Paul
 
Krazykoder struts2 domain_object_modeldriven
Krazykoder struts2 domain_object_modeldrivenKrazykoder struts2 domain_object_modeldriven
Krazykoder struts2 domain_object_modeldrivenKrazy Koder
 
User controls
User controlsUser controls
User controlsaspnet123
 
Java%20 new%20faq.doc 0
Java%20 new%20faq.doc 0Java%20 new%20faq.doc 0
Java%20 new%20faq.doc 0aravind_aashu
 
jQuery -Chapter 2 - Selectors and Events
jQuery -Chapter 2 - Selectors and Events jQuery -Chapter 2 - Selectors and Events
jQuery -Chapter 2 - Selectors and Events WebStackAcademy
 
Excel to Use Rest API to communicate with a server
Excel to Use Rest API to communicate with a serverExcel to Use Rest API to communicate with a server
Excel to Use Rest API to communicate with a serverdocuboy
 
Validation using javascripts by karan chanana
Validation using javascripts by karan chananaValidation using javascripts by karan chanana
Validation using javascripts by karan chananakaran info
 
Add two numbers karan chanana
Add two numbers karan chananaAdd two numbers karan chanana
Add two numbers karan chananakaran info
 
Inventory management
Inventory managementInventory management
Inventory managementRajeev Sharan
 

Tendances (16)

Colegio nacional dario guevara mayorga
Colegio nacional dario guevara mayorgaColegio nacional dario guevara mayorga
Colegio nacional dario guevara mayorga
 
Cordova training : Day 4 - Advanced Javascript
Cordova training : Day 4 - Advanced JavascriptCordova training : Day 4 - Advanced Javascript
Cordova training : Day 4 - Advanced Javascript
 
Krazykoder struts2 domain_object_modeldriven
Krazykoder struts2 domain_object_modeldrivenKrazykoder struts2 domain_object_modeldriven
Krazykoder struts2 domain_object_modeldriven
 
User controls
User controlsUser controls
User controls
 
Dagger1
Dagger1Dagger1
Dagger1
 
Web 5 | JavaScript Events
Web 5 | JavaScript EventsWeb 5 | JavaScript Events
Web 5 | JavaScript Events
 
MVC 2.0 - A Breakthrough
MVC 2.0 - A BreakthroughMVC 2.0 - A Breakthrough
MVC 2.0 - A Breakthrough
 
Web 6 | JavaScript DOM
Web 6 | JavaScript DOMWeb 6 | JavaScript DOM
Web 6 | JavaScript DOM
 
Java%20 new%20faq.doc 0
Java%20 new%20faq.doc 0Java%20 new%20faq.doc 0
Java%20 new%20faq.doc 0
 
Javascript 2
Javascript 2Javascript 2
Javascript 2
 
jQuery -Chapter 2 - Selectors and Events
jQuery -Chapter 2 - Selectors and Events jQuery -Chapter 2 - Selectors and Events
jQuery -Chapter 2 - Selectors and Events
 
Excel to Use Rest API to communicate with a server
Excel to Use Rest API to communicate with a serverExcel to Use Rest API to communicate with a server
Excel to Use Rest API to communicate with a server
 
Dynamic databinding
Dynamic databindingDynamic databinding
Dynamic databinding
 
Validation using javascripts by karan chanana
Validation using javascripts by karan chananaValidation using javascripts by karan chanana
Validation using javascripts by karan chanana
 
Add two numbers karan chanana
Add two numbers karan chananaAdd two numbers karan chanana
Add two numbers karan chanana
 
Inventory management
Inventory managementInventory management
Inventory management
 

En vedette

En vedette (7)

Mediator pattern
Mediator patternMediator pattern
Mediator pattern
 
Design Pattern lecture 4
Design Pattern lecture 4Design Pattern lecture 4
Design Pattern lecture 4
 
Mediator Pattern
Mediator PatternMediator Pattern
Mediator Pattern
 
Proxy design pattern
Proxy design patternProxy design pattern
Proxy design pattern
 
Mediator pattern
Mediator patternMediator pattern
Mediator pattern
 
Mediator
MediatorMediator
Mediator
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 

Similaire à mediator

25-inheritance-polymorphism.ppt
25-inheritance-polymorphism.ppt25-inheritance-polymorphism.ppt
25-inheritance-polymorphism.pptPiyushAery
 
information on -inheritance-polymorphism in java.ppt
information on -inheritance-polymorphism in java.pptinformation on -inheritance-polymorphism in java.ppt
information on -inheritance-polymorphism in java.pptssuserf170c4
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object ReferencesTareq Hasan
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objectsPhúc Đỗ
 
Uncommon Design Patterns
Uncommon Design PatternsUncommon Design Patterns
Uncommon Design PatternsStefano Fago
 
Javascript foundations: Introducing OO
Javascript foundations: Introducing OOJavascript foundations: Introducing OO
Javascript foundations: Introducing OOJohn Hunter
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshowilias ahmed
 
Building .NET-based Applications with C#
Building .NET-based Applications with C#Building .NET-based Applications with C#
Building .NET-based Applications with C#Umar Farooq
 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Jalpesh Vasa
 
Metaprogramming in JavaScript
Metaprogramming in JavaScriptMetaprogramming in JavaScript
Metaprogramming in JavaScriptMehdi Valikhani
 
Java script objects 1
Java script objects 1Java script objects 1
Java script objects 1H K
 
Realm Mobile Database - An Introduction
Realm Mobile Database - An IntroductionRealm Mobile Database - An Introduction
Realm Mobile Database - An IntroductionKnoldus Inc.
 
descriptive programming
descriptive programmingdescriptive programming
descriptive programmingAnand Dhana
 
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 & STMMario Fusco
 

Similaire à mediator (20)

mediator
mediatormediator
mediator
 
Javascript
JavascriptJavascript
Javascript
 
25-inheritance-polymorphism.ppt
25-inheritance-polymorphism.ppt25-inheritance-polymorphism.ppt
25-inheritance-polymorphism.ppt
 
information on -inheritance-polymorphism in java.ppt
information on -inheritance-polymorphism in java.pptinformation on -inheritance-polymorphism in java.ppt
information on -inheritance-polymorphism in java.ppt
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objects
 
Uncommon Design Patterns
Uncommon Design PatternsUncommon Design Patterns
Uncommon Design Patterns
 
Javascript foundations: Introducing OO
Javascript foundations: Introducing OOJavascript foundations: Introducing OO
Javascript foundations: Introducing OO
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Building .NET-based Applications with C#
Building .NET-based Applications with C#Building .NET-based Applications with C#
Building .NET-based Applications with C#
 
Week 9 IUB c#
Week 9 IUB c#Week 9 IUB c#
Week 9 IUB c#
 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1
 
Metaprogramming in JavaScript
Metaprogramming in JavaScriptMetaprogramming in JavaScript
Metaprogramming in JavaScript
 
Ajax chap 2.-part 1
Ajax chap 2.-part 1Ajax chap 2.-part 1
Ajax chap 2.-part 1
 
Ajax chap 3
Ajax chap 3Ajax chap 3
Ajax chap 3
 
Metaworks3
Metaworks3Metaworks3
Metaworks3
 
Java script objects 1
Java script objects 1Java script objects 1
Java script objects 1
 
Realm Mobile Database - An Introduction
Realm Mobile Database - An IntroductionRealm Mobile Database - An Introduction
Realm Mobile Database - An Introduction
 
descriptive programming
descriptive programmingdescriptive programming
descriptive programming
 
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
 

Dernier

AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesMd Hossain Ali
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Commit University
 
GenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation IncGenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation IncObject Automation
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioChristian Posta
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintMahmoud Rabie
 
PicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer ServicePicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer ServiceRenan Moreira de Oliveira
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxGDSC PJATK
 
Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.francesco barbera
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8DianaGray10
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdfPedro Manuel
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfAijun Zhang
 
RAG Patterns and Vector Search in Generative AI
RAG Patterns and Vector Search in Generative AIRAG Patterns and Vector Search in Generative AI
RAG Patterns and Vector Search in Generative AIUdaiappa Ramachandran
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPathCommunity
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...Aggregage
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.YounusS2
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024D Cloud Solutions
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024SkyPlanner
 

Dernier (20)

AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)
 
GenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation IncGenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation Inc
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and Istio
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership Blueprint
 
PicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer ServicePicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer Service
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptx
 
Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdf
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdf
 
RAG Patterns and Vector Search in Generative AI
RAG Patterns and Vector Search in Generative AIRAG Patterns and Vector Search in Generative AI
RAG Patterns and Vector Search in Generative AI
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation Developers
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024
 

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”.
  • 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);
  • 15. Flow diagram WorkerObj1 SenderObj Worker Obj2
  • 16. CCS Model • SenderObject(reg,sendmedmsg,sendobjmsg,printmsg)= reg.sendmedmsg’.SenderObject<reg,sendmedmsg,sendobjmsg,printmsg> • Mediator(sendmedmsg,sendobj1msg,sendobj2msg)= sendmedmsg.sendobj1’msg.Mediator<sendmedmsg,sendobj1msg,sendob j2msg> + sendmedmsg.sendobj2msg.Mediator<sendmedmsg,sendobj1msg,sendobj 2msg> • Workerobj1(reg,sendmedmsg,sendobjmsg,printmsg)=sendobjmsg.printms g’.Workerobj1<reg,sendmedmsg,sendobjmsg,printmsg> • Workerobj2(reg,sendmedmsg,sendobjmsg,printmsg)=sendobjmsg.printms g’.Workerobj2<reg,sendmedmsg,sendobjmsg,printmsg>