Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Similar documents
Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Define an object that encapsulates how a set of objects interact.

! Statechart diagrams contain. ! States. ! Transitions. ! States : A,B,C, A is initial state. ! Labels : 0,1. ! Transitions:

Tecniche di Progettazione: Design Patterns

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Design Patterns. Produced by. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

Produced by. Design Patterns. MSc Computer Science. Eamonn de Leastar

Object Oriented Concepts. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

INTERNAL ASSESSMENT TEST III Answer Schema

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Design Patterns Design patterns advantages:

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

6 The MVC model. Main concepts to be covered. Pattern structure. Using design patterns. Design pattern: Observer. Observers

COSC 3351 Software Design. Design Patterns Behavioral Patterns (I)

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Chapter 1 GUI Applications

Jonathan Aldrich Charlie Garrod

Design Patterns #3. Reid Holmes. Material and some slide content from: - GoF Design Patterns Book - Head First Design Patterns

Midterm assessment - MAKEUP Fall 2010

An Example. Unified Modelling Language - 2 ITNP090 - Object Oriented Software Design. Class diagram. Class diagram. Dr Andrea Bracciali

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

1. What is Jav a? simple

CHAPTER 5 GENERAL OOP CONCEPTS

Using Design Patterns in Java Application Development

Produced by. Agile Software Development. Eamonn de Leastar

(Incomplete) History of GUIs

Swing from A to Z Some Simple Components. Preface

Produced by. App Development & Modelling. Eamonn de Leastar

Building Graphical User Interfaces. GUI Principles

Object-Oriented Design

Graphic User Interfaces. - GUI concepts - Swing - AWT

Introduction to concurrency and GUIs

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

The Factory Method Pattern

TDDB84 Design Patterns Lecture 06

CS193k, Stanford Handout #16

SampleApp.java. Page 1

Design Patterns V Structural Design Patterns, 2

State Application Using MVC

1005ICT Object Oriented Programming Lecture Notes

MIT AITI Swing Event Model Lecture 17

Design Patterns. Produced by. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Exceptions. Produced by. Algorithms. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

Produced by. Design Patterns. MSc Computer Science. Eamonn de Leastar

Answer on question #61311, Programming & Computer Science / Java

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

Produced by. Mobile Application Development. Eamonn de Leastar

Java. GUI building with the AWT

Java Swing Introduction

Cocoa Design Patterns. Erik M. Buck October 17, 2009

SOFTWARE PATTERNS. Joseph Bonello

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

Produced by. Mobile Application Development. Eamonn de Leastar

Graphical User Interfaces. Comp 152

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

Design Patterns. CSC207 Fall 2017

Interface Implementation

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Design Patterns. Gunnar Gotshalks A4-1

TTTK Program Design and Problem Solving Tutorial 3 (GUI & Event Handlings)

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Client-side GUI. A simple Swing-gui for searching for proudcts

Design Patterns Reid Holmes

Adapter Pattern Structural

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

Chris9an Kästner Charlie Garrod

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Beyond CSE143. What s Left To Do? Templates. Using Templates. A Template Class. A Problem with Reusing Code CSE 143

SDC Design patterns GoF

Graphical Interfaces

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

Design of Software Systems (Ontwerp van SoftwareSystemen) Design Patterns Reference. Roel Wuyts

Software Engineering

Swing - JLabel. Adding a text (and HTML) labels to a GUI

Example 3-1. Password Validation

CAS 703 Software Design

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Window Interfaces Using Swing Objects

Abstract, Interface, GUIs. Ch. 11 & 16

JAVA NOTES GRAPHICAL USER INTERFACES

Produced by. App Development & Modelling. BSc in Applied Computing. Eamonn de Leastar

Swing - JTextField. Adding a text field to the main window (with tooltips and all)

Transcription:

Design Patterns MSc in Computer Science Produced by Eamonn de Leastar (edeleastar@wit.ie)! Department of Computing, Maths & Physics Waterford Institute of Technology http://www.wit.ie http://elearning.wit.ie

Mediator Behavioural 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. 3

Motivation (1) Object-oriented design encourages the distribution of behavior among objects. Such distribution can result in an object structure with many connections between objects; in the worst case, every object ends up knowing about every other. Though partitioning a system into many objects generally enhances reusability, proliferating interconnections tend to reduce it again. 4

Motivation (2) Lots of interconnections make it less likely that an object can work without the support of others the system acts as though it were monolithic. Moreover, it can be difficult to change the system's behavior in any significant way, since behavior is distributed among many objects. As a result, you may be forced to define many subclasses to customize the system's behavior. 5

Motivation (3) (Holub example) The method ask() displays the small window. It accepts an answer and, if enter pressed, the window shuts down, and ask( ) returns the entered string. If the window is closed then ask(..) returns an empty string. public static void main(string[] args) SwingQuery query = new SwingQuery(); String result = query.ask("how are you?"); System.out.println(result); The code encapsulates a complex interaction with the GUI subsystem, but the user exercises all this complexity by doing a simple thing. The details are all hidden. Moreover, code that uses Query is now considerably simplified. 6

class SwingQuery implements Query public String ask(string question) final Object done = new Object(); final Object init = new Object(); final JFrame frame = new JFrame("Query"); final JTextField answer = new JTextField(); answer.setpreferredsize(new Dimension(200, 20)); frame.getcontentpane().setlayout(new FlowLayout()); frame.getcontentpane().add(answer); frame.getcontentpane().add(new JLabel(question)); answer.addactionlistener (new ActionListener() public void actionperformed(actionevent e) synchronized (init) synchronized (done) frame.dispose(); done.notify(); );... 7

frame.addwindowlistener (new WindowAdapter() public void windowclosing(windowevent e) synchronized (init) synchronized (done) frame.dispose(); answer.settext(""); done.notify(); ); synchronized (done) synchronized (init) frame.pack(); frame.setvisible(true); try done.wait(); catch (InterruptedException e) return answer.gettext(); 8

SwingQuery Structure SwingQuery hides ActionListener WindowAdapter JLabel JTextField + multi threading issues associated with Swing 9

Applicability 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. 10

Structure Class!! Object 11

Participants Mediator (Not used) defines an interface for communicating with Colleague objects. ConcreteMediator (SwingQuery) implements cooperative behavior by coordinating Colleague objects. knows and maintains its colleagues. Colleague classes Subsystem being mediated. (JTextField, JLabel, WindowAdapter, ActionListener) 12

Collaborations Colleagues send and receive requests from a Mediator object. The mediator implements the cooperative behavior by routing requests between the appropriate colleague(s). 13

Consequences (1) It limits subclassing. A mediator localizes behavior that otherwise would be distributed among several objects. Changing this behavior requires subclassing Mediator only; Colleague classes can be reused as is. It decouples colleagues. A mediator promotes loose coupling between colleagues. You can vary and reuse Colleague and Mediator classes independently. It simplifies object protocols. A mediator replaces many-to-many interactions with one-to-many interactions between the mediator and its colleagues. One-to-many relationships are easier to understand, maintain, and extend. 14

Consequences (2) It abstracts how objects cooperate. Making mediation an independent concept and encapsulating it in an object lets you focus on how objects interact apart from their individual behavior. That can help clarify how objects interact in a system. It centralizes control. The Mediator pattern trades complexity of interaction for complexity in the mediator. Because a mediator encapsulates protocols, it can become more complex than any individual colleague. This can make the mediator itself a monolith that's hard to maintain. 15

Except where otherwise noted, this content is licensed under a Creative Commons Attribution-NonCommercial 3.0 License.! For more information, please see http:// creativecommons.org/licenses/by-nc/3.0/