Simula 67, the first object-oriented programming language, was created to simplify computer simulations. Following the object-oriented approach, software developers begin modeling physical entities and intangible concepts by collecting their details in a class. The developers map the actions the modeled "thing" can do to the class's operations and implement them as functions in a programming language. They map the data or information the object manages to the class's attributes and implement them as member variables. Sometimes, it isn't clear if developers should choose a variable, a function, or both. We can write a correct program in many ways, so we choose the easiest way in cases like these.
In this example, we will model a stopwatch with two buttons and a display. The display, formed by the two hands and the numbers, displays the elapsed time between starting and stopping the watch. When pressed, the button on the side resets the display to 0. The top button performs two different operations depending on the watch's state: If stopped, the watch starts; if running, the watch stops. In general, the state of an object is the object's current condition or what the object is doing (e.g., stopped or running).
The Stopwatch Class
We begin by analyzing the problem. The stopwatch does something or performs some behavior when the user presses either of the buttons. Therefore, it's easy to model the two buttons with operations or functions. If the program had a graphical user interface (GUI), the display would always be visible, but with a console-based program, we must add an operation to display the elapsed time on demand. We implement the display with an attribute (variable) and an operation (function) combination.
The design phase follows and builds on the analysis. The Stopwatch class currently specifies the three features we identified in the "real world." As we design the program, we discover it needs two more member variables not identified during analysis and not present in the UML diagram. It's common for a program to require features (variables and functions) beyond those in the "real world." During the design phase, we add these features, called implementation features. We add a variable to store when the Stopwatch object begins running. The time difference between when the watch stops and starts will give us the elapsed time. We also add a flag to represent the watch's state: running or stopped.
(a)
(b)
The Stopwatch UML class diagram. The two UML class diagrams represent two phases of the software development process: analysis and design. (See also Class Development). The stime function models the button at the top of the stopwatch: pressing it once starts the watch running; pressing it again stops it. The reset function models the reset button to the right of the start/stop button; pressing it resets the time to zero. The display function displays the elapsed time.
A Stopwatch UML class diagram created during analysis. Each member in the class diagram corresponds to a feature identified in the problem domain, i.e., in a physical stopwatch.
A modified Stopwatch UML class diagram updated during design. The stopped and start_time member variables don't appear in the original "real world" problem, but they are necessary for a software implementation.
When creating class diagrams, software developers attempt to make them independent of any programming language or operating system. Nevertheless, some data types (e.g., bool) are specific to a limited number of programming languages, and others (e.g., timeb) are specific to a limited number of operating systems. Programmers must translate these types to match the implementing language and operating environment.
State Machines
UML class diagrams represent the static structure of an object-oriented program. They specify the data that instances of the class are responsible for maintaining and the operations they can perform. The diagrams form the skeleton or armature of an object-oriented program - every part of the program attaches to and builds on the diagrammed classes. Some classes also have dynamic behavior, which means that what they do next depends on what they have done in the past. The UML represents dynamic behavior with statechart diagrams based on rigidly specified state machines.
State machines often recognize data patterns or respond to events such as button presses. How we describe some parts of a state machine depends on its purpose. A basic state machine consists of just a few simple parts, each a finite set of elements:
States. Each state describes a condition the object can be in or an activity it can perform. The UML represents states as "round-tangles" (rectangles with rounded corners).
Transitions. Objects may change state by following transitions. The UML represents transitions as an arrow between states. So, a transition or state change is allowed in the direction of the arrow.
Events. Events, appearing as labels on transitions, trigger a transition or state change. When recognizing a pattern, an event is often the input of a single character. Events can also be a user pressing a button, a signal that a count-down timer has reached its end, an alarm indicating that a manufacturing process has reached a critical temperature, etc.
Actions. In a software system, actions are typically function calls. Actions also appear in transition labels following the '/' character.
Zero or more accepting or halt states. A halt state is where a state machine may end its dynamic behavior. If the machine performs a set of actions, the actions may cease in a halt state. If the machine searches for a pattern, the halt state signals that the machine recognizes and accepts the pattern.
System Calls
When a program needs a service that doesn't have a language-specific operation, it must use a system call. Such is the case with the stopwatch program, which needs timing information. We use a system call that returns the amount of time since the epoch, the beginning of time for the computer. For Unix, Linux, and macOS systems, the epoch is January 1, 1970; for Windows systems, the epoch is January 1, 1980. In practice, the program defines a structure with two integer fields: time and millitm. The first field is the number of whole seconds since the epoch, and the second is the number of milliseconds. The program passes the structure by-pointer in the system call, and the operating system fills the structure.
Structure
System Call
Windows
_timeb
_ftime_s
Unix Linux macOS
timeb
ftime
Getting the time since the epoch. The structures and the system calls needed to get the time since the epoch varies between operating systems, but the same #include directive incorporates them into a program:
#include <sys/timeb.h>
Elaborated Source Code
The third phase of the software design process is implementation or programming. In a "real world" situation, it's common for a customer to provide developers with a set of requirements to which they must adhere. The absence of formal requirements or a physical watch to model allows the developers to adopt two arbitrary behaviors. First, it may be difficult to reset the time if a mechanical watch is running. So, in that case, we'll choose to do nothing. Second, a mechanical watch continuously displays the elapsed time. We could duplicate that behavior with a GUI program, but not one based on the console. So, we only show the time when the watch is stopped. If a problem specifies a particular behavior, we must follow the requirement; otherwise, we can choose how to implement an operation. If there is a customer, we should get approval (in writing) for our choices.