‎2013 Aug 29 12:10 PM
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 ?
‎2013 Aug 29 12:58 PM
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 -
How you implement it, depends on the use case.
BR,
Suhas
‎2013 Aug 29 12:58 PM
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 -
How you implement it, depends on the use case.
BR,
Suhas
‎2013 Aug 29 3:14 PM
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.