SlideShare uma empresa Scribd logo
1 de 17
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”.
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

Mais conteúdo relacionado

Mais procurados

Colegio nacional dario guevara mayorga
Colegio nacional dario guevara mayorgaColegio nacional dario guevara mayorga
Colegio nacional dario guevara mayorga
Jefferson
 
Krazykoder struts2 domain_object_modeldriven
Krazykoder struts2 domain_object_modeldrivenKrazykoder struts2 domain_object_modeldriven
Krazykoder struts2 domain_object_modeldriven
Krazy Koder
 
User controls
User controlsUser controls
User controls
aspnet123
 
Java%20 new%20faq.doc 0
Java%20 new%20faq.doc 0Java%20 new%20faq.doc 0
Java%20 new%20faq.doc 0
aravind_aashu
 
Validation using javascripts by karan chanana
Validation using javascripts by karan chananaValidation using javascripts by karan chanana
Validation using javascripts by karan chanana
karan info
 
Add two numbers karan chanana
Add two numbers karan chananaAdd two numbers karan chanana
Add two numbers karan chanana
karan info
 
Inventory management
Inventory managementInventory management
Inventory management
Rajeev Sharan
 

Mais procurados (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
 

Destaque

4th grade curriculum night classroom 10 11
4th grade curriculum night classroom  10 114th grade curriculum night classroom  10 11
4th grade curriculum night classroom 10 11
Bret Biornstad
 
Presentation1 Intro To Computers Week 7
Presentation1 Intro To Computers Week 7Presentation1 Intro To Computers Week 7
Presentation1 Intro To Computers Week 7
Ashley Bohannon
 
PICTURES TO DESCRIBE
PICTURES TO DESCRIBEPICTURES TO DESCRIBE
PICTURES TO DESCRIBE
lola guillen
 
Soc. Unit I, Packet 2
Soc. Unit I, Packet 2Soc. Unit I, Packet 2
Soc. Unit I, Packet 2
NHSDAnderson
 

Destaque (20)

IGNITE MySQL - Backups Don't Make Me Money
IGNITE MySQL - Backups Don't Make Me MoneyIGNITE MySQL - Backups Don't Make Me Money
IGNITE MySQL - Backups Don't Make Me Money
 
Portfolio
PortfolioPortfolio
Portfolio
 
christmas
christmaschristmas
christmas
 
4th grade curriculum night classroom 10 11
4th grade curriculum night classroom  10 114th grade curriculum night classroom  10 11
4th grade curriculum night classroom 10 11
 
Varnish Cache - International PHP Conference Fall 2012
Varnish Cache - International PHP Conference Fall 2012Varnish Cache - International PHP Conference Fall 2012
Varnish Cache - International PHP Conference Fall 2012
 
Come presentarsi efficacemente a un investitore
Come presentarsi efficacemente a un investitoreCome presentarsi efficacemente a un investitore
Come presentarsi efficacemente a un investitore
 
Mohammed Farrag Resume
Mohammed Farrag ResumeMohammed Farrag Resume
Mohammed Farrag Resume
 
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...
 
Lecture 5 Kernel Development
Lecture 5 Kernel DevelopmentLecture 5 Kernel Development
Lecture 5 Kernel Development
 
Pankaj_Resume
Pankaj_ResumePankaj_Resume
Pankaj_Resume
 
Brand Strategy Overview For Nbbn
Brand Strategy Overview For NbbnBrand Strategy Overview For Nbbn
Brand Strategy Overview For Nbbn
 
Unit1Business-English
Unit1Business-EnglishUnit1Business-English
Unit1Business-English
 
Bando Start Up Er
Bando Start Up ErBando Start Up Er
Bando Start Up Er
 
Resume
ResumeResume
Resume
 
Presentation1 Intro To Computers Week 7
Presentation1 Intro To Computers Week 7Presentation1 Intro To Computers Week 7
Presentation1 Intro To Computers Week 7
 
PICTURES TO DESCRIBE
PICTURES TO DESCRIBEPICTURES TO DESCRIBE
PICTURES TO DESCRIBE
 
Soc. Unit I, Packet 2
Soc. Unit I, Packet 2Soc. Unit I, Packet 2
Soc. Unit I, Packet 2
 
Lecture2 process structure and programming
Lecture2   process structure and programmingLecture2   process structure and programming
Lecture2 process structure and programming
 
Social Media Platforms for Small Companies
Social Media Platforms for Small CompaniesSocial Media Platforms for Small Companies
Social Media Platforms for Small Companies
 
[ArabBSD] Unix Basics
[ArabBSD] Unix Basics[ArabBSD] Unix Basics
[ArabBSD] Unix Basics
 

Semelhante a mediator2

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 Đỗ
 
Java script objects 1
Java script objects 1Java script objects 1
Java script objects 1
H K
 
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
 

Semelhante a mediator2 (20)

mediator
mediatormediator
mediator
 
Javascript
JavascriptJavascript
Javascript
 
25-inheritance-polymorphism.ppt
25-inheritance-polymorphism.ppt25-inheritance-polymorphism.ppt
25-inheritance-polymorphism.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 3
Ajax chap 3Ajax chap 3
Ajax chap 3
 
Ajax chap 2.-part 1
Ajax chap 2.-part 1Ajax chap 2.-part 1
Ajax chap 2.-part 1
 
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
 
Linked In Presentation
Linked In PresentationLinked In Presentation
Linked In Presentation
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

mediator2

  • 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”.
  • 5.
  • 6.
  • 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>