Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How do we use singleton in OO ABAP, Can someone please give a perfect example.

How do we use singleton in OO ABAP, Can someone please give a perfect example. That would be great help

1 ACCEPTED SOLUTION

FredericGirod
Active Contributor

Singleton is basically, a static instance creation, That means when you will request the instance creation of your object, in the same LUW, SAP will always propose you the same instance.

You need to specify the creation of the instance is private, otherwise, someone will use the statement NEW zcl_temp_singleton_001().

--> We specify CREATE PRIVATE

You need a static method to create the instance.

--> Static method is not describe by METHODS but by CLASS-METHODS

You need a static variable, containing the already created instance.

--> Like method, you have to use CLASS-DATA

that's all.

The example:

CLASS zcl_temp_singleton_001 DEFINITION
  PUBLIC
  FINAL
  CREATE PRIVATE .


  PUBLIC SECTION.
    CLASS-METHODS get_instance
      RETURNING
        VALUE(ro_instance) TYPE REF TO zcl_temp_singleton_001.
    METHODS set_data
      IMPORTING
        iv_valeur TYPE string.
    METHODS get_data
      RETURNING
        VALUE(rv_valeur) TYPE string.


  PRIVATE SECTION.
    CLASS-DATA go_instance TYPE REF TO zcl_temp_singleton_001.
    DATA gv_valeur TYPE string.

ENDCLASS.



CLASS ZCL_TEMP_SINGLETON_001 IMPLEMENTATION.

  METHOD get_data.
    rv_valeur = gv_valeur.
  ENDMETHOD.


  METHOD get_instance.
    ro_instance = COND #( WHEN go_instance IS BOUND
                            THEN go_instance
                          ELSE NEW zcl_temp_singleton_001(  )  ).
    go_instance = ro_instance.
  ENDMETHOD.


  METHOD set_data.
    gv_valeur = iv_valeur.
  ENDMETHOD.

ENDCLASS.


an example how to use this code:

REPORT ztest_fred_003.

PARAMETERS p_valeur TYPE string.

DATA(mon_singleton) = zcl_temp_singleton_001=>get_instance(  ).
mon_singleton->set_data( p_valeur ).

PERFORM p_display_parameter IN PROGRAM ztest_fred_003b.

REPORT ztest_fred_003b.

FORM p_display_parameter.
  DATA(mon_singleton) = zcl_temp_singleton_001=>get_instance(  ).
  WRITE /1 mon_singleton->get_data( ).
ENDFORM.

1 REPLY 1

FredericGirod
Active Contributor

Singleton is basically, a static instance creation, That means when you will request the instance creation of your object, in the same LUW, SAP will always propose you the same instance.

You need to specify the creation of the instance is private, otherwise, someone will use the statement NEW zcl_temp_singleton_001().

--> We specify CREATE PRIVATE

You need a static method to create the instance.

--> Static method is not describe by METHODS but by CLASS-METHODS

You need a static variable, containing the already created instance.

--> Like method, you have to use CLASS-DATA

that's all.

The example:

CLASS zcl_temp_singleton_001 DEFINITION
  PUBLIC
  FINAL
  CREATE PRIVATE .


  PUBLIC SECTION.
    CLASS-METHODS get_instance
      RETURNING
        VALUE(ro_instance) TYPE REF TO zcl_temp_singleton_001.
    METHODS set_data
      IMPORTING
        iv_valeur TYPE string.
    METHODS get_data
      RETURNING
        VALUE(rv_valeur) TYPE string.


  PRIVATE SECTION.
    CLASS-DATA go_instance TYPE REF TO zcl_temp_singleton_001.
    DATA gv_valeur TYPE string.

ENDCLASS.



CLASS ZCL_TEMP_SINGLETON_001 IMPLEMENTATION.

  METHOD get_data.
    rv_valeur = gv_valeur.
  ENDMETHOD.


  METHOD get_instance.
    ro_instance = COND #( WHEN go_instance IS BOUND
                            THEN go_instance
                          ELSE NEW zcl_temp_singleton_001(  )  ).
    go_instance = ro_instance.
  ENDMETHOD.


  METHOD set_data.
    gv_valeur = iv_valeur.
  ENDMETHOD.

ENDCLASS.


an example how to use this code:

REPORT ztest_fred_003.

PARAMETERS p_valeur TYPE string.

DATA(mon_singleton) = zcl_temp_singleton_001=>get_instance(  ).
mon_singleton->set_data( p_valeur ).

PERFORM p_display_parameter IN PROGRAM ztest_fred_003b.

REPORT ztest_fred_003b.

FORM p_display_parameter.
  DATA(mon_singleton) = zcl_temp_singleton_001=>get_instance(  ).
  WRITE /1 mon_singleton->get_data( ).
ENDFORM.