Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
SAP ABAP Advanced Cookbook

You're reading from   SAP ABAP Advanced Cookbook Featuring over 80 sophisticated recipes, this is a superb tutorial for ABAP developers and consultants. It teaches you advanced SAP programming using the high level language through diagrams, step-by-step instructions, and real-time examples.

Arrow left icon
Product type Paperback
Published in Dec 2012
Publisher Packt
ISBN-13 9781849684880
Length 316 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Rehan Zaidi Rehan Zaidi
Author Profile Icon Rehan Zaidi
Rehan Zaidi
Arrow right icon
View More author details
Toc

Table of Contents (22) Chapters Close

SAP ABAP Advanced Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. ABAP Objects 2. Dynamic Programming FREE CHAPTER 3. ALV Tricks 4. Regular Expressions 5. Optimizing Programs 6. Doing More with Selection Screens 7. Smart Forms – Tips and Tricks 8. Working with SQL Trace 9. Code Inspector 10. Simple Transformations 11. Sending E-mail Using BCS Classes 12. Creating and Consuming Web Services 13. SAP Interactive Forms by Adobe 14. Web Dynpro for ABAP 15. Floorplan Manager 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

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image