Gyan Factory

Gyan Factory
SAP Technical Project Support

Tuesday, February 16, 2016

ABAP Object Design Patterns – Singleton

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.
UPDATE:This blog post has been updated with clear example demo on 12/17/2009. So, there could be some comments which would be obsolete.

What is 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.

Code Lines

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
*&---------------------------------------------------------------------*
  DATAlo_application TYPE REF TO lcl_application.
  IF lo_application IS BOUND.
    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:
*&---------------------------------------------------------------------*
*& 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 CREATE PRIVATE.
*
  PUBLIC SECTION.
*   Static Method which will return us the object reference
    CLASS-METHODS:
      get_apps_instance
        RETURNING
          value(ro_appsTYPE REF TO lcl_application.
*
    METHODS:
      set_v_name
        IMPORTING
          iv_name TYPE char30,
      get_v_name
        RETURNING
          value(rv_nameTYPE char30.
*
  PRIVATE SECTION.
*   static class reference to hold the existing object reference
    CLASS-DATAlo_apps TYPE REF TO lcl_application.
*
    DATAv_name TYPE char30.
*
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.
    ENDIF.
*   assigning reference back to exporting parameter
    ro_apps lo_apps.
  ENDMETHOD.                    "get_apps_instance
*
  METHOD set_v_name.
    me->v_name iv_name.
  ENDMETHOD.                    "set_v_name
*
  METHOD get_v_name.
    rv_name me->v_name.
  ENDMETHOD.                    "get_v_name
*
ENDCLASS.                    "lcl_application IMPLEMENTATION
*
*
START-OF-SELECTION.
*
*.Reference: 1 .........................................
  DATAlo_application TYPE REF TO lcl_application.
  DATAlv_result TYPE char30.
*
  WRITE'LO_APPLICATION: '.
* calling the method which gets us the instance
* Statement CREATE OBJECT LO_APPLICATION
* would not work as the class LCL_APPLICATION instantiation
* is set to PRIVATE
  lo_application lcl_application=>get_apps_instance).
* Set the variable and get it back.
  lo_application->set_v_name'This is first Object' ).
  lv_result lo_application->get_v_name).
  WRITE/ lv_result.
  CLEAR lv_result.
*
*.Reference: 2............................................
  DATAlo_2nd_apps TYPE REF TO lcl_application.
  SKIP 2.
  WRITE'LO_2ND_APPS : '.
*   calling the method which gets us the instance
* By calling GET_APPS_INSTANCE method again to get the singleton
* object, it would give the same object back and assign it to
* LO_2ND_APPS object reference. This would be varified by
* getting the value of the instance attribute V_NAME
  lo_2nd_apps lcl_application=>get_apps_instance).
  lv_result lo_2nd_apps->get_v_name).
  WRITE/ lv_result.
  CLEAR lv_result.
This will generate the output like this:

Update – Singleton Usage

Recently, I have posted another article on Singleton – ABAP Objects Design Patterns Singleton Usage. This article demonstrates step by step Singleton implementation along with UML sequence diagram.
SDN Wiki – Singleton design pattern

No comments:

Post a Comment