1 / 13

Mediator

Mediator. 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.”

sshipman
Download Presentation

Mediator

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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) { show = d; } void book() { btnBook.setEnable(false); btnView.setEnabled(true) btnSearch.setEnabled(true); show.setText(“booking…”);

  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(); } } class BtnSearch extends JButton implements Command { Mediator med; BtnSearch(ActionListener al, Mediator m) { super("Search"); addActionListener(al); med = m; med.registerSearch(this); } public void execute() { med.search(); } } class BtnBook extends JButton implements Command { Mediator med; BtnBook (ActionListener al, Mediator m) { super("Book"); addActionListener(al); med = m; med.registerBook(this);

  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); } public void actionPerformed(ActionEvent ae) { Command comd = (Command)ae.getSource(); comd.execute(); } public static void main(String[] args) { new MediatorDemo(); } } java MediatorDemo

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

More Related