Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
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 FREE CHAPTER 2. Dynamic Programming 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 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.

You have been reading a chapter from
SAP ABAP Advanced Cookbook
Published in: Dec 2012
Publisher: Packt
ISBN-13: 9781849684880
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 €18.99/month. Cancel anytime