1 / 139

Unit 8 Mediator

Unit 8 Mediator. Summary prepared by Kirk Scott. Design Patterns in Java Chapter 10 Mediator. Summary prepared by Kirk Scott. Introduction. A mediator is something that stands between one base class and another

sorena
Download Presentation

Unit 8 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. Unit 8Mediator Summary prepared by Kirk Scott

  2. Design Patterns in JavaChapter 10Mediator Summary prepared by Kirk Scott

  3. Introduction • A mediator is something that stands between one base class and another • In object terms, the mediator object stands between one or more instances of one or more base classes • Instances of the base classes interact with the mediator class rather than interacting with each other directly

  4. The implementation of mediators can differ depending on what the relationships between instances of the base classes are • This means that it could also be represented by a variety of UML diagrams

  5. The background for mediators can be expressed in terms of responsibility • The general rule is that objects are responsible for themselves • In other words, classes should contain some well-defined, limited, and coherent data set, along with the methods needed to work with the data • Classes, in general, should not be heavily reliant on objects of other classes to implement needed functionality

  6. On the other hand, recall the Builder pattern • It illustrated offloading construction responsibility/functionality into another class • Also recall the Proxy pattern • In it, the client dealt with the proxy (stand-in) rather than dealing directly with the base class

  7. The Mediator pattern can be viewed as offloading/concentrating responsibility/functionality into a separate class • The mediator becomes responsible for the relationships between classes

  8. Although conceptual in nature, the relationships are real aspects of the design • The have a life of their own • It turns out to make good design sense to have a separate class that is devoted to storing and maintaining the relationships

  9. Scenario • Consider this scenario: • In a group of objects, each one “is aware of” or has a reference to every other member of the group • First of all, note that this can be complex

  10. Also note that the set of relationships itself becomes an abstraction that might be implemented as a separate class • Introducing a new class factors the relationship functionality out of the base classes • The new class concentrates responsibility for the relationships among objects and reduces their responsibility for themselves

  11. The individual objects become dependent on the class that maintains the relationships • In the code, the individual objects relate only to the class in the middle, not with each other • This new class in the middle is a mediator class • The base objects are no longer tightly coupled with each other, but they are tightly coupled with the mediator

  12. Book Definition of the Pattern • Book definition: • The intent of the Mediator pattern is to define an object that encapsulates how a set of objects interact; this promotes loose coupling, keeping the objects from referring to one another explicitly, and lets you vary their interactions independently.

  13. Comment mode on: • The pattern makes the base classes more loosely coupled with each other. • In a sense, it does this by coupling them to a common class in the middle. • They are coupled to the class in the middle by virtue of the fact that the class in the middle concentrates the responsibility for maintaining the relationships

  14. Modeling Relationships • Keep in mind that code typically models some reality • In that reality, individual objects may be linked with each other • However, the relationships themselves truly are a separate concept • How you represent the relationships in code is a design choice • Correctly implementing the relationships in a separate class abstracts out the responsibility for maintaining relationships

  15. Concrete Examples from the Book • The book pursues two examples to illustrate the use of the Mediator design pattern • 1. The pattern can become apparent/be used when developing GUI’s with multiple parts in increasingly complex relationships • 2. The pattern can also become apparent when modeling the real world where objects have relationships with each other and the code needs to reflect those relationships

  16. The book will claim that the first example is the more common one • Before the dust has settled it should become apparent that the second example, even if less common, makes the logic of the pattern clearer

  17. A Classic Example: GUI Mediators • The book’s first example applies the mediator pattern to a program with a graphical user interface • The book shows a GUI, shows some code, and shows a refactoring using the pattern • At the end of the book’s example, it basically comes down to this: • “Voila—and now you see how these things don’t interact directly with each other anymore—they interact through this other class in the design.”

  18. In order to reduce the “voila” factor, it is helpful to give a preview of how it will fit together: • The example is based on tubs and machines • There will be a class that is responsible for the GUI components • This is the client • It graphically represents the objects in the application • It also has buttons, etc., so actions can be taken

  19. There will be a mediator • This will implement listening—the functionality for what happens when an action is taken in the GUI • There will be base classes for tub and machine objects, plus a base class which keeps track of these objects in the data model • In this case, the class which takes care of the objects will be known as a “mock database” (stay tuned)

  20. Inherently, a relationship between the data model and the graphical user interface representation of the state of the application has to be maintained • In this example, the mediator, a listener which stands between the GUI/client and the data model classes is responsible for maintaining the relationship

  21. The Scenario • The graphical user interface for the example is shown on the overhead following the next one • A factory has machines in it, and the machines at any given time have a set of tubs of materials that belong to them • The interaction of interest is the moving of tubs from one machine to another

  22. It is important to understand that at this point the book is interested in mediation between the graphical user interface and the data model in the application • It is apparent that there is another basic relationship in the application: • Which tubs are located at which machines • Applying mediation to that relationship will be covered in the next example

  23. In the interface, if you select a source machine from the first column, the tubs belonging to it appear in the second column • Then you pick a tub in the second column and a destination machine in the third column • Once three things are highlighted, you can press the “Do it!” button in order to move the tub from the source machine to the destination • Both the GUI representation and the underlying data model in the software should be updated

  24. Extending the Development Scenario • The authors paint out the scenario further in this way • An interface like this can be developed using a wizard • However, the wizard produces cookie-cutter code that doesn’t necessarily fit a nice design pattern

  25. Using a wizard might produce a class named MoveATubthat is basically a monolithic application • The UML for this class is given on the next overhead

  26. Refactoring the Design • The book observes that the methods in the MoveATub class are a mixture • The desire is to refactor this design so that it is better • In other words, the desire is to break it into parts where the responsibilities are clearly divided • In the long run this will make the code easier to maintain and modify

  27. GUI Construction Methods in the Monolithic Class • There are methods in MoveATub that construct GUI components • For example, there is a method assignButton(), which is the method which constructs the “Do it!” button • Its code is given on the next overhead for reference • Notice that it is implemented with lazy initialization

  28. private JButtonassignButton() • { • if(assignButton == null) • { • assignButton = new JButton(“Do it!”); • assignButton.setEnabled(false); • assignButton.addActionListener(this); • } • return assignButton; • }

  29. Domain Object Construction in the Monolithic Class • There are also methods in MoveATub that construct domain objects like lists of machines and tubs, and so on • In other words, the elements of the problem domain are mixed with GUI elements

  30. Listener Implementation in the Monolithic Class • MoveATub also implements the ActionListener and ListSelectionListener interfaces • In it you find methods that are related to actions, like valueChanged() and updateTubList() • The authors give the code for the valueChanged() method for reference • It is shown on the following overhead

  31. public void valueChanged(ListSelectionEvent e) • { • // … • assignButton().setEnabled( • !tubList().isSelectionEmpty() • && !machineList().isSelectionEmpty()); • }

  32. The book observes that at the very least you might consider moving the event handling methods into a different class • That would be a step towards applying the mediator pattern

  33. Ways of Implementing Listening • How listening is implemented opens out into a somewhat broader topic • Think back to how listening was implemented in CS 202 • The orange book’s plan was this: • Implement listening in a separate listener class, but make it an inner class so that it had direct access to the instance variables of the outer class

  34. What we’ve just seen in the monolithic MoveATub class is that the class itself can implement the listening interface • Among the points this book is making is that the monolithic class has too many different kinds of things in it • Classes should be coherent, single-purpose, and self-contained

  35. Where to put listening and how to make sure listeners have access to the things they need are important topics in Java GUI programming • Eventually, at the end of this course, this will lead to a discussion of the model-view-controller design pattern and the use of observers as well as listeners.

  36. Redesigning the Application Using the Pattern • Challenge 10.1 • Complete the diagram in Figure 10.3 to show a refactoring of MoveATub, introducing a separate mock database class and a mediator class to receive the events of the MoveATub GUI.

  37. Comment mode on: • As usual, doing this on your own verges on the impossible • What, for example, does the book mean by a mock database? • As usual, the only practical approach is to just look at the book’s solution and try and figure out what it means

  38. Solution 10.1 • Figure B.11 shows a solution.

  39. What is to be gleaned from the UML diagram of the solution? • MoveATub2 now is pretty much limited to the graphical components of the application • NameBase is the so-called mock database • This is the part of the application that is problem domain oriented • It keeps track of the state of machines and their tubs

  40. MoveATubMediator implements the ActionListener and ListSelectionListener interfaces • It contains the actionPerformed(), valueChanged(), and updateTubList() methods • As its name indicates, this is the (GUI) mediator • When things happen in the GUI, actions are taken indirectly on the NameBase, through the mediator

  41. It is also possible for the mediator to take actions on the GUI, like enabling or disabling a button • The authors say that the most common example of mediation is this use in GUI development • The mediator, in this example, stands between and implements the interaction between the GUI class/object and the NameBase class/object

  42. A simplified, generic UML diagram of the pattern would show the button and the NameBase, with the MoveATubMediator between them. • This is almost the end of the presentation of this example • I will not show additional code

  43. Just looking at the book’s UML diagram, some of the relationships and arrows are not completely clear • Also, as mentioned earlier, making sure the listeners have access to what they need is important, but unresolved • There is one last blip on this example, and then I’m moving on to the next example, which I actually find more informative

  44. A Sequence Diagram for the Pattern • You’ve seen the use of listeners already in CS 202. • What’s new is that you can now apply a pattern name to (at least part of) the role the listener class plays in the overall design • The following challenge gets at this idea by means of a sequence diagram rather than a static structure diagram • It shows a sequence of calls and returns from the GUI component, to the listener, to the underlying data structure

  45. Challenge 10.2 • “Draw a diagram that shows what happens when the user clicks the Do it! Button. • Show whichever objects you think are most important, and show the messages that pass between these objects.”

  46. Solution 10.2 • Figure B.12 shows one solution.

More Related