SAP ABAP Advanced Cookbook
上QQ阅读APP看书,第一时间看更新

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:

  1. 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 stopthe 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.
    How to do it...
  2. The private attributes employee number and name are defined, as it is based on the dictionary data elements persno and smnam respectively.
  3. 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.
  4. 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.
    How to do it...
  5. 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.
  6. 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.
    How to do it...

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.

How it works...