MainframeMaster

COBOL Data Encapsulation

Data encapsulation hides internal data implementation details and provides controlled access through well-defined interfaces. Learn to implement data hiding, access control, and modular design in COBOL programs.

Basic Data Encapsulation

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
WORKING-STORAGE SECTION. 01 CUSTOMER-DATA. 05 CUSTOMER-ID PIC 9(6). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-BALANCE PIC 9(9)V99. 05 CUSTOMER-STATUS PIC X. PROCEDURE DIVISION. *> Encapsulated data access procedures PERFORM INITIALIZE-CUSTOMER-DATA PERFORM SET-CUSTOMER-NAME USING 'JOHN SMITH' PERFORM GET-CUSTOMER-BALANCE INTO DISPLAY-BALANCE PERFORM UPDATE-CUSTOMER-STATUS USING 'A'.

Encapsulate data access through procedures rather than direct manipulation. Define procedures for initializing, setting, getting, and updating data. This provides controlled access and hides implementation details.

Data Access Procedures

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
INITIALIZE-CUSTOMER-DATA. MOVE 0 TO CUSTOMER-ID MOVE SPACES TO CUSTOMER-NAME MOVE 0 TO CUSTOMER-BALANCE MOVE 'N' TO CUSTOMER-STATUS. SET-CUSTOMER-NAME USING INPUT-NAME. IF FUNCTION LENGTH(FUNCTION TRIM(INPUT-NAME)) > 0 MOVE FUNCTION UPPER-CASE(INPUT-NAME) TO CUSTOMER-NAME DISPLAY 'Customer name set to: ' CUSTOMER-NAME ELSE DISPLAY 'Error: Invalid customer name' END-IF. GET-CUSTOMER-BALANCE INTO OUTPUT-BALANCE. MOVE CUSTOMER-BALANCE TO OUTPUT-BALANCE DISPLAY 'Customer balance retrieved: ' OUTPUT-BALANCE. UPDATE-CUSTOMER-STATUS USING NEW-STATUS. IF NEW-STATUS = 'A' OR NEW-STATUS = 'I' OR NEW-STATUS = 'S' MOVE NEW-STATUS TO CUSTOMER-STATUS DISPLAY 'Customer status updated to: ' CUSTOMER-STATUS ELSE DISPLAY 'Error: Invalid status value: ' NEW-STATUS END-IF.

Create procedures for all data access operations. Include validation, error checking, and logging in access procedures. This ensures data integrity and provides a consistent interface for data manipulation.

Data Validation and Integrity

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
VALIDATE-CUSTOMER-DATA. MOVE 'Y' TO VALIDATION-FLAG *> Validate customer ID IF CUSTOMER-ID = 0 OR CUSTOMER-ID > 999999 DISPLAY 'Error: Invalid customer ID: ' CUSTOMER-ID MOVE 'N' TO VALIDATION-FLAG END-IF. *> Validate customer name IF CUSTOMER-NAME = SPACES OR CUSTOMER-NAME = LOW-VALUES DISPLAY 'Error: Customer name is required' MOVE 'N' TO VALIDATION-FLAG END-IF. *> Validate customer balance IF CUSTOMER-BALANCE < 0 DISPLAY 'Error: Customer balance cannot be negative' MOVE 'N' TO VALIDATION-FLAG END-IF. *> Validate customer status IF CUSTOMER-STATUS NOT = 'A' AND CUSTOMER-STATUS NOT = 'I' AND CUSTOMER-STATUS NOT = 'S' DISPLAY 'Error: Invalid customer status: ' CUSTOMER-STATUS MOVE 'N' TO VALIDATION-FLAG END-IF.

Implement comprehensive data validation to ensure data integrity. Check all data fields for valid values, ranges, and formats. Use validation flags to track data quality and provide meaningful error messages.

