SlideShare uma empresa Scribd logo
1 de 13
Baixar para ler offline
MEDIATOR DESIGN
PATTERN
Presented by:
Kuyseng Chheoun24-Aug-2014
MEDIATOR DESIGN
Intent
Define an object that
encapsulates how a set of
objects interact. Mediator
promotes loose coupling by
keeping objects from
referring to each other
explicitly, and it lets you vary
their interaction
independently.
Ref: Head First Design Pattern P.622
Applicability
Use the Mediator pattern when:
• a set of objects communicate in well-defined but
complex ways. The resulting interdependencies are
unstructured and difficult to understand.
• reusing an object is difficult because it refers to and
communicates with many other objects.
• a behavior that's distributed between several classes
should be customizable without a lot of subclassing.
Structure
Consequences
• Comprehension - The mediator encapsulate the logic of
mediation between the colleagues. From this reason it' more
easier to understand this logic since it is kept in only one class.
• Decoupled Colleagues - The colleague classes are totally
decoupled. Adding a new colleague class is very easy due to
this decoupling level.
• Simplified object protocols - The colleague objects need to
communicate only with the mediator objects. Practically the
mediator pattern reduce the required communication
channels(protocols) from many to many to one to many and
many to one.
• Limits Subclassing - Because the entire
communication logic is encapsulated by the
mediator class, when this logic need to be extended
only the mediator class need to be extended.
• Complexity - in practice the mediators tends to
become more complex and complex. A good
practice is to take care to make the mediator classes
responsible only for the communication part. For
example when implementing different screens the
the screen class should not contain code which is
not a part of the screen operations. It should be put
in some other classes.
Examples
Example 1 - Air traffic controller.
Air traffic controller (ATC) is a
mediator between flights. It helps in
communication between flights and
coordinates/controls landing, take-
off. Two flights need not interact
directly and there is no dependency
between them. This dependency is
solved by the mediator ATC. If
ATC is not there all the flights have
to interact with one another and
managing the show will be very
difficult and things may go wrong.
Example 2 - Chat application
In a chat application we can have several participants.
It's not a good idea to connect each participant to all
the others because the number of connections would
be really high, there would be technical problems due
to proxies and firewalls, etc... . The most appropriate
solution is to have a hub where all participants will
connect; this hub is just the mediator class.
Sample Codes (other example)
class Buyer!
attr_accessor :budget!
!
def initialize budget: 0!
@budget = budget!
end!
!
def follow_agency agency!
@agency = agency!
end!
!
def buy house!
@agency.mediate_purchase house, self!
end !
end!
class Seller!
attr_accessor :balance!
!
def initialize balance: 0!
@balance = balance!
end!
!
def publish agency, house!
agency.register house!
end!
!
end!
class Agency!
def initialize!
@houses = []!
end!
!
def register house!
@houses << house!
end!
!
def mediate_purchase house, buyer!
if conditions_are_met? house, buyer!
handle_money house, buyer!
house.owner = buyer!
end!
end!
!
private!
!
def conditions_are_met? house, buyer!
@houses.include?(house) && buyer.budget >=
house.price!
end!
!
def pay seller, amount!
seller.balance += amount!
end!
!
def charge buyer, amount!
buyer.budget -= amount!
end!
!
def handle_money house, buyer!
pay house.owner, house.price!
charge buyer, house.price!
end!
end!
class House!
attr_accessor :owner!
attr_reader :price!
!
def initialize rooms: 0, price: 0, owner: nil!
@rooms = rooms!
@price = price!
@owner = owner!
end!
end!
References
• http://www.oodesign.com/mediator-pattern.html
• http://code.tutsplus.com/courses/gang-of-four-
design-patterns-in-ruby
• http://www.amazon.com/Design-Patterns-
Elements-Reusable-Object-Oriented/dp/
0201633612
ありがとう

Mais conteúdo relacionado

Mais procurados

Slide 4 Interaction Diagram
Slide 4 Interaction DiagramSlide 4 Interaction Diagram
Slide 4 Interaction Diagram
Niloy Rocker
 

Mais procurados (20)

Flyweight pattern
Flyweight patternFlyweight pattern
Flyweight pattern
 
Object diagram
Object diagramObject diagram
Object diagram
 
Builder pattern
Builder patternBuilder pattern
Builder pattern
 
Chain of responsibility
Chain of responsibilityChain of responsibility
Chain of responsibility
 
Flyweight Pattern
Flyweight PatternFlyweight Pattern
Flyweight Pattern
 
CQRS and Event Sourcing
CQRS and Event SourcingCQRS and Event Sourcing
CQRS and Event Sourcing
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
 
