top of page

ABAP Singleton Pattern

The singleton pattern logic aims to ensure that, during the execution of our program, only one instance has access to the relevant object. To elaborate a bit more; If we have a control class and we do not want it to create a new instance in ABAP every time a 'perform' is called or a function is invoked, we can apply this method. This way, we can use the same instance throughout the entire runtime. In other object-oriented languages, it is generally used to access data through the same instance during the entire runtime by setting it once for database access.


In ABAP usage, my personal preference is to use this method for controlling objects and integrations to originate from the same point. For example; web service usage. Because of the power of ABAP, you may say that when our program is called, it creates a global instance, and I can set it when the program is initialized. When you want to use this instance in the function you call, you will realize the need for a static class. Thus, during the runtime process, you won't have to carry around the instance you created with the singleton, with extra parameters from place to place.


In the example below, I created a static-private value, assigned a value to it within the program, and tried to get the value from the function I called, and the static class returned the same value to me with the same data structure. 


singleton template

Based on this example, if it’s an object access instead of a variable, we would have utilized this “singleton” template.


Disadvantages; You may encounter problems when multiple instances are needed, for example, if you have a vehicle class and you want to manage it with an internal table, in such a case, I would suggest using the factory method like in “SALV”, so you can control the data without creating an “instance”.


You may experience issues with inheritance when using singleton with your “instance”.

design pattern

Example Codes, 


Süleyman Çelik

F&R EWM ECC S/4 HANA ABAP-OO Developer / FIORI / Solution Architect 

47 views0 comments

Recent Posts

See All

Комментарии


bottom of page