Tuesday, January 03, 2012

OO ABAP - Singleton class

*&---------------------------------------------------------------------*
*& Report  YSAP_OOABAP_SINGLETON_CLASS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ysap_ooabap_singleton_class.

TABLES: pa0002.

PARAMETERS: pernr TYPE pa0002-pernr.

DATABEGIN OF struct,
      pernr TYPE pa0002-pernr,
      vorna TYPE pa0002-vorna,
      nachn TYPE pa0002-nachn,
      END OF struct.

DATA: itab LIKE TABLE OF struct ,
      wa LIKE struct.

*----------------------------------------------------------------------*
*       CLASS class1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS class1 DEFINITION CREATE PRIVATE.
  PUBLIC SECTION.
    METHODS : method1.
    CLASS-METHODS : method2.
ENDCLASS"c1 DEFINITION

*----------------------------------------------------------------------*
* CLASS c1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS class1 IMPLEMENTATION.
  METHOD method1.
    SELECT * FROM pa0002 INTO CORRESPONDING FIELDS OF TABLE itab WHERE pernr EQ pernr.
    LOOP AT itab INTO wa.
      WRITE: / 'personnel number:', wa-pernr, 'First name:', wa-vorna,'Last name',  wa-nachn.
    ENDLOOP.
  ENDMETHOD"m1

  METHOD method2.
    DATA : object TYPE REF TO class1.
    CREATE OBJECT : object.
    CALL METHOD : object->method1.
  ENDMETHOD"m2

ENDCLASS"c1 IMPLEMENTATION

START-OF-SELECTION.
  CALL METHOD : class1=>method2.
  data: object1 type ref to class1.
  create object object1.
  call method : object1->method1.


Compilation error:
 

Comment these 3 lines we can get the OUTPUT.


 



OUTPUT 


 

 

2 comments:

  1. The Following example is good to understand 'singleton':

    A 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.

    Counter class’s definition and implementation is as follows,

    REPORT ZFAR_SINGLETON.

    class counter definition create private. "Rule 1

    public section.

    class-methods:
    get_counter returning value(obj) type ref to counter. "Rule 3

    methods:
    increment,
    get_count returning value(cnt) type i.

    private section.
    class-data instance type ref to counter. "Rule 2
    data count type i.

    endclass.

    class counter implementation.

    method get_counter.
    if instance is initial.
    create object instance.
    endif.
    obj = instance.
    endmethod.

    method increment.
    add 1 to count.
    endmethod.

    method get_count.
    cnt = count.
    endmethod.


    endclass.

    start-of-selection.

    data: ctr1 type ref to counter,
    ctr2 type ref to counter,
    num1 type i,
    num2 type i.

    ctr1 = counter=>get_counter( ).
    ctr1->increment( ).
    ctr2 = counter=>get_counter( ).
    ctr2->increment( ).

    num1 = ctr1->get_count( ).
    num2 = ctr2->get_count( ).

    write: num1, num2.

    Since there is only one instance exists, both the references point to the same object and operates on the same object. Use Singleton Pattern whenever possible to ensure good design of the system.

    ReplyDelete
  2. Thank You Gopi its very nice example to understand

    ReplyDelete