Behavioral Patterns  «Prev  Next»

Observed Object - Exercise

An Observed Object

Objective:Write a Time class as an Observed object.
Rewrite the Time class from Module three so that it adheres to both the Singleton and the Observed pattern. This class should simply count seconds for an amount of time given in the constructor. Each second, it should notify registered observers of the current time using their timeChanged() method. When you are finished, the class should look like this:

Time class with methods currentTime, endOfTime, getInstance, init, run(), addTimeObserver, removeTimeObserver
Time class with methods currentTime, endOfTime, getInstance, init, run(), addTimeObserver, removeTimeObserver

The run() method increments the currentTime, and notifies each registered listener of that change.
In the text area below, type your answer and click the Submit button.