메뉴 건너뛰기

SAP 한국 커뮤니티

OO Style 코딩의 이해

양키(이경환) 2014.04.01 05:42 조회 수 : 4555



기본적인 Basic 코딩 스타일에서 OO 스타일의 코딩 습관은

필요하다고 생각되어집니다.


아래 비교 소스를 보고 가볍게 코딩 스타일을 이해하시면 될 것 같습니다.


Traditional way of writing the code:

 

REPORT  ysdnblog_classic.
PARAMETERS : p_rows TYPE count DEFAULT '100'.
START-OF-SELECTION.
  DATA : it_mara TYPE STANDARD TABLE OF mara.
  PERFORM get_data CHANGING it_mara.
  PERFORM display USING it_mara.
*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
FORM get_data  CHANGING ch_mara TYPE mara_tt.
  SELECT * FROM mara INTO TABLE ch_mara  UP TO p_rows ROWS .
ENDFORM.                    " GET_DATA
*&---------------------------------------------------------------------*
*&      Form  DISPLAY
*&---------------------------------------------------------------------*
FORM display  USING    i_mara TYPE mara_tt.
  DATA : lr_table TYPE REF TO cl_salv_table.
  cl_salv_table=>factoryIMPORTING    r_salv_table   = lr_table
                           CHANGING     t_table        =   i_mara )    .
  lr_table->display( ).
ENDFORM.                    " DISPLAY


OO way (only using Static methods)


 

REPORT  ysdnblog_class_static.
PARAMETERS : p_rows TYPE count DEFAULT '100'.
*----------------------------------------------------------------------*
*       CLASS lcl_main DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_main DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS : get_data  ,
                    display.
  PRIVATE SECTION.
    CLASS-DATA it_mara TYPE mara_tt.
ENDCLASS.                    "lcl_main DEFINITION
*----------------------------------------------------------------------*
*       CLASS lcl_main IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_main IMPLEMENTATION.
  METHOD get_data.
    SELECT * FROM mara INTO TABLE lcl_main=>it_mara  UP TO p_rows ROWS .
  ENDMETHOD.                    "GET_DATA
  METHOD display.
    DATA : lr_table TYPE REF TO cl_salv_table.
    cl_salv_table=>factoryIMPORTING    r_salv_table   = lr_table
                             CHANGING     t_table        =   lcl_main=>it_mara  )    .
    lr_table->display( ).
  ENDMETHOD.                    "display
ENDCLASS.                    "lcl_main IMPLEMENTATION

START-OF-SELECTION.

  lcl_main=>get_data( ).

  lcl_main=>display( ).

 


 

 

OO way (Only Instance methods)

 

 

REPORT  ysdnblog_class_instance.
PARAMETERS : p_rows TYPE count DEFAULT '100'.
*----------------------------------------------------------------------*
*       CLASS lcl_main DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_main DEFINITION.
  PUBLIC SECTION.
    METHODS :   get_data  ,
                display.
  PRIVATE SECTION.
    DATA it_mara TYPE mara_tt.
ENDCLASS.                    "lcl_main DEFINITION
*----------------------------------------------------------------------*
*       CLASS lcl_main IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_main IMPLEMENTATION.
  METHOD get_data.
    SELECT * FROM mara INTO TABLE me->it_mara  UP TO P_rows ROWS .
  ENDMETHOD.                    "GET_DATA
  METHOD display.
    DATA : lr_table TYPE REF TO cl_salv_table.
    cl_salv_table=>factoryIMPORTING    r_salv_table   = lr_table
                             CHANGING     t_table        =   me->it_mara  )    .
    lr_table->display( ).
  ENDMETHOD.                    "display
ENDCLASS.                    "lcl_main IMPLEMENTATION

START-OF-SELECTION.

data : lr_main TYPE REF TO lcl_main.

create OBJECT lr_main.
  lr_main->get_data( ).
  lr_main->display( ).

 

I

 

My IDEAL way in OO


REPORT  ysdnblog_class_ideal.
parameters : p_rows type count default '100'.
*----------------------------------------------------------------------*
*       CLASS lcl_main DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_main DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS : start.
  PRIVATE SECTION.
    METHODS : get_data  ,
              display.
    CLASS-DATA : lr_main TYPE REF TO lcl_main.
    DATA it_mara TYPE mara_tt.
ENDCLASS.                    "lcl_main DEFINITION
*----------------------------------------------------------------------*
*       CLASS lcl_main IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_main IMPLEMENTATION.
  METHOD start.
    CREATE OBJECT lr_main.
    lr_main->get_data( ).
    lr_main->display( ).
  ENDMETHOD.                    "start
  METHOD get_data.
    SELECT * FROM mara INTO TABLE me->it_mara  UP TO P_rows ROWS .
  ENDMETHOD.                    "GET_DATA
  METHOD display.
    DATA : lr_table TYPE REF TO cl_salv_table.
    cl_salv_table=>factoryIMPORTING    r_salv_table   = lr_table
    CHANGING     t_table        =   me->it_mara  )    .
    lr_table->display( ).
  ENDMETHOD.                    "display
ENDCLASS.                    "lcl_main IMPLEMENTATION

 

START-OF-SELECTION.

 

  lcl_main=>start( ).

 

 

New Trend of and completely moving your report to OO:

 


REPORT  ysdnblog_class_new.

 


SELECTION-SCREEN : BEGIN OF SCREEN 200.
PARAMETERS p_rows TYPE count DEFAULT '100'.
SELECTION-SCREEN : END OF SCREEN 200.

 

*----------------------------------------------------------------------*
*       CLASS lcl_main DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_main DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS : start.

 

  PRIVATE SECTION.
    METHODS : get_data  ,
              display.
    CLASS-DATA : lr_main TYPE REF TO lcl_main.
    DATA it_mara TYPE mara_tt.
ENDCLASS.                    "lcl_main DEFINITION
*----------------------------------------------------------------------*
*       CLASS lcl_main IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_main IMPLEMENTATION.
  METHOD start.
BREAK-POINT.
    CALL SELECTION-SCREEN 200.
    IF sy-subrc IS INITIAL.

 

      CREATE OBJECT lr_main.
      lr_main->get_data( ).
      lr_main->display( ).
    ENDIF.

 

  ENDMETHOD.                    "start
  METHOD get_data.
    SELECT * FROM mara INTO TABLE me->it_mara  UP TO p_rows ROWS .
  ENDMETHOD.                    "GET_DATA
  METHOD display.
    DATA : lr_table TYPE REF TO cl_salv_table.
    cl_salv_table=>factoryIMPORTING    r_salv_table   = lr_table
    CHANGING     t_table        =   me->it_mara  )    .
    lr_table->display( ).
  ENDMETHOD.                    "display
ENDCLASS.                    "lcl_main IMPLEMENTATION

 

START-OF-SELECTION.

 

  lcl_main=>start( ).