Java Reference
In-Depth Information
clone.elementAt(i);
if(volume == 0)
{
current.muted(event);
}else if(difference > 0)
{
current.volumeDecreased(event);
}else if(difference < 0)
{
current.volumeIncreased(event);
}
}
}
public int getVolume()
{
return volume;
}
public void setStation(float s)
{
station = s;
}
public float getStation()
{
return station;
}
}
Let me make a few comments about the Radio bean class:
The Radio bean is a source of volume events because of the add,
remove, and get volume listener methods.
■■
The listeners are stored in a Vector, and the add() and remove() meth-
ods of the Vector class simplify adding and removing listeners.
■■
The volume events occur when the volume changes, which is within
the setVolume() method.
■■
Within setVolume(), the Vector of listeners is traversed, and each object
in the Vector is notified of the event by having a VolumeListener
method invoked on it.
■■
I cloned the Vector before notifying listeners to demonstrate a standard
trick in JavaBeans programming. Cloning the Vector is done for thread
safety. Notifying the listeners might take a while, and a listener might
remove itself from the Vector right before it is to be notified of a Vol-
umeEvent. Because the Vector is cloned, the listener removes itself from
the original Vector, but it still gets notification of the event because the
listener is still in the cloned Vector.
■■
Search WWH ::




Custom Search