Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Singleton Class

Former Member
0 Kudos
83

HI,

Can any one give me code of singleton class.

Regards

Vinay

1 ACCEPTED SOLUTION

former_member183804
Active Contributor
0 Kudos
47

program pattern_singleton.


***********************************************************************
* Singleton
* =========
*   Intent
*   ------
*   Ensure a class has only one instance, and provide a global point
*   of access to it.
***********************************************************************

class lcl_Singleton definition create private.

  public section.

    class-methods:
      get_Instance returning value(Result) type ref to lcl_Singleton.

  private section.
    class-data:
      fg_Singleton type ref to lcl_Singleton.

endclass.



class lcl_Singleton implementation.

method get_Instance.
  if ( fg_Singleton is initial ).
    create object fg_Singleton.
  endif.
  Result = fg_Singleton.
endmethod.

endclass.
4 REPLIES 4

Former Member
0 Kudos
47

Hello Vinay,

Could you specify what the context is ? A singleton class, as I understand, is the one which has only one instance per session.

Now, do you want to create one class? For what purpose?

Regards,

Anand Mandalika.

ChristianFi
Active Participant
0 Kudos
47

create a class with definition create private to make sure that no instance can be created from outside of the class.

define a static attribute wich holds the instance.

create the instance in a class method or in the class constructor and only if the static attribute is initial.

Christian

former_member183804
Active Contributor
0 Kudos
48

program pattern_singleton.


***********************************************************************
* Singleton
* =========
*   Intent
*   ------
*   Ensure a class has only one instance, and provide a global point
*   of access to it.
***********************************************************************

class lcl_Singleton definition create private.

  public section.

    class-methods:
      get_Instance returning value(Result) type ref to lcl_Singleton.

  private section.
    class-data:
      fg_Singleton type ref to lcl_Singleton.

endclass.



class lcl_Singleton implementation.

method get_Instance.
  if ( fg_Singleton is initial ).
    create object fg_Singleton.
  endif.
  Result = fg_Singleton.
endmethod.

endclass.

0 Kudos
47

Thanks for the code, Klaus.

Regards,

Subramanian V.