in

Singleton Class or Design pattern in SAP ABAP, Advantages of Singleton Class

Difference between Singleton class and static class in SAP ABAP

singleton class abap example, Singleton object in ABAP, Singleton design pattern in SAP ABAP, Difference between Singleton class and static class in SAP ABAP, Advantages of Singleton Class in ABAP,

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.
singleton class abap example, Singleton object in ABAP, Singleton design pattern in SAP ABAP, Difference between Singleton class and static class in SAP ABAP, Advantages of Singleton Class in ABAP,
Singleton class ABAP example, Singleton object in ABAP, Singleton design pattern in SAP ABAP, Difference between Singleton class and static class in SAP ABAP, Advantages of Singleton Class in ABAP.

SAP OO ABAP Class Definition, Objects, Types Of Class, Implementation

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

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.

What is ERP Software, ABAP history, SAP R3 System Landscape

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.

  1. Testability improves when we use the Singleton class,
  2. 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.
  3. The advantage of using a private constructor in Singleton class (as no one creates an object of your class).

What do you think?

Written by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

ERP Software, the History of SAP, What is ABAP, Roles and responsibilities of ABAP or BASIS or Functional consultants, SAP System Landscape, and SAP R/3 Architecture.

What is ERP Software, ABAP history, SAP R3 System Landscape

SAP ABAP