This tutorial is a gentle introduction to OO COBOL programming, using a class with some simple behavior, called the Stopwatch class. In this tutorial you will create instances of the Stopwatch class. An instance of the Stopwatch class is a stopwatch object, and having created stopwatches, you can send messages to them.
This tutorial consists of the following sessions:
This section introduces you to the Stopwatch class, describing its behavior.
The Stopwatch class has two parts:
A class is an object in its own right; it can have behavior and data of its own, which is different to the behavior and data of instances of the class. The main function of most classes is to implement the behavior needed to create and initialize new instances.
You can use stopwatches to time things. Each stopwatch has methods to start, stop and reset timing, and to get the current elapsed time.
The Stopwatch class factory and instance methods are listed below.
| Factory methods | Description |
|---|---|
"new" returning anObject |
Returns an instance of the Stopwatch class, with the handle in anObject |
"howMany" returning aNumber |
Returns the number of Stopwatch instances created so far. |
| Instance methods | Description |
|---|---|
"start" |
Starts a stopwatch running. |
"stop" |
Stops a stopwatch running. |
"reset" |
Resets the elapsed time on the stopwatch to zero. You can only reset a stopwatch that isn't running. |
"getTimeFormatted" |
Returns the time since the stopwatch started in hh:mm:ss:ss format (hours:minutes:seconds:hundredths of seconds). |
In addition to these methods, the Stopwatch class inherits all those implemented by its superclass (Base). The superclass is the parent from which this class inherits its basic behavior.
The Stopwatch class inherits all the factory methods of Base, and its instances inherit all the instance methods of Base. This means that Stopwatch responds to all the messages that Base responds to as well as its own. Inheritance is explained in more detail in the section the section Inheritance in the chapter Classes.
The Base class is part of the class library supplied with your COBOL system. If you want to look at the methods of Base see your Class Library Reference.
You can now move on to the next session, which uses the Debugger to show you message sending.
In this section you will create instances of the Stopwatch class, and send messages to the instances, using the supplied program, timer.cbl.
Program timer.cbl is not an OO COBOL class; it is a piece of procedural code which uses OO COBOL objects. To communicate with the objects timer.cbl uses the following OO COBOL syntax:
Declares the classes used by the programs, and links them to their filenames.
Declares a variable to hold object handles.
Sends a message to an object.
You will now debug timer to see some simple object behavior. The code demonstrates the following points:
To debug timer:
This compiles timer.cbl and stopwtch.cbl.
The Debugger starts with the statement below tag T010 highlighted ready for execution.
This sends the "new" message to the Stopwatch class, which creates an instance of itself, returning an object handle to it in wsStopWatch1.
The Examine List window opens, displaying the data item. It is declared with usage "object reference", which is a COBOL data type introduced for OO COBOL programming..The class name Stopwatch indicates that this data item can be used only to hold an object reference to a Stopwatch object. It now holds a handle (a four-byte reference) to the object you have created.
When you write in OO COBOL, you use the object handle as an address to send messages to the object. You can pass object handles between different programs or objects as parameters, in effect enabling you to pass an object between different parts of an application.
The stopwatch starts timing.
This creates a new instance of the Stopwatch class, and puts the object handle into wsStopwatch2. Each of the two stopwatches you created has the same behavior, but contains different data.
"howMany" is a factory method which enables you to query the factory data of Stopwatch. Double-click wsCount to view the value of wsCount. It contains 2, the number of instances created so far.
The second stopwatch starts running.
You are now querying the time the first stopwatch has been running, and it returns the time which has elapsed since you started it. The other statements display the value on the console. Click View > Debug Windows > Application Output to see the console. (You can dock this window: right-click on the window, click Allow Docking and drag the window to the bottom right-hand corner of the IDE).
The stopwatches stop running.
This code fetches and displays the elapsed time that each stopwatch was running.
The "finalize" method destroys an object, deallocating the memory it uses. The "finalize" method always returns a null object handle, which you can use (as here) to set the object reference which held the handle to null.
You must always "finalize" objects when you have finished with them, to avoid wasting system resources.
This completes the tutorial on creating objects and sending messages. You have seen:
At this point you might be thinking that there is nothing special about objects, and you could have done exactly the same thing using a few subroutines. This is true, but think about what this would involve.
First you need to define a data structure for recording the start, stop and elapsed time for a stopwatch. Then timer1.cbl needs to declare this structure for each stopwatch it is going to use. When you want to start timing, you call the start subroutine, passing it one of these structures as a parameter. Similarly when you want to stop, or get the elapsed time.
Should you decide to alter the implementation of the stopwatch subroutines, you probably also need to change every place where you have declared stopwatch data.
Compare this with the situation using objects. You declare an object reference for each stopwatch, and having declared it, you simply send it messages to start, stop or give the elapsed time. You can alter the implementation as much as you want, providing you keep the interface for the messages the same.
All the implementation details are hidden from you, and each stopwatch you declare encapsulates its data and implementation into a single entity. The code for any client of a stopwatch is kept very simple, enabling you to concentrate on what you want to do with it, rather than on how to use it.
Copyright © 2009 Micro Focus (IP) Ltd. All rights reserved.