Creating classes based on factory methods
One important design pattern that is used in object-oriented ABAP is the factory design. This allows you to create objects of a particular class either via a factory
class or via factory
method defined within the class. The emphasis of this recipe is to design a class that supports the creation of its objects via a factory
method, rather than direct instantiation outside the class via CREATE OBJECT
statement.
A factory
method is a static method that creates and then returns (as a parameter) a reference to the object of the class it belongs to. The code for the creation of the object is contained within the factory
method. This recipe shows the factory design. You may further modify to enhance the structure in order to suit your needs
We have referred to the coding of the standard cl_salv_table
class factory method for creating the class shown in this recipe. The class created in this recipe will be used in the subsequent recipes of singleton and adapter design pattern.
Getting ready
For the sake of this recipe and the ones that follow, we will focus on an employee and name example. The class will encapsulate an eight-character number (in numeric form) for the employee number 00000014
and a 30-character field for the employee name. For example, there can be an employee John Reed
with number. This will be stored in the private attributes of the class as Name
and Number
.
How to do it...
For creating a class as a factory method design, follow these steps:
Create a class definition for
fac_meth_class
in the program. Thefactory
method is a static method for the class and is defined viaCLASS-METHODS
. The class definition contains the additioncreate private
in order to stopthe instantiation of the class from outside viaCREATE 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
andsmnam
respectively.The static method
factory
imports the name and number of the employee to be created and returns the employee objectemployee_obj
of the object referencefac_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 thefactory
and theconstructor
is written here. Thefactory
method receives the number and the name of the employee to be created. It includes theCREATE 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 thefac_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 referencesemp1
andemp2
, that is, employee00000012
and0000014
.
How it works...
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
.