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 adapter pattern


Another important design pattern is the adapter design. As the name suggests, the adapter design is used for conversion of one object into another object belonging to a different class. An adapter class will have a method that takes as input the object reference that is to be converted and outputs it into the other object reference format.

We have referred to the cl_salv_tree_adapter standard class while making of this recipe.

Getting ready

In order to demonstrate the adapter, we need two classes (input class and output class). The input class will be the fac_meth_class created earlier. For the output, we will create another class fac_meth_class2. This will serve as the class, into the format of which the input object will be converted.

It is without a factory method for sake of simplicity. It contains employee number and employee name but the format of these two is different from the classes shown in the previous recipes. The employee name of this class is based on data element emnam, whereas the number is a character without zeros having length as eight. The name is of the form (firstname lastname), meaning John Reed will be stored as John Reed and not Reed John as in the previous recipes. The constructor outputs the message, Converted employee created.

We will use the same class used previously as the input object for the adapter method.

How to do it...

For creating a singleton class, follow these steps:

  1. Create a deferred definition of the adapter class adapter_meth_class that we are going to create in the next step.

  2. Specify the adapter_meth_class as a friend of our fact_meth_class class in the definition via the FRIENDS addition.

  3. The adapter class is then defined. It contains a static adapter method adapter that imports an object based on fac_meth_class and returns one in the fac_meth_class2 format.

  4. The implementation of the adapter class is then created. It contains the code of the adapter method. The adapter method will convert the incoming number of the employee from numeric to character format. In addition, the name of the employee is converted to the firstname lastname format. The new object based on the second class fac_meth_class2 is then created and returned as an exporting parameter of the method.

  5. While calling the adapter method, you first create an object based on the fac_meth_class class that is a factory method (for illustrative purpose), similar to the previous recipe for the object reference EMP. This is then passed on to the static adapter method of the adapter_meth_class. The adapter class returns the converted object in the second format.

How it works...

When the program calls the static method adapter of the class adapter_meth_class , the code in the adapter method is executed. The adapter method calls the necessary code for converting the number into the character format and any zeros are removed from the number. In addition, the SPLIT statement is called for converting name of the employee in the (first name last name) format such as converting Reed John into John Reed. Finally the CREATE OBJECT is called in order to create the object in the converted class format . This triggers the constructor for the converted class fac_meth_class2 that outputs the message "Converted: Employee Created having number 1234" and name John Reed. Since we called the factory method of the original fac_meth_class before the adapter method call, the original constructor was also called and message printed also.

See also

  • Design Patterns in Object-Oriented ABAP published by SAP-Press