SINGLETON CLASS CONCEPT:
We all know the basic and theortical aspects,but now we are going to look at the practical and process behind.
use:
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.
class which implements Singleton Pattern should have the following things other than the class’s own data members and methods,
1. Private constructor(s)
2. A private static member of the same type of the class.
3. A static method which returns the only instance.
.*&---------------------------------------------------------------------*
*& Report ZSINGLETON
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZSINGLETON.
data: it_mara TYPE TABLE OF mara.
data: wa_mara type mara.
class zsingle DEFINITION CREATE PRIVATE.
PUBLIC SECTION.
METHODS : GET_MARA.
CLASS-METHODS :ZINSTANT RETURNING VALUE(LR_INS) TYPE REF TO ZSINGLE.
PRIVATE SECTION.
CLASS-DATA:LR_INS TYPE REF TO ZSINGLE.
ENDCLASS.
CLASS ZSINGLE IMPLEMENTATION.
METHOD GET_MARA.
SELECT * FROM MARA INTO TABLE IT_MARA UP TO 30 ROWS.
LOOP AT IT_MARA INTO WA_MARA .
WRITE :/ WA_MARA-MATNR, WA_MARA-MTART,WA_MARA-MEINS.
ENDLOOP.
ENDMETHOD.
METHOD ZINSTANT.
IF LR_INS IS INITIAL.
CREATE OBJECT LR_INS.
ENDIF.
ENDMETHOD.
ENDCLASS.
*********************************************************************************************
START-OF-SELECTION.
DATA: LO_CLAS TYPE REF TO ZSINGLE.
LO_CLAS = ZSINGLE=>ZINSTANT( ).
LO_CLAS->GET_MARA( ).