Object Oriented Design
Object Oriented DesignObject Oriented Design
Object Oriented Design
 
Event handling
Event handlingEvent handling
Event handling
 
Introduction to Design Pattern
Introduction to Design  PatternIntroduction to Design  Pattern
Introduction to Design Pattern
 
Architecture of .net framework
Architecture of .net frameworkArchitecture of .net framework
Architecture of .net framework
 
4 memory management bb
4   memory management bb4   memory management bb
4 memory management bb
 
Decorator Design Pattern
Decorator Design PatternDecorator Design Pattern
Decorator Design Pattern
 
Exception Handling in VB.Net
Exception Handling in VB.NetException Handling in VB.Net
Exception Handling in VB.Net
 
Slide 4 Interaction Diagram
Slide 4 Interaction DiagramSlide 4 Interaction Diagram
Slide 4 Interaction Diagram
 
Java Course 11: Design Patterns
Java Course 11: Design PatternsJava Course 11: Design Patterns
Java Course 11: Design Patterns
 
SOLID principles
SOLID principlesSOLID principles
SOLID principles
 
The Singleton Pattern Presentation
The Singleton Pattern PresentationThe Singleton Pattern Presentation
The Singleton Pattern Presentation
 
Software Engineering : OOAD using UML
Software Engineering : OOAD using UMLSoftware Engineering : OOAD using UML
Software Engineering : OOAD using UML
 
Common language runtime clr
Common language runtime clrCommon language runtime clr
Common language runtime clr
 

Destaque

Mediator Pattern
Mediator PatternMediator Pattern
Mediator Pattern
Anuj Pawar
 
Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
Sara Torkey
 
Deploying and releasing applications
Deploying and releasing applicationsDeploying and releasing applications
Deploying and releasing applications
Ma Xuebin
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
soms_1
 
human computer interface
human computer interfacehuman computer interface
human computer interface
Santosh Kumar
 
USER INTERFACE DESIGN PPT
USER INTERFACE DESIGN PPTUSER INTERFACE DESIGN PPT
USER INTERFACE DESIGN PPT
vicci4041
 

Destaque (16)

Memento pattern
Memento patternMemento pattern
Memento pattern
 
Software Design Trilogy Part II - Design Patterns for Rubyists
Software Design Trilogy Part II - Design Patterns for RubyistsSoftware Design Trilogy Part II - Design Patterns for Rubyists
Software Design Trilogy Part II - Design Patterns for Rubyists
 
Mediator Pattern
Mediator PatternMediator Pattern
Mediator Pattern
 
Decorator design pattern (A Gift Wrapper)
Decorator design pattern (A Gift Wrapper)Decorator design pattern (A Gift Wrapper)
Decorator design pattern (A Gift Wrapper)
 
The Decorator Pattern
The Decorator PatternThe Decorator Pattern
The Decorator Pattern
 
Human Factors and User Interface Design
Human Factors and User Interface DesignHuman Factors and User Interface Design
Human Factors and User Interface Design
 
Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
 
Observer Pattern
Observer PatternObserver Pattern
Observer Pattern
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Deploying and releasing applications
Deploying and releasing applicationsDeploying and releasing applications
Deploying and releasing applications
 
Chain of Responsibility Pattern
Chain of Responsibility PatternChain of Responsibility Pattern
Chain of Responsibility Pattern
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design Pattern
 
Mediator
MediatorMediator
Mediator
 
human computer interface
human computer interfacehuman computer interface
human computer interface
 
USER INTERFACE DESIGN PPT
USER INTERFACE DESIGN PPTUSER INTERFACE DESIGN PPT
USER INTERFACE DESIGN PPT
 

Semelhante a Mediator Design Pattern

Semelhante a Mediator Design Pattern (20)

Mediator.pptx
Mediator.pptxMediator.pptx
Mediator.pptx
 
Design Principles to design Patterns
Design Principles to design PatternsDesign Principles to design Patterns
Design Principles to design Patterns
 
Effective Software Design
Effective Software Design Effective Software Design
Effective Software Design
 
Software design principles - jinal desai
Software design principles - jinal desaiSoftware design principles - jinal desai
Software design principles - jinal desai
 
System software design1
System software design1System software design1
System software design1
 
A Tale of Two Patterns
A Tale of Two PatternsA Tale of Two Patterns
A Tale of Two Patterns
 
Agile cards
Agile cardsAgile cards
Agile cards
 
Design Pattern Notes: Nagpur University
Design Pattern Notes: Nagpur UniversityDesign Pattern Notes: Nagpur University
Design Pattern Notes: Nagpur University
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
 
