Let us understand what is Singleton class or design pattern in SAP ABAP, the Difference between Singleton class and static class in SAP ABAP, the Advantages of Singleton Class in ABAP, singleton class ABAP example and so on.
Definition of Singleton Class
What is Singleton class in SAP ABAP: The concept of restricting the instantiation of the class to only one object is called Singleton. As the name suggests, it will restrict to create only one instance of a class. This is known as Singleton Pattern or Singleton Class in OO ABAP.
Sometimes in 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.
Steps 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.
Sample OO ABAP Program of Singleton Class
============Singleton class abap program example============
DATA : IT_MARA TYPE TABLE OF MARA,
WA_MARA TYPE MARA.
CLASS CL_SINGLETON DEFINITION CREATE PRIVATE. “create a private class
METHODS: GET_MARA. “actual method get mara
CLASS-METHODS: INSTANTIATE RETURNING VALUE(LR_INST) TYPE REF TO CL_SINGLETON . “create a static method
CLASS-DATA: LR_INST TYPE REF TO CL_SINGLETON. “private variable
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.
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.
IF LR_INST IS INITIAL.
CREATE OBJECT LR_INST. “create object
Difference between a singleton and a static class ABAP?
Singleton pattern: is one of the simplest design patterns which involves only one class that instantiates itself to make sure that it creates one instance.
Singleton ensures that it has only one instance and provides a global point of access to the object.
So when you create a singleton class it does not allow you to create multiple instances of the class, rather a single instance will be created which acts as a global access point to reference that class throughout the lifetime of the application component
The best examples are the logger classes.
Let me explain with the simplest nearby example, suppose you log into your Facebook account so one instance will be created for you until you logout and with the same instance you can visit pages/profiles/messages, or cross-application validations will be done.
Static Class is a class that needs no instance to access its components.
You can directly access the components using the “=>” operator.
Furthermore, singleton classes are always a private class that cannot be instantiated by simply creating a reference to the class and then creating an object.
No, it won’t work.
You need to have a private attribute declared in that singleton class which has a type reference to the same class.
Advantages of Singleton Class in ABAP:
There are many advantages of Singleton Class in ABAP as shown below.
- Testability improves when we use the Singleton class,
- Singleton’s design pattern supports the main moto of the Object-Oriented Design principle by allowing it to open for enhancements and closed for modifications.
- The advantage of using a private constructor in Singleton class (as no one creates an object of your class).