Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

When we do the development using the ABAP objects than we need to make sure that we are using the right design patterns.

Design patterns is the patterns which we are using while design and coding of the application.

Singleton design pattern is used when we need to make sure that, application will be able to create only and ONLY one instance of the Class. We can implement the Singleton design patterns in the ABAP by using the CLASS-DATA.

Follow this link

3 Comments

  1. Guest

    In my opinion, this is not the correct implmentation of the singleton pattern. Normally you would like to reuse the instantiation of a singletion in several places by calling the get_instance() method. Therefore you set the visibility of the class to private:

      CLASS lcl_singleton DEFINITION CREATE PRIVATE.

    and create a public class-method get_instance(), which returns the reference to itself:

      METHOD get_instance.

        IF ref IS INITIAL.

          CREATE OBJECT ref.

        ENDIF.

        returning_param = ref.

      ENDMETHOD.

    The example in the link only prevents to instantiate the class several times, but if you have a program, which uses several classes, you have to hand over the instance from the first instantiation point to all other classes. With the example above, you could access the instance inside whole program.

     Full example:http://en.wikipedia.org/wiki/Singleton_pattern 

  2. Guest

    Volker is right. The mentioned webpage shows a worng implementation.

    That code does not even construct a singelton since the instantiation of "lcl_application"  is not private.

    If you invoke

    CREATE OBJECT lo_2nd_apps.
    

    you will get a new instance. This instance will be different from the class attribute "lo_apps". Thus, the singleton is not a singelton!

  3. Guest

    This error was corrected. I think now the page realy show an example of singleton design pattern in ABAP.