cancel
Showing results for 
Search instead for 
Did you mean: 

Singleton in OOps ABAP

VijayCR
Active Contributor
0 Kudos

Hello Experts,

I understand singleton means we can create only one instance of a class.But what exactly is the use of the singleton in OOPS.Can you provide me how to implement in OOPS.

Thnaks and Regards,

Vijay.

Accepted Solutions (1)

Accepted Solutions (1)

former_member229979
Participant

Vijay,

Singleton can be explained in following scenario:

You have an application where an employee of the company is logging in to perform some Employee specific operations. So, whenever he logs in, an employee related instance is created. There in, only 1 instance should be valid at a single point in time. This is possible via singleton class.

Hope this helps.

VijayCR
Active Contributor
0 Kudos

Hi Megha,

Thank you very much for the information.I wanted to know how can we implement in our SAP ABAP Using OOPS.Can you please provide me the steps and the model code.

Thanks,

Vijay,

Former Member
0 Kudos

Below are the main steps:

1. Create a class which can only be privetly instantiated.

2. Create a static private attribute in class refering to class it self. (say mo_single)

3. Create a static public methos with returning parameter as object reference point to this class.

4. Inside the static method

a. first check if mo_single is not bound then instantiate the object mo_single and assign to returning paramter.

b. if mo_single is bound then no need to instantiate just return the object reference.

One more thing - unlike many other programming languages singleton object in ABAP is only per session. if you execute the same program in another session you will get a new instance. just like global variable of FG.

Edited by: Gaurav B. on Jan 12, 2012 10:21 AM

VijayCR
Active Contributor
0 Kudos

Hi Gaurav,

Thanks for the information.It was very helpful.Please let me also know how to call a private class from the se38 report.I tried calling the method but i am getting an error 'Method "GET_SINGTON" is unknown or PROTECTED or PRIVATE.'Let me know how i can solve this issue.And where exactly the singleton property is actually checked.

Thanks,

Vijay.

Former Member
0 Kudos

Method GET_SINGTON should be static and should be defined as public. you should be able to call it with class name using scope resolution operator. e.g.

call method zcl_singleton=>GET_SINGTON( ).

There is no such syntax to make a class singleton in ABAP. all you need to manage programatically. Just to eloberate ... when you create a class by setting property Instantiation = Private that mean nowhere outside the class CREATE OBJECT can be used to instantiate the call.

Now you need a way to instantiate the class. as instance methods can not used without instantiation we create a static method and make it public so that i can be called out side the class.

Then inside this method we instantiate the class. But if you can not use instance variables in static method you we declare a class attribute as static and make it private so that it not visible to outside world.

Now using the private static variable in the public static method we instantiate the class, but you need to make sure that your create object statement in this static method is executed only once the session life time so you first check if that private instance variable is already bound that mean in some previous call you would have instantiated and then you just return the object reference stored in that private static variable else you instantiate and return.

Edited by: Gaurav B. on Jan 12, 2012 11:07 AM

former_member184578
Active Contributor
0 Kudos

Hi,

Try this,

1.Create a class ZCL_SINGLETON_TEST and select instantiation type private.

2.Now, Create a Static private attribyte lr_ref type ref to ZCL_SINGLETON_TEST.

3.Create one public static method, get_instance with returning parameter r_ref type ref to ZCL_SINGLETON_TEST.

In method implementation write the below code,

method get_instance.
if lr_ref is initial.
* as this is static instance only first time instance will be created.
create object lr_ref.
endif.

*Return the reference.
r_ref = lr_ref.
endmethod.

Now ,In your report call like this, as you cannot create object for private class.

data lo_ref type ref to ZCL_SINGLETON_TEST.
* get instance of single ton class
lo_ref = ZCL_SINGLETON_TEST=>get_instance( ).

Now using the lo_ref instance you can access the methods of single ton class.

Hope this helps u.,

revert if you need some more clarifications,

Thanks & Regards,

Kiran.

VijayCR
Active Contributor
0 Kudos

Hello gaurav,

Hello Kiran,

Thank you for the information.I am still not exactly able to get the prominence of the singleton in the businees processes like where exactly this functionality is used.

Thanks,

Vijay.

Edited by: vijaysimha_cr on Jan 13, 2012 6:03 AM

former_member184578
Active Contributor
0 Kudos

Hi Vijay,

The Singleton pattern is used in the design of logger classes. This classes are ussualy implemented as a singletons, and provides a global logging access point in all the application components without being necessary to create an object each time a logging operations is performed.

also check the comments in this blog [Real World Apps using Singleton|http://stackoverflow.com/questions/733842/what-are-the-real-world-applications-of-singleton-pattern]

in summary we use singletons where an object was going to be used many times over the life of the application, required initialization, to reduce memory and required only single instance.

Hope this helps u.,

Thanks & Regards,

Kiran.

Former Member

Answers (2)

Answers (2)

Former Member
0 Kudos

This message was moderated.

Former Member
0 Kudos

Hi Vijay,

I have written a blog post regarding single pattern. check out

Regards,

Fareez