Object-Oriented Programming with Java Lecture 6 Using the Java Event Model.

Slides:



Advertisements
Similar presentations
13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
Advertisements

1 Lecture 18 Further Threading Overview  Case Study: Cooperating Threads  Producer/ Consumer model threads  Waiting for Synchronized Data  Busy Waiting.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Java Threads Part II. Lecture Objectives To understand the concepts of multithreading in Java To be able to develop simple multithreaded applications.
Object-Oriented Programming with Java The Java Event Model Lecture 5.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Threads Part II.
Object-Oriented Enterprise Application Development Lecture 8 Advanced JavaBeans.
Slides prepared by Rose Williams, Binghamton University ICS201 Lectures 18 : Threads King Fahd University of Petroleum & Minerals College of Computer Science.
Object-Oriented Programming with Java Lecture 2 The Java Event Model.
Internet Software Development The Java Event Model Lecture 5.
Multithreading.
Io package as Java’s basic I/O system continue’d.
PROG Mobile Java Application Development PROG Mobile Java Application Development Event Handling Creating Menus.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Internet Software Development More stuff on Threads Paul Krause.
Observer Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Component-Based Software Engineering Using Interfaces Paul Krause.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
University of Sunderland Java Threading, Mutex and Synchronisation Lecture 02 COMM86 Concurrent and Distributed Software Systems.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Nested References 2 inner reference data types Classes-Interfaces.
CS 350 – Software Design The Observer Pattern – Chapter 18 Let’s expand the case study to include new features: Sending a welcome letter to new customers.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
1 Web Based Programming Section 8 James King 12 August 2003.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
Copyright © 2002, Systems and Computer Engineering, Carleton University EventModel.ppt * Object-Oriented Software Development Part 17.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Introduction to Java Beans CIS 421 Web-based Java Programming.
UID – Event Handling and Listeners Boriana Koleva
IBM TSpaces Lab 3 Transactions Event Registration.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaBeans and.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Multithreading in JAVA
Java Thread and Memory Model
Advanced Java Session 4 New York University School of Continuing and Professional Studies.
© Wang Bin 2004 Java Threads. © Wang Bin 2004 In this lesson, you will learn to: u Define the concepts of threads and multithreading  Identify the functions.
Multi-Threading in Java
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things.
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, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
Lorenz: Visitor Beans: An Aspect-Oriented Pattern Aspect-oriented pattern: describes a solution to a tangling problem in a particular context.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Java Beans - Basics CIS 421 Web-based Java Programming.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Section 2.2 The StringLog ADT Specification. 2.2 The StringLog ADT Specification The primary responsibility of the StringLog ADT is to remember all the.
Chapter 32 JavaBeans and Bean Events
Chapter 36 JavaBeans and Bean Events
Chapter 8 Classes and Objects
Architectural Patterns for Interactive Software
CS360 Windows Programming
CNT 4007C Project 2 Good morning, everyone. In this class, we will have a brief look at the project 2. Project 2 is basically the same with project 1.
Android Topics Asynchronous Callsbacks
Component-Based Software Engineering
Constructors, GUI’s(Using Swing) and ActionListner
Component-Based Software Engineering
Pre-assessment Questions
Component-Based Software Engineering
Component-Based Software Engineering
Threads and Multithreading
CMSC 202 Threads.
CSC 243 – Java Programming, Fall, 2008
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Object-Oriented Programming with Java Lecture 6 Using the Java Event Model

Contents  Quick Review of the Java Event model  Using this model to notify Customers that Pizzas are ready from a Bakery  This example is taken from JavaBeans by Example, by Henri Jubin, Prentice Hall

Java Event Model Event Source Event Listener Register Event Listener Fire Event Event Object Event Object

Chili PizzaExpress EventObject source getSource() toString() Bakery addOrderListener() removeOrderListener() sendMessage(PizzaEvent) fires passed to registers with 0..* invokes notifications in0..* «interface» OrderListener pizzaStatus(evt) Customer run( ) iNumber iSliceNumber PizzaEvent

