SAP ABAP Object Design Patterns: Singleton

Posted by Krishh Webworld | 6:13 PM | , , , | 2 comments »

Today we will try to explore the design patterns in the ABAP Objects. We will start with the Singleton design pattern, which is the simplest of its family of design patterns.


What is the concept of the Singleton design pattern?
The concpet of restricting the instantiation of the a class to only and only to one object is called Singleton. As name suggests, it will restrict to create only one instance of a class. The calss will have a logic in place which will deny if the application will ask for more than one instance.

Let's try to understand with an example:
We have an application which will bring the pay stub of an employee for current month. In a standarad system, there will be only one active salary account for the employee with the company. Because of this fact, we should only create one object of the employee's salary account. In program, we are creating this object in a loop. So, what happens if we don't have a design pattern which will restrict it to create more than one object. Application will create, rather overwrite an instance of the class.

In ABAP, generally we check if the instance is created or not, like:
Code Snippet to check instance
 
*&---------------------------------------------------------------------*
DATA: lo_application TYPE REF TO lcl_application.
IF lo_application IS INITIAL.
CREATE OBJECT lo_application.
ENDIF.

But, if we take another reference to the class and create a instance of that, it will definatly allow. So, we need to have Singleton design pattern implemented.

How to implement Singleton design Pattern in ABAP?
We can use the CLASS-DATA(static data) to save the created instance within the class and check with that instance, if application asks for a new instance.

We will look at this example.

UML diagrm for the example:



Code Snippet for Design Patterns: Singleton
  
*&---------------------------------------------------------------------*
*& Report shows how to use the static data of the class to
*& implement the design patterns.
*&---------------------------------------------------------------------*
REPORT ztest_singleton_pattern.
*
*----------------------------------------------------------------------*
* CLASS lcl_application DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_application DEFINITION.
*
PUBLIC SECTION.
* Static Method which will return us the object reference
CLASS-METHODS:
get_apps_instance
EXPORTING
eo_apps TYPE REF TO lcl_application.
*
PRIVATE SECTION.
* static class reference to hold the existing object reference
CLASS-DATA: lo_apps TYPE REF TO lcl_application.
*
ENDCLASS. "lcl_application DEFINITION
*
*
*----------------------------------------------------------------------*
* CLASS lcl_application IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_application IMPLEMENTATION.
*
* This method will return the object reference to the calling application
METHOD get_apps_instance.
IF lo_apps IS INITIAL.
* creation of the object
CREATE OBJECT lo_apps.
* assigning reference back to exporting parameter
eo_apps = lo_apps.
ENDIF.
ENDMETHOD. "get_apps_instance
ENDCLASS. "lcl_application IMPLEMENTATION
*
*
START-OF-SELECTION.
*
*.Reference: 1 .........................................
DATA: lo_application TYPE REF TO lcl_application.
WRITE: / 'LO_APPLICATION: '.
* calling the method which gets us the instance
CALL METHOD lcl_application=>get_apps_instance
IMPORTING
eo_apps = lo_application.
IF lo_application IS NOT INITIAL.
WRITE: / 'Object has been created..!'.
ELSE.
WRITE: / 'Error: Object has not been created..!'.
ENDIF.
*
*.Reference: 2............................................
DATA: lo_2nd_apps TYPE REF TO lcl_application.
SKIP 2.
WRITE: / 'LO_2ND_APPS : '.
* calling the method which gets us the instance
* Our instance will be initial here because the STATIC calss
* reference LO_APPS in the calss LCL_APPLICATION will prevent
* this call to create a new instance.
CALL METHOD lcl_application=>get_apps_instance
IMPORTING
eo_apps = lo_2nd_apps.
IF lo_2nd_apps IS NOT INITIAL.
WRITE: / 'Object has been created..!'.
ELSE.
WRITE: / 'Error: Object has not been created..!'.
ENDIF.



This will generate the output like this:

2 comments

  1. Nethan Vidya // October 21, 2011 at 11:39 PM  

    architectSAP specialize in sap abap development services across the SAP Business Suite. Their team of expert SAP ABAP consultants has prior experience in all core modules of SAP ERP (FI, CO, SD, MM, PP, WM, PM...), SAP BW, SAP SCM (APO), SAP CRM, SAP Retail and SAP SRM to deliver SAP ABAP development services.

  2. admin // December 4, 2011 at 2:34 PM  

Archives

Subscribe Now: Feed Icon