|Extending a Class||Debugging Object COBOL Applications|
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.
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.
There are three steps to using requirements-based vocabularies:
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.
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.
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:
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
<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).
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".
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
Put external classes at the top of your program, ahead of any PROGRAM-ID or CLASS-ID statements.
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.
Copyright © 1999 MERANT International Limited. All rights reserved.
This document and the proprietary marks and names used herein are protected by international law.
|Extending a Class||Debugging Object COBOL Applications|