mediator design pattern

13
MEDIATOR DESIGN PATTERN Presented by: Kuyseng Chheoun 24-Aug-2014

Upload: kuy-seng-chheoun

Post on 03-Jul-2015

206 views

Category:

Software


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Mediator Design Pattern

MEDIATOR DESIGN PATTERN

Presented by:

Kuyseng Chheoun24-Aug-2014

Page 2: Mediator Design Pattern

MEDIATOR DESIGN

Page 3: Mediator Design Pattern

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

Page 4: Mediator Design Pattern

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.

Page 5: Mediator Design Pattern

Structure

Page 6: Mediator Design Pattern

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.

Page 7: Mediator Design Pattern

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

Page 8: Mediator Design Pattern

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.

Page 9: Mediator Design Pattern

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.

Page 10: Mediator Design Pattern

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!

Page 11: Mediator Design Pattern

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!

Page 12: Mediator Design Pattern

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

Page 13: Mediator Design Pattern

ありがとう