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
- SIngleton Pattern on Wikipedia
- ABAP Objects Design Patterns: Singleton for more information (Note: outside SDN link)
- ABAP Objects Design Patterns Singleton Usage with Class setup & UML Diagram (Note: outside SDN link)
3 Comments
Unknown User (py25qzl)
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
Patrick Mueller
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
you will get a new instance. This instance will be different from the class attribute "lo_apps". Thus, the singleton is not a singelton!
Unknown User (vw1f1oc)
This error was corrected. I think now the page realy show an example of singleton design pattern in ABAP.