Chapter 8: Objects and Messages Tutorial

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:

  1. The Stopwatch class
  2. Sending messages

The Stopwatch Class

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 Animator to show you message sending.

Sending Messages

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:

You will now animate timer to see some simple object behavior. The code demonstrates the following points:

To animate timer:

  1. Use Infomgr to locate the required demonstration programs and transfer them to a working directory.
  2. Run the supplied shell script timrscrp.

    This compiles timer.cbl and stopwtch.cbl.

  3. Start animating timer using the following command line:
    anim timer

    Animator starts with the statement below tag T010 highlighted ready for execution.

  4. Step the statement ("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.

  5. Look at the contents of data item "wsStopwatch1" with the Animator Query function.

    This data item 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.

  6. Close the Animator monitor.
  7. Step the statement below tag T020 ("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.

  8. Step the line below tag T030 ("invoke Stopwatch "new"...").

    This creates a new instance of the Stopwatch class, and puts the object handle into "wsStopwatch"2. Each of the two stopwatches you created has the same behavior, but contains different data.

  9. Step the statement below tag T040 ("invoke Stopwatch "howMany"").

    "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.

  10. Step the statement below tag T050 ("invoke wsStopwatch2 "start"").

    The second stopwatch starts running.

  11. Step the statements below tag T060 ("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.

  12. Step the statements below tag T070 ("invoke wsStopwatch1 "stop"").

    The stopwatches stop running.

  13. Step the statements below tag T080.

    This code fetches the elapsed time between the "start" and "stop" messages for each stopwatch.

  14. Step the statements below tag T090 ("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.

  15. Press Escape to stop the Animator.

Summary

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 © 2006 Micro Focus (IP) Ltd. All rights reserved.