Defining a factory method
Define COBOL factory methods in the PROCEDURE
DIVISION
of the FACTORY
paragraph of a class
definition. A factory method defines an operation that is supported
by a class independently of any object instance of the class. COBOL
factory methods are equivalent to Java™ public
static methods.
You typically define factory methods for classes whose
instances require complex initialization, that is, to values that
you cannot assign by using VALUE
clauses alone. Within
a factory method you can invoke instance methods to initialize the
instance data. A factory method cannot directly access instance data.
You
can code factory attribute (get and set) methods to make factory data
accessible from outside the FACTORY
paragraph, for
example, to make the data accessible from instance methods in the
same class or from a client program. For example, the Account class
could define a factory method getNumberOfAccounts to return the current
tally of the number of accounts.
You can use factory methods
to wrap procedure-oriented COBOL programs so that they are accessible
from Java programs. You can
code a factory method called main
to enable you to
run an OO application by using the java command,
and to structure your applications in keeping with standard Java practice. See the related tasks
for details.
In defining factory methods, you use the same
syntax that you use to define instance methods. A COBOL factory method
definition consists of four divisions (like a COBOL program), followed
by an END METHOD
marker:
Division | Purpose | Syntax |
---|---|---|
IDENTIFICATION (required) |
Same as for a class instance method | Same as for a class instance method (required) |
ENVIRONMENT (optional) |
Same as for a class instance method | Same as for a class instance method |
DATA (optional) |
Same as for a class instance method | Same as for a class instance method |
PROCEDURE (optional) |
Same as for a class instance method | Same as for a class instance method |
Within a class definition, you do not need to make each factory method-name unique, but you do need to give each factory method a unique signature. You can overload factory methods in exactly the same way that you overload instance methods. For example, the CheckingAccount subclass provides two versions of the factory method createCheckingAccount: one that initializes the account to have a default balance of zero, and one that allows the opening balance to be passed in. Clients can invoke either createCheckingAccount method by passing arguments that match the signature of the intended method.
If you define
a data item with the same name in both the DATA DIVISION
of
a factory method and the DATA DIVISION
of the FACTORY
paragraph,
a reference in the method to that data-name refers only to the method
data item. The method DATA DIVISION
takes precedence.
Example: defining a factory (with methods)
Structuring OO applications
Wrapping procedure-oriented COBOL programs
Instantiating COBOL classes
Defining a class instance method
Coding attribute (get and set) methods
Overloading an instance method
Hiding a factory or static method
Invoking factory or static methods
Using object-oriented COBOL and Java under IMS