Sample 19 demonstrates object persistence based on the program created in Sample 18, “Advanced Object-Oriented Programming.” In Sample 18, all objects are created in memory and thus upon termination of the program, all objects are lost.
In an actual system, however, data must be preserved when the program terminates. For an object-oriented system, in which the data is held in objects, the objects must continue to exist whether or not the application is executing. These objects are known as persistent objects.
In general, a persistent object can be implemented by relating the object to databases or files. In this example, persistent objects are rolled out by relating each object to an index file record.
For further details on object perpetuation, refer to the section “Making Objects Persistent” in the “NetCOBOL User’s Guide”.
Overview
The function of this sample is identical to that of Sample 18 except that the employee and address objects are stored in an index file, and persistence functions are added.
Persistence is implemented by adding the following methods to the employee and address objects.
Store-Method (Object method)
Stores an object in the file.
RetAt-Method (Factory method)
Loads an object from the file using the employee number as the key.
Remove-Method (Factory method)
Deletes an object from the file using the employee number as the key.
Actually, the object saving and retrieving is implemented by using the employee master class and the address master class. From the object user (the MAIN program in this example), however, the process is viewed as if the employee object assumed all the tasks.
Programs and Files in Sample 19
MAIN.COB (COBOL source program)
ALLMEM.COB (COBOL source program)
MEMBER.COB (COBOL source program)
MANAGER.COB (COBOL source program)
ADDRESS.COB (COBOL source program)
SET.COB (COBOL source program)
STORE.COB (COBOL source program)
ALLMEM_M.COB (COBOL source program)
ALLMEMMF (Data file)
BONU_MAN.COB (COBOL source program)
BONU_MEM.COB (COBOL source program)
MEM_SET.COB (COBOL source program)
MEM_STOR.COB (COBOL source program)
MAN_SET.COB (COBOL source program)
MAN_STOR.COB (COBOL source program)
ADDR_M.COB (COBOL source program)
ADDR_MF (Data file)
SALA_MAN.COB (COBOL source program)
SALA_MEM.COB (COBOL source program)
SAMPLE19.PRJ (Project file)
SAMPLE19.CBI (Compiler option file)
COBOL85.CBR (Environment variable initialization file)
COBOL Functions used in Sample 19
Object-oriented programming functions
Indexed file functions
Project management
Object-Oriented Syntax used in Sample 19
INVOKE and SET statements
Object properties
In-line method calls
REPOSITORY paragraphs
Class, object, factory and method definitions
Building the Application
Project Manager's build function is used to make the executable program.
Start the Project Manager.
Open project file SAMPLE19.PRJ.
Select Build from the Project menu.
Debugging the Application
The project file is supplied with the debugging option set. You can execute the program under the control of the COBOL Debugger by:
Selecting Debug from the Project menu.
Clicking on the OK button in the Start Debugging dialog box.
The COBOL Debugger loads the code as shown in the following figure.
Executing the Application
To run the application, select Execute from the Project menu.
The main application window is displayed.
For details on running the program, see Sample 18, ”Advanced Object-oriented Programming”. The main difference is that data you enter is preserved from one execution of the application to the next to the next.