Question of the Week No. 59
Researched by Rama Roberts
Question of the Week presents answers to key questions posed by the
developer community. The intent is to pass this important, but not
always easy-to-find, information on to JavaTM
Developer ConnectionSM (JDC) members. The
questions are selected from the JDC newsgroups generally because: they are
frequently asked, they are significant or timely, or their answers are not
easily accessible.
Note: If you have a question to which you need an answer, try the JDC newsgroups. You can read through
the existing newsgroups, or with your
free JDC membership, you can post
new messages or threads.
Topic: How do I signal another object?
Question: Can anyone point me in the right
direction when it comes to event-handling and signaling. I have two
objects (non-AWT) and one needs to 'register an interest' in a
particular event that the other one is
signaling. The object that is 'listening' for the event from
the second object has a handle to it, but I'm a little
unsure how the second one 'signals' and what the first one
should do in order to handle the event.
Can anyone help or point to some useful documentation?
Thanks
Dave
Answer:
There are a few models to explore. The Observer/Observable pattern may be appropriate in
the situation where you have need
to update a set of objects based on a change in
another.
At least two objects are needed, one which extends
the Observable class and represents the changeable 'data' or 'model' and
one that implements the Observer interface and is
the class whose interest is registered with the Observable.
The following is a short demo - the only code of
real import is the implementation of the update() method by the Observer, and
the use of the three methods
addObserver(),setChanged(), and notifyObservers()
[the minimum that must be used] by the
Observable. A change in the state of a String in
the Observable will notify the Observer, which will in turn output a message to
that effect to the console:
import java.util.*;
import java.io.*;
public class Actor extends Observable {
private String stateString = "idle";
public Actor() {
addObserver(new Watcher());
}
public String getState() {
return stateString;
}
public void setState(String s) {
if( ! (s.equals(stateString) ) ) {
setChanged();
notifyObservers("State changed");
}
stateString = s;
}
public static void main(String[] args) {
String s;
Actor actor = new Actor();
BufferedReader reader =
new BufferedReader(
new InputStreamReader(System.in));
try {
System.out.println("Enter a state (" +
actor.getState(
) + " is the current state): ");
while((s = reader.readLine()) != null) {
actor.setState(s);
System.out.println("Current state: " +
actor.getState());
}
}
catch(Exception e) {
System.out.println("Program terminated.");
}
}
}
class Watcher implements Observer {
public void update(Observable o, Object arg) {
System.out.println("Observable object " +
o + " has changed state.");
}
}
Many thanks to JDC member xhunterx for contributing to
this answer.
ct 16 02:37:17 PDT 1999
[Member not logged in]
Is there any way to cast a Thread returned by an enumerate
from the class ThreadGroup in order to make full use of
polymorphism. I get a runtime error if I do that.
Sat Oct 16 17:29:54 PDT 1999
MChaston
One thing to watch out for in this case is the update sequence.
If the observer reads the 'stateString' on notification, because
it is updated after the notification message, it would be wrong.
It is normally safer to update the system before firing change
notifications.
You may also want to look at the java.awt.EventListener
interface and associated models (see Java Tutorials).
Sun Oct 17 06:23:41 PDT 1999
tenne
hjkjhkjhkj
Sun Oct 17 17:14:29 PDT 1999
[Member not logged in]
question for the for loop program; how do I promt for a
number of value;calculate the maximun input and the average
input
the interger number is 5;(loop 5times);
for loop=20times what is the right code for the program loop?
"THE INFORMATION PROVIDED IN THE JAVA DEVELOPER'S
CONNECTION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
LIMITATION THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR, AND NON-INFRINGEMENT.
THIS INFORMATION IS PROVIDED AT NO CHARGE. BY USE OF THE
INFORMATION YOU AGREE THAT NEITHER SUN MICROSYSTEMS,
INC. NOR ITS LICENSORS WILL BE LIABLE FOR ANY DAMAGES
WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL,
CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL
DAMAGES, EVEN IF SUN OR ITS LICENSORS HAVE BEEN ADVISED OF
THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT
ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR
CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING
THIS INFORMATION COULD INCLUDE TECHNICAL INACCURACIES
OR TYPOGRAPHICAL ERRORS. CHANGES ARE PERIODICALLY
ADDED TO THE INFORMATION HEREIN. SUN MICROSYSTEMS, INC.
MAY MAKE IMPROVEMENTS AND OTHER CHANGES IN THE
PRODUCTS AND THE PROGRAMS DESCRIBED HEREIN AT ANY TIME
WITHOUT NOTICE."