300 likes | 320 Views
Mediator. Kensho Tsuchihashi. Table of Contents. What is Mediator? What problem does Mediator solve? Advantage and Disadvantage Additional information Structure Example Code. 1. What is Mediator?. Mediator is one of the 23 Design Patterns which were selected by the GoF (Gang of Four).
E N D
Mediator Kensho Tsuchihashi
Table of Contents • What is Mediator? • What problem does Mediator solve? • Advantage and Disadvantage • Additional information • Structure • Example • Code Page 2
1. What is Mediator? Mediator is one of the 23 Design Patterns which were selected by the GoF (Gang of Four). Purpose Creation Structure Behavior Factory Method Scope Class Interpreter Template Interpreter Template Abstract Factory Builder Prototype Singleton Objects Adapter Bridge Composite Decorator Façade Flyweight Proxy Chain of Responsibility Command Iterator Mediator Memento Observer State Strategy Visitor Page 3
1. What is Mediator? Mediator - Somebody who tries to settle a dispute between other parties. In this context.. Mediator - Pattern that defines simplified communication between classes. Page 4
1. What is Mediator? More formally.. - Mediator defines an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from reffering to each other explicitly, and it lets you vary their interaction indepently. Page 5
2. What problem does Mediator solve? What if every objects communicate to each other one by one? Page 6
2. What problem does Mediator solve? Existence of Mediator makes things much simpler. Page 7
2. What problem does Mediator solve? • Encourages the distribution of behavior among objects. • As a result, an object structure tends to be many connections between objects; in the worst case, every object ends up knowing each other. (Spaghetti code) Motivation Object-Oriented Design Mediator • Is responsible for controlling and coordinating the interactions for a group from referring to each other explicitly. Page 8
2. What problem does Mediator solve? • A set of objects communicate in well-defined but complex ways. The resulting interdependencies are unstructured and difficult to understand. • Reuse 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. Applicability Use Mediator when Page 9
3. Advantage and Disadvantage • Mediator helps clarify how objects interact in a system. 3-1. Advantage 3-2. Disadvantage • Mediator pattern trades complexity of interaction for complexity in the mediator. • Mediator can become more complex than any individual colleague, which makes mediator itself hard to maintain. Page 10
4. Additional information Analogies • Conductor • Meeting scheduler • Airport control tower • Traffic controller • Stock Market Page 11
ConcreteColleague2 ConcreteColleague2 ConcreteMediator Colleague Mediator 5. Structure In UML +Mediator Page 12
6. Example Login Dialog • Select “Guest“ or “Login“. • When user login, input “username“ and “password“. • Press “OK“ or “Cancel“. .. Looks so simple Page 13
6. Example But what if with these requirements? • When “Guest“ is selected, “username“and “password“ should be disabled. • When “Login“ is selected, “username“ should be enabled. • When there is no character in “username“, “password“ should be disabled. • When user inputs any characters in “username“, “password“ should be enabled. • When there is any character both in “username“ and “password“, “OK“ should be enabled. If not, “OK“ should be disabled. (When “Guest“ is selected, “OK“ should be enabled.) .. pretty complicated! Page 14
See real program. Page 15
7. Code 7-1. List of classes Page 16
<<interface>> Mediator <<interface>> Colleague createColleague colleagueChanged setMediator setColleagueEnabled ColleagueButton ColleagueCheckbox ColleagueTextField Checkbox Frame Button TextField LoginFrame mediator mediator mediator checkGuest checkLogin textUser textPass buttonOk buttonCancel setMediator setColleagueEnabled setMediator setColleagueEnabled textValueChanged setMediator setColleagueEnabled itemStateChanged createColleague colleagueChanged userpassChanged actionPerformed 7. Code 7-2. Class Diagram Page 17
7. Code 7-3. Sequence Diagram : LoginFrame : ColleagueCheckbox : ColleagueTextField : ColleagueButton Page 18
7. Code 7-4. (1) Mediator public interface Mediator { public abstract void createColleagues(); public abstract void colleagueChanged(); } Page 19
7. Code 7-4. (2) Colleague public interface Colleague { public abstract void setMediator(Mediator mediator); public abstract void setColleagueEnabled(boolean enabled); } Page 20
7. Code 7-4. (3) ColleagueButton import java.awt.Button; public class ColleagueButton extends Button implements Colleague { private Mediator mediator; public ColleagueButton(String caption) { super(caption); } public void setMediator(Mediator mediator) { this.mediator = mediator; } public void setColleagueEnabled(boolean enabled) { // Mediator will tell you enabled/disabled setEnabled(enabled); } } Page 21
7. Code 7-4. (4) ColleagueTextField import java.awt.*; public class ColleagueTextField extends TextField implements TextListener, Colleague { private Mediator mediator; public ColleagueTextField(String text, int columns) { super(text, columns); } public void setMediator(Mediator mediator) { this.mediator = mediator; } public void setColleagueEnabled(boolean enabled) { // Mediator will tell you enabled/disabled setEnabled(enabled); setBackground(enabled ? Color.white : Color.lightGray); } public void textValueChanged(TextEvent e) { // If texfield changed, report to Mediator mediator.colleagueChanged(); } } Page 22
7. Code 7-4. (5) ColleagueCheckbox import java.awt.*; public class ColleagueCheckbox extends Checkbox implements ItemListener, Colleague { private Mediator mediator; public ColleagueCheckbox(String caption, CheckboxGroup group, boolean state) { super(caption, group, state); } public void setMediator(Mediator mediator) { this.mediator = mediator; } public void setColleagueEnabled(boolean enabled) { // Mediator will tell you enabled/disabled setEnabled(enabled); } public void itemStateChanged(ItemEvent e) { // If checkbox state changed, report to Mediatormediator.colleagueChanged(); } } Page 23
7. Code 7-4. (6) LoginFrame (1/5) import java.awt.Frame; import java.awt.Label; import java.awt.Color; import java.awt.CheckboxGroup; import java.awt.GridLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class LoginFrame extends Frame implements ActionListener, Mediator { private ColleagueCheckbox checkGuest; private ColleagueCheckbox checkLogin; private ColleagueTextField textUser; private ColleagueTextField textPass; private ColleagueButton buttonOk; private ColleagueButton buttonCancel; Page 24
7. Code 7-4. (6) LoginFrame (2/5) public LoginFrame(String title) { super(title); setBackground(Color.lightGray); setLayout(new GridLayout(4, 2)); // Creating grid 4 x 2 using layout manager createColleagues(); // Creating Colleagues add(checkGuest); // Layout from here add(checkLogin); add(new Label("Username:")); add(textUser); add(new Label("Password:")); add(textPass); add(buttonOk); add(buttonCancel); colleagueChanged(); // Initial setting for enabled/disabled pack(); show(); } Page 25
7. Code 7-4. (6) LoginFrame (3/5) public void createColleagues() { CheckboxGroup g = new CheckboxGroup(); checkGuest = new ColleagueCheckbox("Guest", g, true); checkLogin = new ColleagueCheckbox("Login", g, false); textUser = new ColleagueTextField("", 10); textPass = new ColleagueTextField("", 10); textPass.setEchoChar('*'); buttonOk = new ColleagueButton("OK"); buttonCancel = new ColleagueButton("Cancel"); // Setting Mediator here checkGuest.setMediator(this); checkLogin.setMediator(this); textUser.setMediator(this); textPass.setMediator(this); buttonOk.setMediator(this); buttonCancel.setMediator(this); Page 26
7. Code 7-4. (6) LoginFrame (4/5) checkGuest.addItemListener(checkGuest); checkLogin.addItemListener(checkLogin); textUser.addTextListener(textUser); textPass.addTextListener(textPass); buttonOk.addActionListener(this); buttonCancel.addActionListener(this); } // When Colleage notices their change, Mediator judges if each colleague should be enabled/disabled public void colleagueChanged() { if (checkGuest.getState()) { // Guest mode textUser.setColleagueEnabled(false); textPass.setColleagueEnabled(false); buttonOk.setColleagueEnabled(true); } else { // Login mode textUser.setColleagueEnabled(true); userpassChanged(); } } Page 27
7. Code 7-4. (6) LoginFrame (5/5) // textUser or textPass has been changed.Mediator judges if each colleague should be enabled/disabled private void userpassChanged() { if (textUser.getText().length() > 0) { textPass.setColleagueEnabled(true); if (textPass.getText().length() > 0) { buttonOk.setColleagueEnabled(true); } else { buttonOk.setColleagueEnabled(false); } } else { textPass.setColleagueEnabled(false); buttonOk.setColleagueEnabled(false); } } public void actionPerformed(ActionEvent e) { System.out.println(e.toString()); System.exit(0); } } Page 28
7. Code 7-4. (7) Main import java.awt.*; import java.awt.event.*; public class Main { static public void main(String args[]) { new LoginFrame("Mediator Sample"); } } Page 29
Sources • http://www.dofactory.com/Patterns/PatternMediator.aspx • http://sourcemaking.com/design_patterns/mediator • “Introduction to Design Pattern in Java” by Hiroshi Yuki • “Design Pattern” by GoF Page 30