2009 Mar 03 8:37 PM
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
2009 Mar 03 11:37 PM
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
2009 Mar 03 11:37 PM
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
2009 Mar 04 8:35 AM
Thank you Naimesh, I think this solves my question.
Best regards
David