Skip to Content
Author's profile photo Karthikeyan Ravikumar

Singleton pattern for ABAP Consumer proxy example scneario

We may have studied Singleton pattern in theory and where it can be used. I came across and interesting application of it and hence want to share my experience with the community and hence this post.

I was developing an adapter to integrated SAP with a third party software using SOAP webservices. The webservice provider was the third party software and therefore the WSDL was available to me. This had to be consumed by SAP. Hence, when this file was used to create a Consumer proxy in SAP (Under Enterprise services in SE80), a class is automatically generated. In order to make the webservice call, the method under this class needs to be called using an object created TYPE REF TO this class.

Now, since this call was needed to be made from multiple places in the code, from different user exits based on the requirement, this object needed to be made available in all those places.
One approach is to instantiate the object in each of those places. However, that is very inefficient in terms of memory management as it is going to create multiple instances of the same object that is needed. Also, for our purposes, there was no data that is different in each of those instances. It was just used to call the method to make the webservice call.
Hence, having a single object was the most efficient way to handle memory and it also ensured that in the future all instance data, if needed, is global and accessible.

In order to achieve this, I used the singleton pattern.
According to this design pattern, we instantiate the object inside a public method called GET_INSTANCE of the same class that we need to call. Inside this method, we check if the object is already instantiated. If it is, then we just pass that instance as the output. If not, we instantiate and pass that instance as the output. And wherever the instance of the object is needed, we just call this method and store the instance that is output in a local object of this class. This way, the same instance is used everywhere along with access to the same data globally.

Sample pseudo-code:

lo_class is a public static attribute of the class.
ro_class is a public static attribute of the method type ref to the same class.


IF lo_class IS INITIAL.
CATCH <objects referring to system,application fault and exception class defined in WSDL>

ro_class = lo_class.

In order to get the instance, the method simply needs to be called:

Define object go_class in the place it needs to be used and then simply declare:
go_class = class=>get_instance( ).

This will get the existing instance or create new instance and get that if not already created.

Hope this gives a practical view of the concept of singleton pattern. Let me know in the comments if you have had the need to use this concept and why.

Happy coding. Cheers! 🙂

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.