Application Development and Automation 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: 
Read only

Singleton Class

Former Member
0 Likes
674

HI,

Can any one give me code of singleton class.

Regards

Vinay

1 ACCEPTED SOLUTION
Read only

former_member183804
Active Contributor
0 Likes
638

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
Read only

Former Member
0 Likes
638

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.

Read only

ChristianFi
Active Participant
0 Likes
638

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

Read only

former_member183804
Active Contributor
0 Likes
639

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.
Read only

0 Likes
638

Thanks for the code, Klaus.

Regards,

Subramanian V.