Mediator - PowerPoint PPT Presentation

About This Presentation
Title:

Mediator

Description:

... a one-to-many dependency between objects so that when one object ... and 'subject' classes; mediator encapsulates communication between other objects. ... – PowerPoint PPT presentation

Number of Views:143
Avg rating:3.0/5.0
Slides: 14
Provided by: johnb248
Category:

less

Transcript and Presenter's Notes

Title: Mediator


1
Mediator
2
Law of Demeter
  • the methods of a class should not depend in
    any way on the structure of any class, except the
    immediate (top-level) structure of their own
    class. Further, each method should send messages
    to objects belonging to a very limited set of
    classes only.
  • OOAD, page 116

3
Why Mediator?
  • Common problem Multiple objects of same or
    different classes must communicate/interact.
    Multiple dependencies complicate objects, lead to
    spaghetti code.
  • Solution Define an object that encapsulates how
    a set of objects interact and interconnect. Make
    that object the hub of communications,
    responsible for controlling and coordinating the
    interactions of clients the colleague objects.

4
Analogies/Metaphors
  • Air traffic control
  • Stock market
  • eBay

5
Advantages/Uses
  • Partition system into smaller objects. Simplifies
    classes/objects by placing inter-object
    communications in mediator.
  • Promotes loose coupling.
  • Clarify complex relationships.
  • Limit subclasses.
  • Improve object reusability.
  • Simplify object protocols.
  • Facilitates refinement by containing changes in
    mediator or single colleagues rather than
    requiring updates in all classes.

6
Dangers
  • Mediator can become monolithic, violating
    proscription against God or manager classes and
    making it hard to maintain.

7
Sample Code
  • Interface Command
  • void execute()
  • Class Mediator
  • BtnView btnView
  • BtnSearch btnSearch
  • BtnBook btnBook
  • LblDisplay show
  • void registerView(BtnView v)
  • btnView v
  • )
  • void registerSearch(BtnSearch s)
  • btnSearch s
  • void registerBook (BtnBook b)
  • btnBook b
  • void registerDisplay(LblDisplay d)

8
continued
  • void search()
  • btnSearch.setEnabled(false
  • btnView.setEnabled(true)
  • btnBook.setEnabled(true)
  • show.setText("searching...")
  • class BtnView extends JButton implements Command
  • Mediator med
  • BtnView(ActionListener al, Mediator m)
  • super("View")
  • addActionListener(al)
  • med m
  • med.registerView(this)
  • public void execute()
  • med.view()

9
continued
  • super("Just start...")
  • med m
  • med.registerDisplay(this)
  • setFont(new Font("Arial",Font.BOLD,24))
  • class MediatorDemo extends JFrame implements
    ActionListener
  • Mediator med new Mediator()
  • MediatorDemo()
  • JPanel p new JPanel()
  • p.add(new BtnView(this,med))
  • p.add(new BtnBook(this,med))
  • p.add(new BtnSearch(this, med))
  • getContentPane().add(new LblDisplay(med),
    "North")
  • getContentPane().add(p, "South")
  • setSize(400,200)
  • setVisible(true)

10
UML
11
UML
12
Related Patterns
  • Façade provides a unified interface to a set of
    interfaces to make a subsystem easier to use, but
    Façade does not add functionality and is not
    known by the subsystem classes.
  • Observer defines a one-to-many dependency
    between objects so that when one object changes
    state all dependents are notified and updated
    automatically. Observer distributes communication
    by introducing observer and subject classes
    mediator encapsulates communication between other
    objects.

13
Sources
  • http//www.javacamp.org/designPattern/mediator.htm
    l
  • http//www.fluffycat.com//javasource/javapatterns/
    DvdLowercaseTitle.txt
  • http//www.vincehuston.org/dp/mediator.html
  • http//www.patterndepot.com/put/8/mediator.pdf
  • Design Pattern, GoF
  • OOAD, Booch et al
Write a Comment
User Comments (0)
About PowerShow.com