Mediator Design Pattern

625 views
488 views

Published on

It descripts about Mediator Pattern and give example code in ruby.

Published in: Software
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
625
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
39
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Mediator Design Pattern

  1. 1. MEDIATOR DESIGN PATTERN Presented by: Kuyseng Chheoun24-Aug-2014
  2. 2. MEDIATOR DESIGN
  3. 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. 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.
  5. 5. Structure
  6. 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. 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. 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. 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. 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. 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!
  12. 12. 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
  13. 13. ありがとう

×