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 singleton design pattern


A singleton class is a class that can have only one instance at a time. Any attempt to create a second or more instances should not be allowed. This recipe shows how to create a class based on the singleton design.

Getting ready

We will use the same class created in the last recipe of factory method. We will make few changes to the class so that we can prevent the creation of multiple instances of the class. We will make a copy of the class (program) shown in the previous recipe and modify it. The name of the copy is singleton_class.

How to do it...

For creating a singleton class, follow these steps:

  1. Make sure the CREATE PRIVATE addition is included in the singleton class definition.

  2. Within the definition, a static attribute number_of_instances having type integer is added to the private section.

  3. The implementation of the class is then written. The factory method has to be slightly modified in order to force the singleton characteristic.

  4. In the implementation of the singleton class, the factory method now contains an IF statement that first checks the number of instances already there when the factory call is made. If the first instance is being created (that is, number_of_instances equals 0), the employee object is created and number_of_instances is set as 1. An ELSE condition is included to output a message if one instance already exists.

How it works...

Similar to the previous recipe, we try to instantiate two objects emp1 and emp2, having number 0000012 and 00000014 respectively. However, in our singleton class, we have added an attribute number_of_instances, which keeps track of the number of class instances that already exist. Upon creation of the first object, the factory method increments this static attribute to 1. On the second object creation attempt, the IF statement does not allow the CREATE OBJECT statement to be called a second time. The result is that the second object is not created. No further attempts of object creation will be allowed. Rather, a message saying that only one object instantiation is allowed is outputted for the second object creation attempt.

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 ₹800/month. Cancel anytime