Behavioral Patterns  «Prev  Next»

Time Observer Interface - Exercise

The Observer Pattern

Objective: Write a TimeObserver interface.
Define an interface (Java) or abstract class (C++) called TimeObserver that has the following structure:
TimeObserver class with a timeChanged method
The provided graphic shows a class diagram for a class named TimeObserver. Here is a description of the diagram:
  1. Class Name: The class is named TimeObserver, as indicated in the top section of the diagram.
  2. Method: The class contains one public method:
          
    +timeChanged(newTime: int): void
          
        
    • +: This symbol indicates that the method is public.
    • timeChanged: This is the name of the method.
    • newTime: int: This indicates that the method accepts a single parameter named newTime, which is of type int.
    • : void: This indicates that the method does not return any value (return type is void).
  3. Purpose: The diagram implies that the TimeObserver class is likely part of an Observer Design Pattern where it monitors or observes changes in a time value. When the time value changes, the timeChanged method is invoked with the updated time as its argument.
TimeObserver class with a timeChanged method

In the text area below, type your answer and click the Submit button.