ABAP Singleton Design Pattern

Summary: in this tutorial, you will learn what is ABAP singleton pattern in design patterns and how to implement it in ABAP.

Problems

The application needs one and only one instance of a class for coordinate actions across system.

Intent

  • Ensure that a class has one and only one instance and provide a global point of access to it.
  • Just-in-time initialization or initialize at the first use is encapsulated.

ABAP Singleton Pattern UML Diragram

The following illustrate UML diagram of the Singleton design pattern:

ABAP Singleton UML
ABAP Singleton UML

ABAP Singleton Pattern Implementation

Implementation of the singleton pattern has to satisfy the following criteria:

  • Only one instance can be created.
  • Requires global access principles.

From the above criteria, we can create a class with a method that creates a new instance of the class if one does not exist.

If an instance of the class already exists, the method just simply returns that object. To make sure that no one can create a new instance of the class by using the CREATE OBJECT statement, we can use CREATE PRIVATE addition in the class definition.

By doing this, the only method inside the class can create a new instance of itself.

*---------------------------------------------------------------------*
* Report  ZDP_SINGLETON
*
*---------------------------------------------------------------------*
* Description    : This program demonstrates ABAP Singleton
*                  Design Pattern
* Program Author : ABAPTutorial.com
*---------------------------------------------------------------------*

REPORT  zdp_singleton.
*----------------------------------------------------------------------*
*       CLASS lcl_singleton DEFINITION
*----------------------------------------------------------------------*
*  Singleton class
*----------------------------------------------------------------------*
CLASS lcl_singleton DEFINITION CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS:
        factory RETURNING value(re_intance) TYPE REF TO lcl_singleton.
    METHODS:
        get_attr RETURNING value(re_attr) TYPE i,
        set_attr IMPORTING im_attr TYPE i.
  PRIVATE SECTION.
    CLASS-DATA:
        mo_instance TYPE REF TO lcl_singleton.
    DATA:
        mv_attr TYPE i.

ENDCLASS.                    "lcl_singleton DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_singleton IMPLEMENTATION
*----------------------------------------------------------------------*
* Singleton class
*----------------------------------------------------------------------*
CLASS lcl_singleton IMPLEMENTATION.
  METHOD factory.
    IF mo_instance IS INITIAL.
      CREATE OBJECT mo_instance.
    ENDIF.
    re_intance = mo_instance.
  ENDMETHOD.                    "constructor
  METHOD get_attr.
    re_attr = mv_attr.
  ENDMETHOD.                    "get_attr
  METHOD set_attr.
    mv_attr = im_attr.
  ENDMETHOD.                    "set_attr
ENDCLASS.                    "lcl_singleton IMPLEMENTATION

DATA: go_obj1 TYPE REF TO lcl_singleton,
      go_obj2 LIKE go_obj1,
      gv_attr TYPE i.

START-OF-SELECTION.
  " Get an instance of lcl_singleton
  go_obj1 = lcl_singleton=>factory( ).
  go_obj1->set_attr( 10 ).

  " try to get another instance
  go_obj2 = lcl_singleton=>factory( ).
  " Check the attribute of the new instance
  " it should be the same as go_obj1.
  gv_attr =  go_obj2->get_attr( ).

  WRITE: gv_attr.