An Observer Example
Here is an ‘example that demonstrates an observable object. It creates an observer called Watcher, that implements the Observer interface. The class being monitored is called Being Watched, It extends Observable. Inside nightwatchmen is the method counter(), which simply counts down from a specified value. It uses sleep( )to wait a tenth of a second between counts Each time the count changes, notify Observers( ) is called with the current count passed as its argument. This causes the update( ) method inside Watch’ to be called, which displays the current count. Inside main(), a Watcher and a Being Watched object, called observing and observed, respectively, are created. Then, observing is added to the list of observers for observed. This means that observing update ) will be called each time counter ) calls notify Observers ).
More than one object can be an observer. For example, the following program implements two observing classes and adds on object of each class to the . BeingWatched observer list. The second observer waits until the count reaches zero and then rings the bell The Observable class and the Observer interface allow you to implement sophisticated program architectures based on the document/view methodology. They ‘are also useful in multithreaded situations
The java.utll.zip package
The java.utll.zip package provides the ability to read and write files in the popular ZIP and GZIP file formats. Both ZIP and GZIP input and output streams are available. Other classes implement the ZLIn algorithms for compression and decompression.
The java.util.jar package
The java.util.jar package provides the ability to read and write Java Archive GAR) files YOlI will see ill Chapter 25 that JAR files are used to contain software components known as Jnva Bemis and any associated files.