Book Image

SAP ABAP Advanced Cookbook

By : Rehan Zaidi
Book Image

SAP ABAP Advanced Cookbook

By: Rehan Zaidi

Overview of this book

ABAP (Advanced Business Application Programming) is SAP's proprietary 4th Generation Language (4GL). SAP core is written almost entirely in ABAP.ABAP is a high level programming language used in SAP for development and other customization processes."SAP ABAP Advanced Cookbook"ù covers advanced SAP programming applications with ABAP. It teaches you to enhance SAP applications by developing custom reports and interfaces with ABAP programming. This cookbook has quick and advanced real world recipes for programming ABAP.It begins with the applications of ABAP Objects and ALV tips and tricks. It then covers Design Patterns and Dynamic Programming in detail.You will also learn the usage of quality improvement tools such as transaction SAT, SQL Trace, and the Code Inspector.Simple transformations and its application in Excel Downloading will also be discussed, as well as the newest topics of Adobe Interactive Forms and the consumption and creation of Web services. The book comes to an end by covering advanced usage of Web Dynpro for ABAP and the latest advancement in Floorplan Manager.
Table of Contents (22 chapters)
SAP ABAP Advanced Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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.

  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.

  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 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.