Software Patterns
Software PatternsSoftware Patterns
Software Patterns
 
Grasp oose week 14.pdf
Grasp oose week 14.pdfGrasp oose week 14.pdf
Grasp oose week 14.pdf
 
effective modular design.pptx
effective modular design.pptxeffective modular design.pptx
effective modular design.pptx
 
Dependency Injection, Design Principles and Patterns
Dependency Injection, Design Principles and PatternsDependency Injection, Design Principles and Patterns
Dependency Injection, Design Principles and Patterns
 
DP PPTS by BK.pptx
DP PPTS by BK.pptxDP PPTS by BK.pptx
DP PPTS by BK.pptx
 
SAD05 - Encapsulation
SAD05 - EncapsulationSAD05 - Encapsulation
SAD05 - Encapsulation
 
CHAPTER 3.pptx
CHAPTER 3.pptxCHAPTER 3.pptx
CHAPTER 3.pptx
 
Grasp principles
Grasp principlesGrasp principles
Grasp principles
 
Software Design principales
Software Design principalesSoftware Design principales
Software Design principales
 
Architecture and design
Architecture and designArchitecture and design
Architecture and design
 
Nina Grantcharova - Approach to Separation of Concerns via Design Patterns
Nina Grantcharova - Approach to Separation of Concerns via Design PatternsNina Grantcharova - Approach to Separation of Concerns via Design Patterns
Nina Grantcharova - Approach to Separation of Concerns via Design Patterns
 

Último

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Último (20)

Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 

Mediator Design Pattern

  • 3. Intent Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. Ref: Head First Design Pattern P.622
  • 4. Applicability Use the Mediator pattern when: • a set of objects communicate in well-defined but complex ways. The resulting interdependencies are unstructured and difficult to understand. • reusing an object is difficult because it refers to and communicates with many other objects. • a behavior that's distributed between several classes should be customizable without a lot of subclassing.
  • 6. Consequences • Comprehension - The mediator encapsulate the logic of mediation between the colleagues. From this reason it' more easier to understand this logic since it is kept in only one class. • Decoupled Colleagues - The colleague classes are totally decoupled. Adding a new colleague class is very easy due to this decoupling level. • Simplified object protocols - The colleague objects need to communicate only with the mediator objects. Practically the mediator pattern reduce the required communication channels(protocols) from many to many to one to many and many to one.
  • 7. • Limits Subclassing - Because the entire communication logic is encapsulated by the mediator class, when this logic need to be extended only the mediator class need to be extended. • Complexity - in practice the mediators tends to become more complex and complex. A good practice is to take care to make the mediator classes responsible only for the communication part. For example when implementing different screens the the screen class should not contain code which is not a part of the screen operations. It should be put in some other classes.
  • 8. Examples Example 1 - Air traffic controller. Air traffic controller (ATC) is a mediator between flights. It helps in communication between flights and coordinates/controls landing, take- off. Two flights need not interact directly and there is no dependency between them. This dependency is solved by the mediator ATC. If ATC is not there all the flights have to interact with one another and managing the show will be very difficult and things may go wrong.
  • 9. Example 2 - Chat application In a chat application we can have several participants. It's not a good idea to connect each participant to all the others because the number of connections would be really high, there would be technical problems due to proxies and firewalls, etc... . The most appropriate solution is to have a hub where all participants will connect; this hub is just the mediator class.
  • 10. Sample Codes (other example) class Buyer! attr_accessor :budget! ! def initialize budget: 0! @budget = budget! end! ! def follow_agency agency! @agency = agency! end! ! def buy house! @agency.mediate_purchase house, self! end ! end! class Seller! attr_accessor :balance! ! def initialize balance: 0! @balance = balance! end! ! def publish agency, house! agency.register house! end! ! end!
  • 11. class Agency! def initialize! @houses = []! end! ! def register house! @houses << house! end! ! def mediate_purchase house, buyer! if conditions_are_met? house, buyer! handle_money house, buyer! house.owner = buyer! end! end! ! private! ! def conditions_are_met? house, buyer! @houses.include?(house) && buyer.budget >= house.price! end! ! def pay seller, amount! seller.balance += amount! end! ! def charge buyer, amount! buyer.budget -= amount! end! ! def handle_money house, buyer! pay house.owner, house.price! charge buyer, house.price! end! end! class House! attr_accessor :owner! attr_reader :price! ! def initialize rooms: 0, price: 0, owner: nil! @rooms = rooms! @price = price! @owner = owner! end! end!