Building the Simulator
Let’s start building the classes that make up the simulator. We
start with the event classes first since they have no dependencies on
other classes in the system. After that, we’ll develop the
interfaces used to listen for the various events. We’ll finish
up with the objects that throw and catch the events in the system.
All of the classes will be part of the
BeansBook.Simulator
package.
Temperature Pulse Events
A temperature pulse event is used to coax a temperature object to
alter its value. The pulse contains the temperature value that the
source object would like the temperature object to synch up with.
First, let’s create a class and call it
TemperaturePulseEvent
. This is the event object
that will be passed as a parameter when the temperature pulse event
is fired. The code for the class looks like this:
package BeansBook.Simulator; // class definition for the temperature pulse event public class TemperaturePulseEvent extends java.util.EventObject { // the pulse temperature protected double theTemp; // constructor public TemperaturePulseEvent(Object source, double t) { // pass the source to the superclass super(source); // save the temperature theTemp = t; } // return the pulse temperature public double getPulseTemperature() { return theTemp; } }
As with all event objects, TemperaturePulseEvent
extends java.util.EventObject
. The variable
theTemp
stores the value of the temperature pulse.
The constructor takes two parameters, named source
and t
. source
is ...
Get Developing Java Beans now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.