Design Patterns «Prev Next»

Singleton Classes for the Course Project

Objective: Write the Singleton classes for the course project in Java or C++.

Workflow note: This page collects your solution. After you submit, the next page will display exactly what you entered and provide any follow-up guidance for the project workflow.

For the simulation you will need a shared clock so that time is consistent across the different classes. This is not a real-time clock; it is a simulated clock. There must be only one instance so every class reads the same time.

  1. Create a Time (or Clock) class that is shared between the different classes. The class should have a single int field named currentTime initialized to 0. Provide a getter method that returns currentTime, but do not provide a setter.

    UML diagram for a Time class with currentTime, endOfTime, constructor, and run() method
    currentTime:int
    endOfTime:int
    +Time(endOfTime:int)
    +run():void
    Time class (UML)
  2. Add a field named endOfTime that stores the number of seconds the clock should run. Set endOfTime in the constructor.
  3. Provide a run() method that increments currentTime by 1 until it reaches endOfTime. In other words, run() counts from the initial time value up to the end of the simulation.

Write this Time (or Clock) class as a Singleton. In the text area below, type your answer and click Submit.