Creating classes based on factory methods
Create a class definition for fac_meth_class in the program.
The factory method is a static method for the class and is defined via
CLASS-METHODS.
The class definition contains the addition create private in
order to stop the instantiation of the class from outside via CREATE
OBJECT.
A constructor is defined that allows setting the value of the
number and the employee name.
The private attributes employee number and name are defined,
as it is based on the dictionary data
elements persno and smnam respectively.
The static method factory imports the name and number of the
employee to be created and returns the
employee object employee_obj of
the object reference fac_meth_class.
The constructor takes as input the
number and the employee name.
The implementation of the fac_meth_class object reference is
then created.
The code for the factory and the constructor is written here.
The factory method receives the number and the name of the employee to be
created.
It includes the CREATE OBJECT statement for creation of the employee object.
The constructor assigns the number and employee name to the
corresponding
private attributes of the newly constructed object.
A WRITE
statement is also included that outputs the name and number of the successful
created employee.
Finally, the call for the factory method is included. The
static method of the
fac_meth_class=>factory object is included and passed
with the number and
name of the employee to be created. A code shows two such
method calls for object
references emp1 and emp2, that is, employee
00000012 and 0000014.
When the program calls the static factory method, the code
within the factory method
is called for each of the two objects emp1 and emp2. The
factory method triggers CREATE
OBJECT statement, which creates a new object and calls the
constructor.
The constructor is called twice, once for each of the two
instantiated objects emp1 and emp2.
This prints the message successful creation for emp1 and
emp2.
No comments:
Post a Comment