component based software engineering
Download
Skip this Video
Download Presentation
Component-Based Software Engineering

Loading in 2 Seconds...

play fullscreen
1 / 21

Component-Based Software Engineering - PowerPoint PPT Presentation


  • 92 Views
  • Uploaded on

Component-Based Software Engineering. Using the Java Event Model Paul Krause. 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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Component-Based Software Engineering' - arin


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
component based software engineering

Component-Based Software Engineering

Using the Java Event Model

Paul Krause

using the java event model
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

Register Event Listener

Fire Event

Java Event Model

Event Source

Event

Object

Event Listener

chili pizzaexpress

«interface»

OrderListener

EventObject

Bakery

source

pizzaStatus(evt)

getSource()

toString()

addOrderListener()

removeOrderListener()

sendMessage(PizzaEvent)

fires

PizzaEvent

passed to

Customer

0..*

registers with

invokes notifications in

0..*

iNumber

iSliceNumber

run( )

Chili PizzaExpress
pizzaevent java

EventObject

source

getSource()

toString()

PizzaEvent

PizzaEvent.java

import java.util.EventObject;

public class PizzaEvent extends EventObject {

public PizzaEvent(Object aSource) {

super(aSource);

}

}

orderlistener java

OrderListener

pizzaStatus

OrderListener.java

import java.util.EventListener;

public interface OrderListener extends

EventListener {

public void pizzaStatus(PizzaEvent anEvent);

}

EventListener

bakery java

Bakery

addOrderListener

removeOrderListener

sendMessage(PizzaEvent)

Bakery.java

public class Bakery

public Bakery( ) {

// constructor of a Bakery instance

}

public addOrderListener( eL ) {

// inserts OrderListeners in some

// structure

}

public removeOrderListener( eL ) {

// deletes OrderListeners from that

// structure

}

private void sendMessage( evt ) {

// broadcast evt somehow

}

properties of bakery java
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
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
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
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
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
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

«interface»

OrderListener

EventObject

Bakery

source

pizzaStatus(evt)

getSource()

toString()

addOrderListener()

removeOrderListener()

sendMessage(PizzaEvent)

fires

PizzaEvent

passed to

Customer

0..*

registers with

invokes notifications in

0..*

iNumber

iSliceNumber

run( )

The Story so Far
the customer class
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
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
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
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
Warning
  • These slides have simplified the implementation a little bit
  • We have missed out:
    • Explicit type conversions;
    • “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
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
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
ad