Monday, December 6, 2010

Singleton class concept in OO abap

Some times we have requirement that class can be instantiated only once or if i rephrase it , it will be we don't want that anybody else can create the object of the class if i have already created one and how can we achieve this functionality?? Please check below :

  • First goto SE24 and create a class with instantiation as PRIVATE.
  • Now create a constructor visibility as PRIVATE so that nobody can create instantiate the class.
  • Now create a STATIC and public method GET_INSTANCE and write the below code .
           IF gref_obj is INITIAL.

                    CREATE OBJECT gref_obj.
           ENDIF.

  • We created above method as static so that we need not to instantiate the class as static method can be directly called from class reference.


Thanks and Regards
Rahul Gupta

No comments: