21/09/2010

Singleton Design Pattern


REPORT ZADN_SAMPLE.

*----------------------------------------------------------------------*
* CLASS cl_applicationstate DEFINITION
*----------------------------------------------------------------------*
CLASS cl_applicationstate DEFINITION CREATE PRIVATE.

PUBLIC SECTION.

DATA : hostname TYPE c LENGTH 128,
ip_addrs TYPE c LENGTH 15.

CLASS-METHODS : getstate EXPORTING p_instance TYPE REF TO cl_applicationstate.

PRIVATE SECTION.

METHODS : constructor.

CLASS-DATA : _instance TYPE REF TO cl_applicationstate.

ENDCLASS. "cl_ApplicationState DEFINITION


*----------------------------------------------------------------------*
* CLASS cl_applicationstate IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS cl_applicationstate IMPLEMENTATION.

METHOD constructor.

ENDMETHOD. "constructor

METHOD getstate.

IF _instance IS NOT BOUND.

CREATE OBJECT _instance.

p_instance = _instance.

ELSE.

p_instance = _instance.

ENDIF.

ENDMETHOD. "getstate

ENDCLASS. "cl_ApplicationState IMPLEMENTATION

START-OF-SELECTION.

DATA : o1_cl_applicationstate TYPE REF TO cl_applicationstate,
o2_cl_applicationstate TYPE REF TO cl_applicationstate.

cl_applicationstate=>getstate( IMPORTING p_instance = o1_cl_applicationstate ).

IF o1_cl_applicationstate IS BOUND.

o1_cl_applicationstate->hostname = 'LOCALHOST'.
o1_cl_applicationstate->ip_addrs = '127.0.0.1'.

ENDIF.

cl_applicationstate=>getstate( IMPORTING p_instance = o2_cl_applicationstate ).

WRITE / : o1_cl_applicationstate->hostname,
o1_cl_applicationstate->ip_addrs.

ULINE.

WRITE / : o2_cl_applicationstate->hostname,
o2_cl_applicationstate->ip_addrs.

ULINE.

IF o1_cl_applicationstate EQ o2_cl_applicationstate.

WRITE / 'Objetos são iguais'.

ENDIF.

Nenhum comentário:

Postar um comentário