Java Reference
In-Depth Information
with the name of the event they want to listen to. This method creates a new
instance of class
DSubscription
and inserts it into the list of observers. In
order to be registered as observers, remote clients must implement the
updateEvent()
method of interface
DObserver
.
Observable servers notify events to their remote observer clients accord-
ing to the push model by invoking method
notifyObservers()
of class
DActivity
.
This method looks for registered observers in its
DSubscription
list and
invokes their remote method
updateEvent()
.
Since
DActivity
objects can play the roles of both observable and observer,
we extend class
DActivity
in order provide a standard implementation of
method
updateEvent()
: it creates a temporary thread that invokes the
abstract method
update()
, which should be implemented in every subclass of
DActivity
.
Please note how important is to use a temporary thread in method
updateEvent()
. This approach decouples the remote observable service from
the actual execution of the observer's
update()
method. In fact, the
update()
method might be a complex and time-consuming procedure that must be
executed by the observer's thread of execution. If we did not follow this
approach, the
update()
method of every registered observer would be
executed by the observable service itself.
19.5.3
Implementation
The implementation of the class diagram depicted in Figure 19.7 reuses part
of the code written for Prototype 1 of Chapter 9 (Manufacturing work cell)
and for Prototype 1 of Chapter 11 (Car parking).
Class
DEvent
represents the generic event that is notified by a distributed
control module to its observers. The broadcaster control module is the
owner of the event and is identified by its URL.
Class
DSubscription
records the association between an event, an observer
and an observable. The observer's URL is used to obtain an RMI reference of
the observer control module.
Class
DActivity
is extended in order to implement the methods defined in
interface
DObserver
and interface
Observable
.
package
mmi;
import
java.io.*;
import
java.rmi.*;
public class
DEvent
implements
Serializable{
public
String name; // the name of the event
public
Object value; // the data associated with the event
public
String ownerURL;
// the process that activates this event
public
DEvent(String name, String ownerURL, Object value) {
this
.name
#
name;
this
.ownerURL
#
ownerURL;