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: 

Singleton pattern with inheritance

Former Member
0 Kudos
827

Hello all,

I tried to design a class using the singleton pattern in ABAP Objects (with lazy creation). The class itself works fine - however, I struggle to create a subclass of my singleton since ABAP Object does not allow me to redefine static methods.

Does anyone know a solution in ABAP Objects to use the singleton pattern together with inheritance?

Thanks in advance for any ideas!

Best regards

David

1 ACCEPTED SOLUTION

naimesh_patel
Active Contributor
0 Kudos
155

You can try to pass the TYPE in the Singleton method for which you want to create an object.

In this example, I am passing the TYPE also to my GET_OBJECT (which is my singleton) method to create an object of that type.

Like:


*----------------------------------------------------------------------*
*       CLASS lcl_animal DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_animal DEFINITION.

  PUBLIC SECTION.
    CLASS-METHODS:
      get_object
        IMPORTING
          if_name TYPE string
        RETURNING
          value(ro_object) TYPE REF TO lcl_animal.

    METHODS:
      write_me.

  PRIVATE SECTION.
    CLASS-DATA:
      wo_singleton TYPE REF TO lcl_animal.

ENDCLASS.                    "lcl_animal DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_lion DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_lion DEFINITION INHERITING FROM lcl_animal.

  PUBLIC SECTION.
    METHODS:
      write_me REDEFINITION.

ENDCLASS.                    "lcl_lion DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_animal IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_animal IMPLEMENTATION.

  METHOD get_object.

    IF wo_singleton IS INITIAL.
      CREATE OBJECT wo_singleton TYPE (if_name).
    ENDIF.

    ro_object = wo_singleton.

  ENDMETHOD.                    "get_object

  METHOD write_me.

    WRITE: / 'I am an Animal'.

  ENDMETHOD.                    "write_me

ENDCLASS.                    "lcl_animal IMPLEMENTATION


*----------------------------------------------------------------------*
*       CLASS lcl_lion IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_lion IMPLEMENTATION.

  METHOD write_me.

    WRITE: / 'I am a LION'.

  ENDMETHOD.                    "write_me

ENDCLASS.                    "lcl_animal IMPLEMENTATION



START-OF-SELECTION.
*  DATA: lo_animal TYPE REF TO lcl_animal.
*
*  lo_animal = lcl_animal=>get_object( 'LCL_ANIMAL' ).
*
*  lo_animal->write_me( ).
*

  DATA: lo_lion TYPE REF TO lcl_lion.

  lo_lion ?= lcl_animal=>get_object( 'LCL_LION' ).
  lo_lion->write_me( ).

Regards,

Naimesh Patel

2 REPLIES 2

naimesh_patel
Active Contributor
0 Kudos
156

You can try to pass the TYPE in the Singleton method for which you want to create an object.

In this example, I am passing the TYPE also to my GET_OBJECT (which is my singleton) method to create an object of that type.

Like:


*----------------------------------------------------------------------*
*       CLASS lcl_animal DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_animal DEFINITION.

  PUBLIC SECTION.
    CLASS-METHODS:
      get_object
        IMPORTING
          if_name TYPE string
        RETURNING
          value(ro_object) TYPE REF TO lcl_animal.

    METHODS:
      write_me.

  PRIVATE SECTION.
    CLASS-DATA:
      wo_singleton TYPE REF TO lcl_animal.

ENDCLASS.                    "lcl_animal DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_lion DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_lion DEFINITION INHERITING FROM lcl_animal.

  PUBLIC SECTION.
    METHODS:
      write_me REDEFINITION.

ENDCLASS.                    "lcl_lion DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_animal IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_animal IMPLEMENTATION.

  METHOD get_object.

    IF wo_singleton IS INITIAL.
      CREATE OBJECT wo_singleton TYPE (if_name).
    ENDIF.

    ro_object = wo_singleton.

  ENDMETHOD.                    "get_object

  METHOD write_me.

    WRITE: / 'I am an Animal'.

  ENDMETHOD.                    "write_me

ENDCLASS.                    "lcl_animal IMPLEMENTATION


*----------------------------------------------------------------------*
*       CLASS lcl_lion IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_lion IMPLEMENTATION.

  METHOD write_me.

    WRITE: / 'I am a LION'.

  ENDMETHOD.                    "write_me

ENDCLASS.                    "lcl_animal IMPLEMENTATION



START-OF-SELECTION.
*  DATA: lo_animal TYPE REF TO lcl_animal.
*
*  lo_animal = lcl_animal=>get_object( 'LCL_ANIMAL' ).
*
*  lo_animal->write_me( ).
*

  DATA: lo_lion TYPE REF TO lcl_lion.

  lo_lion ?= lcl_animal=>get_object( 'LCL_LION' ).
  lo_lion->write_me( ).

Regards,

Naimesh Patel

0 Kudos
155

Thank you Naimesh, I think this solves my question.

Best regards

David