Skip to Content

What is the concept of the Singleton design pattern?

The concept of restricting the instantiation of the class toonly and only to one object is called Singleton. As name suggests, it willrestrict to create only one instance of a class. The class will have logic inplace which will deny if the application will ask for more than one instance.

Let’s try to understand with following examples:

 

  1. We have an application which will bring the pay stub of an employee for current month. In a standard system, there will be only one active salary account forthe employee with the company. Because of this fact, we should only create oneobject of the employee’s salary account. In program, we are creating this object in a loop. So, what happens if we don’t have a design pattern which willrestrict it for creating more than one instance of that object? Application will create; rather overwrite an instance of the class. 
  2.  

  3. In WebDynpro ABAP when you want to enhance SAP standard components wherein a child component wants to share data with the parent component. You can’t add an interface node, not any interface method ina WebDynpro Enhancement component controller. Check the official announcementfrom SAP: SAP note 1476699.

 

So only way to share data between standard WebDynpro components is a singleton class.

This is how you would create a singleton class:

1.       Createa class in SE24 which has instantiation property as private, this would create a constructor which is private.

Create Singleton Class

2.       Since constructor is private, to instantiate the class, we will need to create a Public Static method. In this public static method we will instantiate the class. To do this we will create an attribute of the type singleton class and store the reference in this attribute.

 Class Attribute 

Class Attribute

 Class Parameters

3.       Code snippet for the public static method is

Code Snipplets
  

 

IF gref_obj IS not BOUND.
    CREATE OBJECT gref_obj TYPE zash_singleton_test.
  ENDIF.
  ref_obj = gref_obj.

 

4.       Now create an attribute at instance level of type Public which will be used to store the value to be shared.

 Store Values Attribute

5.       Now create an instance of the class in Child component and set the values to beshared in WDDOINIT of a child Componentcontroller

 

  DATA: lo_obj TYPE REF TO zash_singleton_test.
  lo_obj ?= zash_singleton_test=>get_instance( ).

lo_obj->a_test_string  = 

‘This value will be printed from calling Component ‘.

 

6.       Read this value from parent component’s WDDOINIT of Componentcontroller

  DATA lo_cmp_usage TYPE REF TO if_wd_component_usage.

  lo_cmp_usage =   wd_this->wd_cpuse_z_sub_comp( ).
  IF lo_cmp_usage->has_active_component( ) IS INITIAL.
    lo_cmp_usage->create_component( ).
  ENDIF.

  DATA: lo_obj1 TYPE REF TO zash_singleton_test.

  DATA z_retrieved_value TYPE string.

  lo_obj1 ?= zash_singleton_test=>get_instance( ).

  IF lo_obj1 IS BOUND.
    z_retrieved_value = lo_obj1->a_test_string.

  ENDIF.

 

Now this pattern can be used in Custom developed WebDynproABAP Components as well to share data between parent and child components this is light weight data sharing approach.

This approach also better allows UIBBs (User InterfaceBuilding Blocks) implemented via feeder classes to be integrated with WDComponents within the Floorplan Manager. 

To report this post you need to login first.

21 Comments

