PreviousExtending a Class Debugging Object COBOL ApplicationsNext

Chapter 13: Requirements-based Vocabulary

Requirements-based vocabulary enables you to define new COBOL verbs and functions for use with the objects you have created. This chapter explains the syntax for defining these new verbs and functions.

13.1 Overview

As an alternative message-sending mechanism to the INVOKE verb, you can create your own new COBOL verbs and functions. For example, you could create a new verb, CREDIT, enabling you to write:

    credit aBankAccount with amount

To use this syntax in a program, you have to include a class interface definition at the start of your program or class. Interface definitions are declared through external classes. An external class has the structure of a class, and defines all of the class and instance methods of the class, but does not contain any of the code for the methods.

By convention, each external class is held in a .if file, and programs using vocabularies use COPY statements to include the class definition. The COPY statements must appear before the CLASS-ID header.

13.2 Using Requirements-based Vocabularies

There are three steps to using requirements-based vocabularies:

  1. Define a vocabulary for an object (or for a set of objects). The vocabulary for each object is defined in an external class.

  2. Include the external classes at the start of each program (or class) which invokes objects through a vocabulary.

  3. Use the verbs and functions defined by the vocabulary to send messages to objects.

In many cases, step 1 will be the responsibility of different programmers to steps 2 and 3. One group of programmers are given the task of designing objects for an organization's own class library, while other groups use the objects from the class library as the building blocks to create business applications.

The class library designers would take charge of defining the vocabulary (step 1 above) for objects in the class library, ensuring that it was consistent and readable. The application builders would use the vocabularies (steps 2 and 3) to simplify their own development work, and to enhance the readability of their applications.

13.2.1 Defining a Vocabulary

You define vocabularies on a class basis. Each vocabulary is defined by an external class. An external class is a program which defines a set of method interfaces, but does not include any code.

13.2.1.1 External Class Structure

External classes have the same basic structure as any Object COBOL class. The outline structure for the BankAccount external class is shown below:

 class-id. BankAccount is external
           inherits from Base
 object section.
 class-control.
     BankAccount is class "account"
     Base is class "Base"
     .
    
* Class methods.
 class-id. "new"
* Method interface definition. 
 end program "new".
  ...
    
* Object program. 
    
 object.
    
* Instance methods
     
 end object.
    
 end class BankAccount.

The only differences between the external class definition for BankAccount, and the BankAccount class itself, are:

13.2.1.2 Method Interface

A method program interface definition has the following elements:

There is no storage declared, and no code in the Procedure Division. The method interface for the credit method looks like this:

 method-id. credit is method.
 linkage section.
 01 lnkAmount        pic 9(10)v99.
 
 procedure division using lnkAmount
     INVOKED AS ==Credit <self> [with] <lnkAmount> ==
         OR AS ==deposit <lnkAmount> [in] <self> ==
 . 
 
  end method credit.

The elements of the new syntax definition are:

Delimiters The INVOKED AS phrase indicates the start of the verb or function signature syntax. Each signature is bracketed by double equals "==". You can specify multiple definitions for a method by using the OR AS phrase.
Parameters Appear inside angle brackets "<>". All parameters must be passed by reference in the PROCEDURE DIVISION USING statement.

<SELF> is a special parameter which marks the point where the sender puts the object reference for the method. <SELF> must always appear once (and only once) in the interface definition. You can also use <THIS> and <OBJECT> as synonyms for <SELF>.

Compulsory words Appear exactly as they are to be used.
Noise words Appear inside square brackets "[ ]". Noise words enable you to make your new verbs and functions more readable. In the example above, you can use both these ways to invoke the credit method:
                         credit anAccount with aSumOfMoney
                         credit anaccount aSumOfMoney

By default, the following are disallowed in the verb signatures of method interface definitions:

You can enable the use of both these in verb signatures by setting compiler directive OOCTRL(-Q). Setting this directive disables the use of qualification in a verb signature which invokes a method. The default setting is OOCTRL(+Q).

13.2.1.2.1 User Defined Functions

You can also create new functions with user vocabularies. The new functions look like COBOL intrinsic functions when used in a program.

For example, you could code:

    compute interest = .06 * function balance (thisBankAccount)

You can omit the keyword function, so you could also write:

    compute interest = .06 * balance (thisBankAccount)

The definition of function balance looks like this:

 method-id. "getBalance".
 linkage section.
 01 lsAmount pic S9(10)v99.

 procedure division returning lsAmount
    INVOKED AS FUNCTION == Balance ( <self> ) ==.
 end method "getbalance".

13.2.2 Using Vocabulary-defined Verbs and Functions

Once a user-defined vocabulary exists for a class, it is very easy to use it in any Object COBOL classes or procedural COBOL programs.

To use vocabulary defined verbs or functions:

  1. Copy the external class defining the new syntax into your own program. If you are using several objects with their own vocabulary definitions, you must copy the external class for each one.

    Put external classes at the top of your program, ahead of any PROGRAM-ID or CLASS-ID statements.

  2. Use the vocabulary defined for the object in place of an INVOKE verb and method name when you want to send the object a message.

One advantage which vocabularies give you is that data values are cast into the format expected by the target method. For example, you can code:

    credit anAccount with 5

The interface definition at the start of the program enables the Checker to convert the number 5 into the numeric format expected by the method. The equivalent using standard Object COBOL requires two statements:

    move 5 to anAmount
    invoke anAccount "credit" using anAmount

To see an example which uses requirements-based vocabulary, see the tutorial Requirements-based Vocabulary Tutorial.


Copyright © 1999 MERANT International Limited. All rights reserved.
This document and the proprietary marks and names used herein are protected by international law.

PreviousExtending a Class Debugging Object COBOL ApplicationsNext