Event Handling in Java: Alternatives and Patterns Raja Sooriamurthi www.kelley.iu.edu/raja Information Systems Department Kelley School of Business Indiana.

Slides:



Advertisements
Similar presentations
Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
Advertisements

Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
Written by: Dr. JJ Shepherd
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
Button click handlers Maarten Pennings. Introduction An activity has one or more views – view is also known as widget or control – examples include Button,
Design Patterns 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Giuseppe Attardi Antonio Cisternino
Writing GUI Applications An introduction with Java.
Chapter 3.4 Programming Fundamentals. 2 Data Structures Arrays – Elements are adjacent in memory (great cache consistency) – They never grow or get reallocated.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Io package as Java’s basic I/O system continue’d.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Object Oriented Programming, Interfaces, Callbacks Delegates and Events Dr. Mike Spann
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 11: Oct 31-Nov 4, 2011 Aditya Mathur Department of Computer Science Purdue.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 3 The Structure and Syntax of Java.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Computer Science Standard Level Mastery Aspects. Mastery Item Claimed JustificationWhere Listed Arrays Used to store the student data Lines P.
Inheritance & Interfaces. The Plan ● Motivate Inheritance ● Motivate Interfaces ● Examples ● Continue Practice on Observer/Observable.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Polymorphism Giuseppe Attardi Antonio Cisternino.
FLTK Help Session By Richard Yu Gu CS 638 -Graphics Fall, 1999.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
3-1 State Design Pattern C Sc 335 Rick Mercer. State Design Pattern State is one of the Behavioral patterns It is similar to Strategy Allows an object.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Applications Development
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Session 18 Chapter 8: Understanding Inheritance. Recall Exercise 2 From Tuesday It’s very annoying to move a target from the pallet and drop it in the.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Object Oriented Programming.  Interface  Event Handling.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
OBSERVER DESIGN PATTERN. Behavioral Patterns  Behavioral patterns are those patterns that are most specifically concerned with communication between.
Salman Marvasti Sharif University of Technology Winter 2015.
Event Handling. Event Issues Event Dispatch – Bottom-up – Top-Down Windowing system must handle events for any application Windowing system may or may.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
A Introduction to Computing II Lecture 4: Visual Programming A Case Study of OOP Fall Session 2000.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Written by: Dr. JJ Shepherd
Object-Oriented Programming Fundamentals. Comp 241, Fall Example Colored points on the screen What data goes into making one? Coordinates Color.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Events. Slide 2©SoftMoore Consulting Events Events are generated when a user interacts with the view objects of an application. Examples –button clicked–
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Object Oriented Programming Lecture 2: BallWorld.
CompSci Inheritance & Interfaces. CompSci Inheritance & Interfaces The Plan  Motivate Inheritance  Motivate Interfaces  Examples  Continue.
Behavior trees & The C# programming language for Java programmers
Observer Pattern Context:
GUIs Model/View/Controller Pattern Using BreezySwing
Composite Design Pattern
Java Programming: From Problem Analysis to Program Design,
CS 153: Concepts of Compiler Design November 30 Class Meeting
null, true, and false are also reserved.
Introduction to Behavioral Patterns (1)
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Objects First with Java A Practical Introduction using BlueJ
Constructors, GUI’s(Using Swing) and ActionListner
Objects First with Java A Practical Introduction using BlueJ
CSC 111 Exam 3 Review Fall 2005.
Advanced ProgramMING Practices
Chapter 15 Event-Driven Programming and Animations Part 1
Presentation transcript:

Event Handling in Java: Alternatives and Patterns Raja Sooriamurthi Information Systems Department Kelley School of Business Indiana University, Bloomington Portions of this talk are from my presentation Prelude to the Java Event Model given at the 6th annual conference on Innovation and Technology in Computer Science Education (ITiCSE), June 2001, Canterbury, UK.

Concepts Event handling Event handling models Inheritance vs delegation based models Notion of first class values Use of interfaces Callback methods Observable / Observer pattern Learning is finding out what you already know. --- Richard Bach

Event Handling event source event listener event object event listener event listener

