# Content
1 Singleton Concept in Object Oriented ABAP
2 Creating global singleton class in OOABAP
3 Creating Local singleton class in OOABAP
Singleton Concept in Object
Oriented ABAP
Last Updated: June 5th 2014 by Ashok Kumar Reddy
Working with singleton design patterns in Object oriented ABAP, Singleton class
in OOABAP
+ -
Sometimes in the real-time business scenarios, we need to have only one instance for a class at a
point of time, for this one we have the concept of singleton in ABAP objects.
Singleton pattern : is the one of the simplest design patterns which involves only one class which
instantiates itself to make sure that it creates one instance.Singleton ensues that it has only one
instance and provides global point of access to the object.
Example - Logger Classes
The Singleton pattern is used in the design of logger classes. This classes are usually implemented
as a singletons, and provides a global logging access point in all the application components without
being necessary to create an object each time a logging operations is performed.
Steps to be followed to create singleton class
Create a private class.
Add a private attribute with reference to the same class.
Create a public static method with returning value of type reference to same class.
Create implementation and create object in the implementation of public static method.
Call static method in any program to create instance for the singleton class.
In the next lesson we will be learning how to create singleton class.
Learner Questions
No Questions by learners, be first one to ask ..!!
Creating global singleton class in
OOABAP
Last Updated: June 5th 2014 by Ashok Kumar Reddy
+ -
Follow below steps to create singleton class in Object oriented ABAP
Step1: Create a private class in Se24
Go to SE24, provide name as ZSAPN_CL_SINGLETON and click on create.
Provide description, select class as private and save.
Go to attributes tab, add a private attribute of type reference to same class.
Go to methods tab, add a static method INSTANTIATE and click on parameters button .
Add a returning parameter as below.
Go back to methods, double click on method INSTANTIATE and add below code.
METHOD INSTANTIATE.
IF LV_INST IS NOT BOUND.
CREATE OBJECT RO_INST.
LV_INST = RO_INST.
ELSE.
RO_INST = LV_INST.
ENDIF.
ENDMETHOD.
Save and activate the class, go to SE38, create a program ZSAPN_SINGLETON_GLOBAL.
Try to create instance directly.
REPORT ZSAPN_SINGLETON_GLOBAL.
DATA : LO_CLASS TYPE REF TO ZCL_SAPN_SINGLETON.
create OBJECT lo_class.
It will through an error.
Now create instance through static method.
REPORT ZSAPN_SINGLETON_GLOBAL.
DATA : LO_CLASS TYPE REF TO ZCL_SAPN_SINGLETON.
LO_CLASS = ZCL_SAPN_SINGLETON=>INSTANTIATE( ).
Now you can access all public instance components through the object.
Learner Questions
No Questions by learners, be first one to ask ..!!
Creating Local singleton class in
OOABAP
Last Updated: June 5th 2014 by Ashok Kumar Reddy
+ -
Below is the example of local singleton class in Object Oriented ABAP Programming .
REPORT ZSAPN_SINGLETON_CLASS.
DATA : IT_MARA TYPE TABLE OF MARA,
WA_MARA TYPE MARA.
CLASS CL_SINGLETON DEFINITION CREATE PRIVATE. "create a private class
PUBLIC SECTION.
METHODS: GET_MARA. "actual method get mara
CLASS-METHODS: INSTANTIATE RETURNING VALUE(LR_INST) TYPE REF TO CL_SINGLETON .
"create a static method
PRIVATE SECTION.
CLASS-DATA: LR_INST TYPE REF TO CL_SINGLETON. "private variable
ENDCLASS.
DATA : LO_CLASS TYPE REF TO CL_SINGLETON.
LO_CLASS = CL_SINGLETON=>INSTANTIATE( ). "get instance of class
*CREATE OBJECT LO_CLASS.
LO_CLASS->GET_MARA( ). "get mara data
CLASS CL_SINGLETON IMPLEMENTATION.
METHOD GET_MARA.
SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
ENDLOOP.
ENDMETHOD.
METHOD INSTANTIATE.
IF LR_INST IS INITIAL.
CREATE OBJECT LR_INST. "create object
ENDIF.
ENDMETHOD.
ENDCLASS.
Learner Questions
No Questions by learners, be first one to ask ..!!