|Object COBOL Tutorials||Writing a Class Program Tutorial|
This tutorial is a gentle introduction to Object 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:
Time to complete: 20 minutes.
This section introduces you to the Stopwatch class, describing its behavior and public object interface. The object interface is the list of methods implemented by an object, together with descriptions of the parameters passed to and from the object by each method.
The public object interface is a censored list of this information; it does not describe the object's private methods. A private method is one used by the object itself; it should not be invoked from outside the object.
The interface for the Stopwatch is split in two:
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 public object interface is listed below.
||Returns an instance of the Stopwatch class, with the handle in
||Returns the number of Stopwatch instances created so far.|
||Start a stopwatch running.|
||Stop a stopwatch running.|
||Reset the elapsed time on the stopwatch to zero. You can only reset a stopwatch which isn't running.|
||Return 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 class methods of Base, and its instances inherit all the instance methods of Base. This means that Stopwatch responds to all the messages given in the Base public interface as well as its own. Inheritance is explained in more detail in the Inheritance Tutorial.
The Base class is part of the class library supplied with Object COBOL. If you want to look at the Base public interface, it is in your Class Library Reference.
You can now move on to the next session, which uses Animator 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 Object COBOL class; it is a piece of procedural code which uses Object COBOL objects. To communicate with the objects timer.cbl uses the following Object 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 animate timer to see some simple object behavior. The code demonstrates the following points:
To animate timer:
This compiles timer.cbl and stopwtch.cbl.
Animator starts with the statement below tag T010 highlighted ready for execution.
invoke StopWatch "new" ...).
This sends the "new" message to the Stopwatch class, which creates an instance of itself, returning an object handle to it in wsStopWatch1.
wsStopwatch1with the Animator Query function.
This data item is declared with usage
object reference, which
is a new COBOL datatype introduced for Object COBOL programming. It now holds a
handle (a four byte reference) to the object you have created.
When programming in Object 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.
invoke wsStopWatch1 "start").
The stopwatch starts timing.
The shell script you just ran did not compile Stopwatch for animation, so you won't see the Stopwatch code executing. In this tutorial we are concentrating on how to use objects, rather than on how to construct them.
invoke Stopwatch "new"...).
This creates a new instance of the Stopwatch class, and puts the object
wsStopwatch2. Each of the two stopwatches you created
has the same behavior, but contains different data.
invoke Stopwatch "howMany").
"howMany" is a class method which enables you to query the class data of Stopwatch. Use the Animator to query the value of wsCount. It contains 2, the number of instances created so far.
invoke wsStopwatch2 "start").
The second stopwatch starts running.
invoke wsStopwatch1 "getTimeFormatted"...).
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. Press F2=view to see the console, press any key to return to the Animator view.
invoke wsStopwatch1 "stop").
The stopwatches stop running.
This code fetches the elapsed time between the "start" and "stop" messages for each stopwatch.
invoke wsStopwatch1 "finalize"...).
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 it has been 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 © 1999 MERANT International Limited. All rights reserved.
This document and the proprietary marks and names used herein are protected by international law.
|Object COBOL Tutorials||Writing a Class Program Tutorial|