Sometimes in the real-time business scenarios, we need to have only one instance for a class at a point of time, for this one we have the concept of singleton in ABAP objects.
Singleton pattern : is the one of the simplest design patterns which involves only one class which instantiates itself to make sure that it creates one instance.Singleton ensues that it has only one instance and provides global point of access to the object.
Example - Logger Classes
The Singleton pattern is used in the design of logger classes. This classes are usually 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.
Steps to be followed to create singleton class
- Create a private class.
- Add a private attribute with reference to the same class.
- Create a public static method with returning value of type reference to same class.
- Create implementation and create object in the implementation of public static method.
- Call static method in any program to create instance for the singleton class.