PizzaEvent.java import java.util.EventObject; public class PizzaEvent extends EventObject { public PizzaEvent(Object aSource) { public PizzaEvent(Object aSource) { super(aSource); super(aSource); }} EventObject source getSource() toString() PizzaEvent

OrderListener.java import java.util.EventListener; public interface OrderListener extends EventListener { public void pizzaStatus(PizzaEvent anEvent); public void pizzaStatus(PizzaEvent anEvent);} EventListener OrderListener pizzaStatus

Bakery.java public class Bakery public Bakery( ) { // constructor of a Bakery instance // constructor of a Bakery instance } public addOrderListener( eL ) { public addOrderListener( eL ) { // inserts OrderListeners in some // structure } public removeOrderListener( eL ) { public removeOrderListener( eL ) { // deletes OrderListeners from that // structure } private void sendMessage( evt ) { private void sendMessage( evt ) { // broadcast evt somehow } Bakery addOrderListener removeOrderListener sendMessage(PizzaEvent)

Properties of Bakery.java import java.lang.Thread; import java.util.*; public class Bakery implements Runnable { private Vector iCustomers = new Vector( ); private Thread iThread; // methods go here… }

Constructor for Bakery public Bakery ( ) { iThread = new Thread(this); iThread.start( ); } // When a new instance of Bakery is created, // a flow of control owned by it is started.

The main flow of control public void run( ) { while(true) { iThread.sleep(4000); PizzaEvent event = new PizzaEvent(this); sendMessage(event);}} // a Bakery broadcasts a message that Pizza is ready // every 4 seconds

Adding and removing Listeners public void addOrderListener(OrderListener aListener) { iCustomers.addElement(aListener);} // Remember iCustomers is a Vector field in Bakery public void removeOrderListener(OrderListener aListener) { iCustomers.removeElement(aListener);}

Broadcasting the message private void sendMessage(PizzaEvent anEvent) { Vector v; v = iCustomers.clone( ); for (int i = 0; i<v.size( ); i++) { OrderListener ol = v.elementAt(i); ol.pizzaStatus(anEvent); // implement in Customer } System.out.println(“Pizza ready …”); }

Summary for Sources  Record all the references to Listener Objects in a “Vector”  Register Listeners by adding their name to the Vector  Unregister Listeners by removing their name from the Vector  Step through the elements of the Vector to notify all the Listeners

The Story so Far EventObject source getSource() toString() Bakery addOrderListener() removeOrderListener() sendMessage(PizzaEvent) fires passed to registers with 0..* invokes notifications in0..* «interface» OrderListener pizzaStatus(evt) Customer run( ) iNumber iSliceNumber PizzaEvent

The Customer Class  A Customer also has its own flow of control public class Customer implements OrderListener, Runnable { private int iNumber; // identify customer private boolean iHaveSlice; // something to eat? private Thread iThread;// identifiy flow of control private Randon iRandom;// gaps between bites private int iSliceNumber;// Slices eaten … }

Construct a Customer public Customer(int aNumber) { iNumber = aNumber; iRandom = new Random(aNumber); iThread = new Thread(this); iThread.start( ); } // Construct a Customer with a specified identifier, and // start its own flow of control

Making your Customer Run public void run( ) { while(true) { if (iHaveSlice) { for (int bites=0; bites<4; bites++) { System.out.println(“customer: “ + iNumber + bites + “ slice:” + iSliceNumber); iThread.sleep(iRandom.nextFloat( ) * 3000); } iHaveSlice = false; iThread.suspend( ); } }// Takes 4 bites, with a rest between each, then }// waits for some more Pizza.

Response to PizzaEvents  Remember, we invoked a method called “pizzaStatus” when we broadcast messages from the Bakery. Customer must implement this: public void pizzaStatus(PizzaEvent anEvent) { if ( ! iHaveSlice) { iHaveSlice = true; iSliceNumber++; iThread.resume( ); }

Warning  These slides have simplified the implementation a little bit  We have missed out: Explicit type conversions; Explicit type conversions; “Synchronisation” of critical sections in threads “Synchronisation” of critical sections in threads  The full implementation can be found on the CSM-15 Web site  This is taken from JavaBeans by Examples, Henri Jubin, Prentice Hall

Running the Bakery public class TestApp { public static void main(String args[ ]) { TestApp t = new TestApp( ); } public TestApp( ) { Bakery b = new Bakery( ); Customer c1 = new Customer( 1 ); Customer c2 = new Customer( 2 ); b.addOrderListener( c1 ); b.addOrderListener( c2 ); }}

Summary  We have explored a simple example of a general Notifier-Observer design pattern  Everything in this example is available in the Java 2 Software Development Kit  The trick has been to use a design pattern that allows as many Observers (Customers, in our case) to be added as required