You must be Logged on to comment or reply to a post.

  1. Thomas Jung
    You make a good point about Singleton Pattern classes.  You are focus on WDA Enhancements here (although you mention UIBBs and FPM). I think you will find that Singleton Pattern is used very frequently in standard SAP FPM applications.  The Single class because the shared data context or model of the FPM application. It is very important to understand this concept, especially if you want to add your own UIBBs to exitisting Floorplan Applications.
    (0) 
    1. Ashish Shah Post author
      Thanks Thomas for your valuable feedback.
      I am sure there are many applications to this concept , i just highlighted one and would highlight others when i get a chance to.
      (0) 
  2. Suhas Saha
    Hi Ashish,

    For implementing Singleton pattern in OO-ABAP SAP has provided the CLASS_CONSTRUCTOR method.

    As you’ve mentioned the CLASS_CONSTRUCTOR is static which ensures the “singleton” principle is achieved.

    Cheers,
    Suhas

    (0) 
  3. Martin Voros
    Hi,

    the singletons bring many disadvantages so you need to be careful when you want to use them. For example the code using singleton is harder to unit test. Basically, there is no difference between singleton and global variable. Just search on Google for Singletons are evil. I personally try to avoid singletons but sometimes they are a good solution.

    Cheers

    (0) 
    1. Ashish Shah Post author
      Hi Martin,

      Singleton concept and Static attributes are differently interpreted in ABAP. For example in other object oriented languages Static variables declared in parent class are not shared in the inherited classes. each classes have their own copy of static fields.

      However here in ABAP OOPs , Static variables are shared within the subclasses – a concept of shared memory.

      So if you can point any specific scenario where you think Singleton is not useful then we can definitely discuss about it.

      Ashish

      Regards,
      Ashish Shah

      (0) 
  4. Gopalakrishnan K R
    Dear Ashish,

    I am not an expert in commenting this but will it not be a problem using static attribute in Web Application. Usage of static objects holding some STATE always creates a problem in web applications as static objects do not die even if session of web application gets over. And anybody else opening the new session of Web Application will not have a new STATE of the static object.
    Can any ABAP WD expert (including Ashish) comment on my message whether it is correct or not?

    Thanks and Regards,
    Gopal

    (0) 
    1. Thomas Jung
      >Usage of static objects holding some STATE always creates a problem in web applications as static objects do not die even if session of web application gets over.

      This is not the case in ABAP. Static objects in general work different in ABAP. Static objects and data are stored in the internal session.  They are shared across all instances within this internal session but they do no cross user sessions. 

      There is no central web container session in ABAP. Each Web Dynpro ABAP user sessions runs in its own dialog user session just like any SAPGUI user connection.

      This is why we have the explict concept of Shared Memory Objects which live at the Application Server level and cross user sessions.

      (0) 
      1. Valerie Lee

        Hi Ashish,

        Thanks for excellent blog.  I am develop a FPM OVP application which will be called from

        a classic report and a dialog program.

        Can you use the concept of singleton class to pass data from report or dialog program to the FPM application?  If yes, how.  I tried a simple one but it did not work. In your example, wd_child is a component usage of wd_parent.  But if it is a report, how can we pass data.  Here is my example.

        Report  Z_report1 –

        Form someroutine.

        DATA lo_obj TYPE REF TO zcl_singleton_test

        lo_obj ?= zcl_singleton_test=>get_instance( ).

          lo_obj->a_test_string = ‘Value from My Z Report’.

        endform.

        I created a ZWD_TEST component and in the WDOINIT of component controller

        I have code.

        DATA: lo_obj1 TYPE REF TO zcl_singleton_test.

          DATA z_retrieved_value TYPE string.
          lo_obj1 ?= zcl_singleton_test=>get_instance( ).


          IF lo_obj1 IS BOUND.
            z_retrieved_value = lo_obj1->a_test_string.
          ENDIF.

        (0) 
  5. Jeroen Custers

    Dear Ashish,

    Thanks for writing this blog. There are more blogs and discussions regarding sharing data between FPM GUIBB. I posted this question also to another blog about singleton class for data sharing. But since this blog has more comments I hope I will get a reply.

    Sharing with a singleton class works, but I think it doesn’t follow one of the principal concepts of the building blocks. By ‘connecting’ the building blocks (feeder class) to a singleton class their re-usability decreases.

    I’m working on a scenario now where the singleton class is dynamic. Each building block (feeder class) has a parameter (i.e. SINGLETON_CLASS). The value is assigned in the GAF configuration at design time and the singleton class instance is created at runtime. I created an interface for this. But this is pretty difficult to implement. What about the Wire Models concept? Should we use this instead?

    What do other people think about this? Does SAP have plans for the future regarding this topic?

    Regards,

    Jeroen

    (0) 
    1. Ashish Shah Post author

      Hi Jeroen,

      I am afraid i have not used wire models and can not comment on it.

      However it is possible for each building blocks to get connected to more than one singleton classes.

      why dont you use one singleton class to share data and other for different purposes.

      Regards,

      Ashish

      (0) 
      1. Jeroen Custers

        Hi Ashish,

        Maybe I can describe my point of concern better with an example.

        FPM application 1

            uses GUIBB A, GUIBB B and GUIBB C

            uses singleton class ZCL_APPL1

        FPM application 2

            uses GUIBB B, GUIBB D and GUIBB E

            uses singleton class…. –> If I want to reuse GUIBB B than I’m forced to use singleton class ZCL_APPL1 (since GUIBB B is connected to this singleton class)

        Regards,

        Jeroen

        (0) 
        1. Ashish Shah Post author

          Hi Jeroen,

          I would say you still have a choice.

          You can use singleton class ZCL_APPL1 for GUIBB A , GUIBB B ,GUIBB C.

          and

          you can use singleton class ZCL_APPL2 for GUIBB B , GUIBB D , GUIBB E.

          in these two different singleton class you can store the different values that you may want to share between different GUIBBs.

          Let me know if you still need more info.

          Regards,

          Ashish Shah

          Regards,

          Ashish Shah

          (0) 
          1. Jeroen Custers

            Hi Ashish,

            Thanks for your reply. How do you handle the singleton class in the above example with GUIBB B (used in both scenarios; ZCL_APPL1 for scenario 1 and ZCL_APPL2 for scenario 2)? Where to choose which singleton class should be used?

            Regards,

            Jeroen

            (0) 
  6. Bhaskar Tripathi

    Hi Ashish,

    Since the class is singleton, why we have to declare GREF_OBJ as static attribute?

    Also, correct me if I am wrong: Using Class constructor, the implementation will change to –

    Method CLASS_CONSTRUCTOR

        CREATE OBJECT gref_obj TYPE zash_singleton_test.

    ENDMETHOD.


    Method GET_INSTANCE.

         ref_obj = gref_obj.

    ENDMETHOD.


    Best Regards,

    Bhaskar

    (0) 
    1. Suhas Saha

      Since the class is singleton, why we have to declare GREF_OBJ as static attribute?

      Because you cannot use an instance component in a static method 😐

      (0) 

Leave a Reply