SlideShare una empresa de Scribd logo
1 de 13
Descargar para leer sin conexión
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
ありがとう

Más contenido relacionado

La actualidad más candente

[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
洪 鹏发
 
Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
Sara Torkey
 

La actualidad más candente (20)

Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)
 
Docker introduction &amp; benefits
Docker introduction &amp; benefitsDocker introduction &amp; benefits
Docker introduction &amp; benefits
 
Design Pattern - Observer Pattern
Design Pattern - Observer PatternDesign Pattern - Observer Pattern
Design Pattern - Observer Pattern
 
Getting Started with React.js
Getting Started with React.jsGetting Started with React.js
Getting Started with React.js
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
 
Creational pattern
Creational patternCreational pattern
Creational pattern
 
Docker 101 - from 0 to Docker in 30 minutes
Docker 101 - from 0 to Docker in 30 minutesDocker 101 - from 0 to Docker in 30 minutes
Docker 101 - from 0 to Docker in 30 minutes
 
Kubernetes architecture
Kubernetes architectureKubernetes architecture
Kubernetes architecture
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
 
Introduction to kubernetes
Introduction to kubernetesIntroduction to kubernetes
Introduction to kubernetes
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
Desarrollando un API con REST
Desarrollando un API con RESTDesarrollando un API con REST
Desarrollando un API con REST
 
Chapter 3: ado.net
Chapter 3: ado.netChapter 3: ado.net
Chapter 3: ado.net
 
Android Fragment
Android FragmentAndroid Fragment
Android Fragment
 
ADO .Net
ADO .Net ADO .Net
ADO .Net
 
Docker & kubernetes
Docker & kubernetesDocker & kubernetes
Docker & kubernetes
 
Docker 101 : Introduction to Docker and Containers
Docker 101 : Introduction to Docker and ContainersDocker 101 : Introduction to Docker and Containers
Docker 101 : Introduction to Docker and Containers
 
React js
React jsReact js
React js
 

Destacado

Mediator Pattern
Mediator PatternMediator Pattern
Mediator Pattern
Anuj Pawar
 
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
 

Destacado (15)

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 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
 

Similar a Mediator Design Pattern

Similar 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

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Último (20)

WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 

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!