SlideShare ist ein Scribd-Unternehmen logo
1 von 13
Downloaden Sie, um offline zu lesen
MEDIATOR DESIGN
PATTERN
Presented by:
Kuyseng Chheoun24-Aug-2014
MEDIATOR DESIGN
Intent
DeïŹne 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-deïŹned but
complex ways. The resulting interdependencies are
unstructured and difïŹcult to understand.
‱ reusing an object is difïŹcult 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.
‱ SimpliïŹed 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 trafïŹc controller.
Air trafïŹc controller (ATC) is a
mediator between ïŹ‚ights. It helps in
communication between ïŹ‚ights and
coordinates/controls landing, take-
off. Two ïŹ‚ights 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 ïŹ‚ights have
to interact with one another and
managing the show will be very
difïŹcult 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 ïŹrewalls, 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
ありがべう

Weitere Àhnliche Inhalte

Was ist angesagt?

Class and object_diagram
Class  and object_diagramClass  and object_diagram
Class and object_diagram
Sadhana28
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
soms_1
 
State Machine Diagram
State Machine DiagramState Machine Diagram
State Machine Diagram
Niloy Rocker
 
Builder pattern
Builder patternBuilder pattern
Builder pattern
Shakil Ahmed
 

Was ist angesagt? (20)

Advanced Structural Modeling
Advanced Structural ModelingAdvanced Structural Modeling
Advanced Structural Modeling
 
Design Pattern - Chain of Responsibility
Design Pattern - Chain of ResponsibilityDesign Pattern - Chain of Responsibility
Design Pattern - Chain of Responsibility
 
Class and object_diagram
Class  and object_diagramClass  and object_diagram
Class and object_diagram
 
Api Gateway
Api GatewayApi Gateway
Api Gateway
 
Monoliths and Microservices
Monoliths and Microservices Monoliths and Microservices
Monoliths and Microservices
 
Database replication
Database replicationDatabase replication
Database replication
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
State Machine Diagram
State Machine DiagramState Machine Diagram
State Machine Diagram
 
Proxy pattern
Proxy patternProxy pattern
Proxy pattern
 
Builder pattern
Builder patternBuilder pattern
Builder pattern
 
Facade pattern
Facade patternFacade pattern
Facade pattern
 
Distributed file systems dfs
Distributed file systems   dfsDistributed file systems   dfs
Distributed file systems dfs
 
CS8791 Unit 2 Cloud Enabling Technologies
CS8791 Unit 2 Cloud Enabling TechnologiesCS8791 Unit 2 Cloud Enabling Technologies
CS8791 Unit 2 Cloud Enabling Technologies
 
Composite Design Pattern
Composite Design PatternComposite Design Pattern
Composite Design Pattern
 
Facade Pattern
Facade PatternFacade Pattern
Facade Pattern
 
Class diagrams
Class diagramsClass diagrams
Class diagrams
 
From framework coupled code to #microservices through #DDD /by @codelytv
From framework coupled code to #microservices through #DDD /by @codelytvFrom framework coupled code to #microservices through #DDD /by @codelytv
From framework coupled code to #microservices through #DDD /by @codelytv
 
Iterator Design Pattern
Iterator Design PatternIterator Design Pattern
Iterator Design Pattern
 
Class diagram
Class diagramClass diagram
Class diagram
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)
 

Andere mochten auch

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

Andere mochten auch (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 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
 
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
 

Ähnlich wie Mediator Design Pattern

Mediator.pptx
Mediator.pptxMediator.pptx
Mediator.pptx
sree kanth
 

Ähnlich wie 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
 

KĂŒrzlich hochgeladen

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
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 

KĂŒrzlich hochgeladen (20)

VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
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...
 
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...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%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
 
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...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%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
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
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
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%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
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%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
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
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
 

Mediator Design Pattern

  • 3. Intent DeïŹne 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-deïŹned but complex ways. The resulting interdependencies are unstructured and difïŹcult to understand. ‱ reusing an object is difïŹcult 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. ‱ SimpliïŹed 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 trafïŹc controller. Air trafïŹc controller (ATC) is a mediator between ïŹ‚ights. It helps in communication between ïŹ‚ights and coordinates/controls landing, take- off. Two ïŹ‚ights 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 ïŹ‚ights have to interact with one another and managing the show will be very difïŹcult 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 ïŹrewalls, 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!