Saturday, November 10, 2012

Object oriented design for a digital watch:

Object oriented design for a digital watch:

A simplistic digital displays Hours of the day, minutes of the hour and the seconds of the minute. If you noticed a digital watch the seconds counts refreshes say twice a second and the keeps updating. When the seconds count is 60 seconds hand is reset and the minute count is incremented. Once the minute hand reaches a count 60 the minutes hand is reset and hours hand is incremented. Once the hours count reaches 24 the hours hand is reset to 0.


From the above description, the components of the system are:
1) WatchController
2) SecondsCounter,MinutesCounter,HoursCounter

Below is the class diagram representation:



We have an abstract Type TemporalCounter which defines the method getCounter() returns the current value of the counter which is common for all the counters.

The remaining methods of the class TemporalCounter are abstract and each sub class should define the behavior/declare itself as an abstract class. The reason they are abstract is each subtype has its own different version of increment. For e.g while incrementing seconds counter, once we reach 60 we have to reset the counter and call minutes hand to increment. Same applies for minutes hand increment except that once it reaches 60 it call hours hand increment.

The main controller class supports methods to start and stop the digital watch. Once started, displayTime is executed by the controller. The sub routine for displayTime should be like this

           public void displayTime() {

               while(!stop){
                    secsHand.increment();
                    hoursHand,display()+":"+minsHand.display()+":"+secsHand.display();
               }
            }

1 comment:

  1. Why is the controller having secondsHand,minutesHand - the question is for digital watch

    ReplyDelete