May 18

Singleton Pattern – OOABAP

The concept of restricting the instantiation of the a class to only one object is called Singleton. This is useful when exactly one object is needed to coordinate actions across the system.

Read about Singleton Pattern

SingleObject class provides a static method to get its static instance to outside world. SingletonPatternDemo, our demo class will use SingleObject class to get a SingleObject object.

2017-05-18 11_11_12-Design Patterns Singleton Pattern.jpg

Let’s try this pattern in ABAP program:

2017-05-18 11_04_21- - Remote Desktop Connection2017-05-18 11_04_45- - Remote Desktop Connection

I have retrieved the singleton instance, set a value to private attribute, retrieved it back and showed it to the user..

2017-05-18 11_05_07- - Remote Desktop Connection

If I try to get instance again, it returns the existing one..2017-05-18 11_05_27- - Remote Desktop Connection

After executing the report, below will be output:

2017-05-18 11_13_53- - Remote Desktop Connection.jpg