0% found this document useful (0 votes)
67 views4 pages

Singleton Class

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views4 pages

Singleton Class

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Singleton Class

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. These classes are usually
implemented as singletons, and provide a global logging access point in all the
application components without being necessary to create an object each time a logging
operation is performed.
Follow below steps to create singleton class in Object oriented ABAP
Step 1: Create a private class in Se24
Go to SE24, provide name as ZSAPN_CL_SINGLETON and click on create.

Provide description, select class as private and save.


Go to attributes tab, add a private attribute of type reference to same class.

Go to methods tab, add a static method INSTANTIATE and click on parameters button.

Add a returning parameter as below.


Go back to methods, double click on method INSTANTIATE and add below code.

METHOD INSTANTIATE.

IF LV_INST IS NOT BOUND.


*-- Create object reference
LV_INST = NEW #( ).
ENDIF.
*-- Pass the reference
RO_INST = LV_INST.

ENDMETHOD.

Save and activate the class, go to SE38, create a program


ZSAPN_SINGLETON_GLOBAL.
Try to create instance directly.

REPORT ZSAPN_SINGLETON_GLOBAL.

DATA : LO_CLASS TYPE REF TO ZCL_SAPN_SINGLETON.

create OBJECT lo_class.

It will through an error.

Now create instance through static method.

REPORT ZSAPN_SINGLETON_GLOBAL.
DATA : LO_CLASS TYPE REF TO ZCL_SAPN_SINGLETON.
LO_CLASS = ZCL_SAPN_SINGLETON=>INSTANTIATE( ).
Now you can access all public instance components through the object.

You might also like