THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

throw new IllegalArgumentException();


UserState state = (UserState) whichState;
if (watching.loggedIn(state)) // user logged in
addUser(state); // add to my list
else
removeUser(state); // remove from list
}
// ...
}


Each Eye object watches a particular Users object. When a user logs in or out, Eye is notified because it
invoked the Users object's addObserver method with itself as the interested object. When update is
invoked, it checks the correctness of its parameters and then modifies its display depending on whether the
user in question has logged in or out.


The check for what happened with the UserState object is simple here. You could avoid it by passing an
object describing what happened and to whom instead of passing the UserState object itself. Such a design
makes it easier to add new actions without breaking existing code.


The Observer/Observable mechanism is a looser, more flexible analogue to the wait/notify
mechanism for threads described in "wait, notifyAll, and notify" on page 354. The thread mechanism ensures
that synchronized access protects you from undesired concurrency. The observation mechanism enables any
relationship to be built between two participants, whatever the threading model. Both patterns have producers
of information (Observable and the invoker of notify) and consumers of that information (Observer
and the invoker of wait), but each one fills a different need. Use wait/notify when you design a
thread-based mechanism, and use Observer/Observable when you need something more general.


Exercise 22.4: Provide an implementation of the Attributed interface that uses
Observer/Observable to notify observers of changes.


22.4. Random


The Random class creates objects that manage independent sequences of pseudorandom numbers. If you
don't care what the sequence is and want it as a sequence of double values, use the method
java.lang.Math.random, which creates a single Random object the first time it is invoked and returns
pseudorandom numbers from that objectsee Section 22.9 on page 657. You can gain more control over the
sequence (for example, the ability to set the seed) by creating a Random object and getting values from it.


publicRandom()

Creates a new random number generator. Its seed will be initialized to a value
based on the current time.

publicRandom(long seed)

Creates a new random number generator using the specified seed. Two
Random objects created with the same initial seed will return the same
sequence of pseudorandom numbers.

public voidsetSeed(long seed)
Free download pdf