Application Development and Automation 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: 
Read only

Singleton doubt

Former Member
0 Likes
543

Hi Experts,

I have doubt in singleton. Recently I have created a singleton class for my program using a class-constructor as given below -

CLASS lcl_zus_r5071_dyn DEFINITION CREATE PRIVATE.
PUBLIC SECTION.
  CLASS-METHODS: class_constructor,
                                   get_instance EXPORTING r_factory TYPE REF TO lcl_zus_r5071_dyn.

   ...


PRIVATE SECTION.
  CLASS-DATA: factory_instance TYPE REF TO lcl_zus_r5071_dyn.
ENDCLASS.

CLASS lcl_zus_r5071_dyn IMPLEMENTATION.   

* Create Instance
  METHOD class_constructor.
    CREATE OBJECT factory_instance.
  ENDMETHOD.                    "class_constructor

* Send instance to Client -
  METHOD get_instance.
    r_factory = factory_instance.
  ENDMETHOD.            

then I get the  object reference using below code in client program -

INITIALIZATION.
* Create single tones -
lcl_zus_r5071_dyn=>get_instance( IMPORTING r_factory = go_sel ).

My doubt arise when I saw SAP help and other forum threads for creating singleton. Every blog suggest to use only a static method for both creation of object and sending the same object to caller. Thus I would like to know is there any potential design issue if I use first static constructor for object creation and then a static method to send the reference ?

       

1 ACCEPTED SOLUTION
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
467

Hello,

The only limitation to using STATIC constr for implementing the singleton design is that you cannot have any interface params Since the STATIC constr is called by the ABAP runtime environment the developer doesn't have any influence on it, hence no interface params.

So if your CLASS constr (or simply CONSTRUCTOR) has any params (IMPORTING of course), then you cannot use the static constructor to pass the actual params from the caller Hence the usage of STATIC constr is restricted in this case.

Thus I would like to know is there any potential design issue if I use first static constructor for object creation and then a static method to send the reference ?

It depends on how to want to design your CLASS constr; if you foresee the usage of params then i you should not use CLASS constr to implement singleton, else you can!

IMO the basic idea to design singleton is -

  1. Define the class instantiation as Private.
  2. Define an private attribute (object ref) which is of the type of the singleton class.

How you implement it, depends on the use case.

BR,

Suhas

2 REPLIES 2
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
468

Hello,

The only limitation to using STATIC constr for implementing the singleton design is that you cannot have any interface params Since the STATIC constr is called by the ABAP runtime environment the developer doesn't have any influence on it, hence no interface params.

So if your CLASS constr (or simply CONSTRUCTOR) has any params (IMPORTING of course), then you cannot use the static constructor to pass the actual params from the caller Hence the usage of STATIC constr is restricted in this case.

Thus I would like to know is there any potential design issue if I use first static constructor for object creation and then a static method to send the reference ?

It depends on how to want to design your CLASS constr; if you foresee the usage of params then i you should not use CLASS constr to implement singleton, else you can!

IMO the basic idea to design singleton is -

  1. Define the class instantiation as Private.
  2. Define an private attribute (object ref) which is of the type of the singleton class.

How you implement it, depends on the use case.

BR,

Suhas

Read only

Former Member
0 Likes
467

Thanks Suhas. It clarifies my doubt.

I think if I want to use Singleton in inheritence, then I have to plan for a factory method with parameter (Usually Parent/child class name). In that case, class-constructor is of no use.