Event Handling Models Giant Case Statement MS Windows, X-Windows Object Oriented Models –Inheritance based MFC, Motif, Java 1.0 –Delegation based Java 1.1

Giant Case Statement case event of KEY_PRESS: action_1 MOUSE_CLICK: action_2 … end Extension => need to edit code No hierarchy of events Problems:

Anatomy of a Button Object label color handle code

Inheritance Based Event Model (Java 1.0) class Button { // other state information and behavior //... text //... color //... parent widget boolean handleEvent() { System.out.println( "button has been clicked" ) ; return true; } class Start extends Button { boolean handleEvent() { System.out.print( "Start... " ) ; return super.handleEvent(); } Button Start

Restrictions of the 1.0 Model Can not change the behavior of the start button at run-time. The inheritance chain is static. Computationally expensive. The action is being associated with the button. Should be associated with the application. Lots of classes needed.

Delegation Based Event Handling (Java 1.1 +) First Class Values Callback functions Key Concepts:

First Class Values store in a variable pass to a function return from a function Can … ? f

Examples of First Class Values Pascal C Lisp Scheme, Ruby Java scalar types (integer, real, char, boolean) arrays and records are not structs functions continuations objects methods are not first class (research systems like GJ and Pizza )

A Simple Example // count the positive numbers in an array class F { static int countPositive( int[] a ) { int cnt = 0; for (int i=0; i < a.length; i++) if ( a[i] > 0 ) cnt ++; return cnt; } Usage: F.countPositive( x ) X =

The Flexibility of First Class Objects class F { static int countIf( int[] a, Predicate pred ) { int cnt = 0; for (int i=0; i < a.length; i++) if (pred.test( a[i] )) cnt ++; return cnt; } class Predicate { public boolean test( int n ) { return( n > 0 ); } Usage: F.countIf(b, new Predicate() );

Abstracting Once More class F { static int countIf( int[] a, Predicate pred ) { int cnt = 0; for (int i=0; i < a.length; i++) if (pred.test( a[i]) ) cnt ++; return cnt; } class Predicate { public boolean test( int n ) { ? } } class IsPositive extends Predicate { public boolean test( int n ) { return( n > 0 ); }

interface Predicate { boolean test( int n ); } class IsPositive implements Predicate { public boolean test( int n ) { return( n > 0 ); } Better Design with an Interface Predicate IsPositive IsPrime …

Use of Anonymous Classes int[] a = { -4, -2, 0, 1, 3, 5, 7 }; int n1, n2; n1 = F.countIf( a, new IsPositive() ); // testing for -ve numbers with an anonymous class n2 = F.countIf( a, new Predicate() { public boolean test( int n ) { return( n<0 ); } );

Event Handling with Callbacks Observable Observer 1.Observers register themselves with each observable item they are interested in 2.Observables inform each interested observer when a relevant change occurs

Callback Methods investor = new Investor( "Bob" ); // each investor defines a call back method void on_change( Stock stk ) { System.out.println( "Price of " + stk ); if (stk.price < 10.00) { System.out.println ("... falls below sell"); }

stk = new Stock( "ABC", ); // register an investor stk.add_watch( investor ); // when a stock price changes... stk.percentChange( ); // falls by 10.5 % //... the stock notifies the interested investors void percentChange( double change ) { price += price * (change/100.0); notifyInvestors(); } // the book keeping code is simple private Vector handlers = new Vector(); void add_watch( Investor p ){ handlers.add( p ); } void notifyInvestors() { for( int i=0; i<handlers.size(); i++ ) ((Investor) handlers.get(i)).on_change( this ); } Callback Methods (contd.)

class Stock extends Observable {... void percentChange (double change) { price += price*(change/100.0); setChanged(); notifyObservers(); } class Investor implements Observer {... public void update (Observable s, Object ignore){ Stock stk = (Stock) s;... } stk.addObserver (... ) Observable / Observer Design Pattern

Summary Ubiquity of event driven programming Models of event handling –Big switch statement –Inheritance based –Delegation based Some design patterns