Design Patterns in ABAP – Singleton

SAPIn software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects. The term comes from the mathematical concept of a singleton.

  • Implementation of a singleton pattern must satisfy the single instance and global access principles.
  • It requires a mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among class objects.
  • The singleton pattern is implemented by creating a class with a method that creates a new instance of the class if one does not exist.
  • If an instance already exists, it simply returns a reference to that object.
  • To make sure that the object cannot be instantiated any other way, the constructor is made PRIVATE
  • Once you have the instance returned by method GET_INSTANCE, you can freely access the public interface (methods/attributes/types, …) of the class.

Singleton class diagram

CLASS lcl_singleton_object DEFINITION CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS:
      get_instance
        RETURNING value(result) TYPE REF TO lcl_singleton_object.

    METHODS:
      set_text IMPORTING i_text TYPE string,
      get_text RETURNING value(result) TYPE string.
  PRIVATE SECTION.
    CLASS-DATA:
      mo_singleton_object TYPE REF TO lcl_singleton_object.

    DATA:
      m_text TYPE string.

    METHODS:
      constructor.
ENDCLASS.
CLASS lcl_singleton_object IMPLEMENTATION.
  METHOD get_instance.
    IF mo_singleton_object IS NOT BOUND.
      CREATE OBJECT mo_singleton_object TYPE lcl_singleton_object.
    ENDIF.
    result = mo_singleton_object.
  ENDMETHOD.                    

  METHOD constructor.
*   ... constructor commands
    m_text = 'Object was created'.
  ENDMETHOD.                  

  METHOD get_text.
    result = m_text.
  ENDMETHOD.                  

  METHOD set_text.
    m_text = i_text.
  ENDMETHOD.                    
ENDCLASS.
START-OF-SELECTION.
  DATA:
    lo_obj_a       TYPE REF TO lcl_singleton_object,
    lo_obj_b       TYPE REF TO lcl_singleton_object,
    l_text         TYPE string.

  lo_obj_a = lcl_singleton_object=>get_instance( ).
  lo_obj_b = lcl_singleton_object=>get_instance( ).

  l_text = lo_obj_a->get_text( ).
  WRITE: / 'Object A: ', l_text.
  l_text = lo_obj_b->get_text( ).
  WRITE: / 'Object B: ', l_text.

  lo_obj_a->set_text( 'Text set from object A' ).
  l_text = lo_obj_a->get_text( ).
  WRITE: / 'Object A: ', l_text.
  l_text = lo_obj_b->get_text( ).
  WRITE: / 'Object B: ', l_text.

  lo_obj_a->set_text( 'Text set from object B' ).
  l_text = lo_obj_a->get_text( ).
  WRITE: / 'Object A: ', l_text.
  l_text = lo_obj_b->get_text( ).
  WRITE: / 'Object B: ', l_text.

Output

Singleton - output

Leave a Reply