Modular Data Design

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
WORKING-STORAGE SECTION. 01 CUSTOMER-MODULE. 05 CUSTOMER-DATA. 10 CUSTOMER-ID PIC 9(6). 10 CUSTOMER-NAME PIC X(30). 10 CUSTOMER-BALANCE PIC 9(9)V99. 05 CUSTOMER-CONTROL. 10 VALIDATION-FLAG PIC X VALUE 'N'. 88 DATA-VALID VALUE 'Y'. 10 LAST-UPDATED PIC 9(8). 10 UPDATE-COUNT PIC 9(4) VALUE 0. 01 ACCOUNT-MODULE. 05 ACCOUNT-DATA. 10 ACCOUNT-NUMBER PIC 9(8). 10 ACCOUNT-TYPE PIC X. 10 ACCOUNT-BALANCE PIC 9(9)V99. 05 ACCOUNT-CONTROL. 10 VALIDATION-FLAG PIC X VALUE 'N'. 88 DATA-VALID VALUE 'Y'. 10 LAST-UPDATED PIC 9(8).

Design modular data structures with separate modules for different business entities. Each module contains its data and control information. This provides clear separation of concerns and easier maintenance.

Data Access Control

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
WORKING-STORAGE SECTION. 01 ACCESS-CONTROL. 05 USER-LEVEL PIC X. 88 ADMIN-USER VALUE 'A'. 88 MANAGER-USER VALUE 'M'. 88 CLERK-USER VALUE 'C'. 05 ACCESS-RIGHTS. 10 CAN-READ PIC X VALUE 'N'. 10 CAN-WRITE PIC X VALUE 'N'. 10 CAN-DELETE PIC X VALUE 'N'. PROCEDURE DIVISION. PERFORM SET-ACCESS-RIGHTS PERFORM VALIDATE-ACCESS USING 'READ' CUSTOMER-DATA.

Implement access control to restrict data access based on user roles and permissions. Define access rights for different operations and validate access before performing data operations.

Data Hiding Techniques

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
WORKING-STORAGE SECTION. 01 HIDDEN-DATA. 05 INTERNAL-COUNTER PIC 9(4) VALUE 0. 05 INTERNAL-FLAG PIC X VALUE 'N'. 05 INTERNAL-BUFFER PIC X(100). 01 PUBLIC-INTERFACE. 05 PUBLIC-COUNTER PIC 9(4) VALUE 0. 05 PUBLIC-STATUS PIC X VALUE 'N'. 88 STATUS-ACTIVE VALUE 'Y'. PROCEDURE DIVISION. *> Only expose necessary data through public interface PERFORM UPDATE-PUBLIC-COUNTER USING 100 PERFORM GET-PUBLIC-STATUS INTO DISPLAY-STATUS.

Hide internal implementation details by separating internal data from public interfaces. Only expose necessary data through well-defined public interfaces. This reduces coupling and improves maintainability.

Data Encapsulation Best Practices

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
WORKING-STORAGE SECTION. 01 ENCAPSULATED-MODULE. 05 MODULE-DATA. 10 DATA-FIELD-1 PIC X(20). 10 DATA-FIELD-2 PIC 9(6). 05 MODULE-CONTROL. 10 VALIDATION-FLAG PIC X VALUE 'N'. 88 DATA-VALID VALUE 'Y'. 10 ERROR-COUNT PIC 9(4) VALUE 0. 05 MODULE-INTERFACE. 10 GET-PROCEDURE PIC X(20) VALUE 'GET-MODULE-DATA'. 10 SET-PROCEDURE PIC X(20) VALUE 'SET-MODULE-DATA'. 10 VALIDATE-PROCEDURE PIC X(20) VALUE 'VALIDATE-MODULE-DATA'.

Follow best practices: define clear interfaces, implement comprehensive validation, use meaningful names, group related data together, document interfaces, and maintain consistent error handling across modules.

Error Handling and Logging

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
HANDLE-DATA-ERROR USING ERROR-TYPE ERROR-MESSAGE. ADD 1 TO ERROR-COUNT DISPLAY 'Data Error: ' ERROR-TYPE ' - ' ERROR-MESSAGE MOVE 'N' TO VALIDATION-FLAG *> Log error for audit trail MOVE FUNCTION CURRENT-DATE TO ERROR-TIMESTAMP WRITE ERROR-LOG-RECORD *> Set appropriate return code IF ERROR-TYPE = 'VALIDATION' MOVE 4 TO RETURN-CODE ELSE IF ERROR-TYPE = 'ACCESS' MOVE 8 TO RETURN-CODE ELSE MOVE 12 TO RETURN-CODE END-IF.

Implement comprehensive error handling and logging for data operations. Log all errors with timestamps, set appropriate return codes, and maintain audit trails for data integrity and troubleshooting.