首页 > 代码库 > ABAP OO与ALV结合方式探索(1)

ABAP OO与ALV结合方式探索(1)

用OO来开发,尤其是在复杂业务的开发过程中

从程序设计的角度而言,应该更简单一点

而ALV是二次开发中登场很高的一个控件

最近做了一些尝试,探索OO的代码和ALV的结合使用

 

使用控件型的ALV

ALV直接写在BO对象中

本质上,只是用OO的语法来重写了一下form的调用方式

初步实现了OO和ALV的使用

 

TYPES:  BEGIN OF ts_bo,    datum TYPE sy-datum,    uzeit TYPE sy-uzeit,    value TYPE string,  END OF ts_bo,  tt_bo TYPE ts_bo OCCURS 0.CLASS lcl_bo DEFINITION.  PUBLIC SECTION.    METHODS:      mock,"模拟业务操作      alv_config," alv 显示参数      alv_display."alv  PRIVATE SECTION.    DATA:      gt_bo       TYPE        tt_bo,      gt_fieldcat TYPE        lvc_t_fcat," 显示字段列表      gs_layout   TYPE        lvc_s_layo , " 页面布局      g_container TYPE REF TO cl_gui_custom_container, " 容器      g_grid      TYPE REF TO cl_gui_alv_grid. "  显示GridENDCLASS.CLASS lcl_bo IMPLEMENTATION.  METHOD mock.    DATA: ls_bo TYPE ts_bo.    DO 10 TIMES.      ls_bo-datum = sy-datum + sy-index.      ls_bo-uzeit = sy-uzeit.      ls_bo-value = http://www.mamicode.com/Times: && sy-index.      APPEND ls_bo TO gt_bo.CLEAR ls_bo.    ENDDO.  ENDMETHOD.  METHOD alv_config.    DATA:      ls_fieldcat TYPE lvc_s_fcat.    ls_fieldcat-fieldname = DATUM.ls_fieldcat-coltext = 日期. APPEND ls_fieldcat TO gt_fieldcat.CLEAR ls_fieldcat.    ls_fieldcat-fieldname = UZEIT.ls_fieldcat-coltext = 时间. APPEND ls_fieldcat TO gt_fieldcat.CLEAR ls_fieldcat.    ls_fieldcat-fieldname = VALUE.ls_fieldcat-coltext = 内容. APPEND ls_fieldcat TO gt_fieldcat.CLEAR ls_fieldcat.    gs_layout-sel_mode = A.    gs_layout-zebra = X .    gs_layout-cwidth_opt = X.  ENDMETHOD.  METHOD alv_display.    IF g_grid IS INITIAL.      CREATE OBJECT g_container        EXPORTING          container_name = C0100.      CREATE OBJECT g_grid        EXPORTING          i_parent = g_container.      CALL METHOD g_grid->set_table_for_first_display        EXPORTING*         I_BUFFER_ACTIVE               =*         I_CONSISTENCY_CHECK           =*         I_STRUCTURE_NAME              =*         is_variant                    = gs_variant          i_save                        = A          i_default                     = X*         is_layout                     = gs_layout*         IS_PRINT                      =*         IT_SPECIAL_GROUPS             =*         it_toolbar_excluding          = gt_exclude*         IT_HYPERLINK                  =        CHANGING          it_outtab                     = gt_bo          it_fieldcatalog               = gt_fieldcat*         it_sort                       = gt_sort*         IT_FILTER                     =        EXCEPTIONS          invalid_parameter_combination = 1          program_error                 = 2          too_many_lines                = 3          OTHERS                        = 4.    ELSE.      CALL METHOD g_grid->refresh_table_display.    ENDIF.  ENDMETHOD.                    "displayENDCLASS.DATA:  ok_code TYPE        sy-ucomm,  go_bo   TYPE REF TO lcl_bo.START-OF-SELECTION.  CREATE OBJECT go_bo.  go_bo->mock( ).  go_bo->alv_config( ).  CALL SCREEN 100.MODULE status_0100 OUTPUT.  SET PF-STATUS PS100.  go_bo->alv_display( ).ENDMODULE.                 " STATUS_0100  OUTPUTMODULE user_command_0100 INPUT.  CASE ok_code.    WHEN BACK.      SET SCREEN 0.  ENDCASE.ENDMODULE.                 " USER_COMMAND_0100  INPUT

 

效果如下:

image

从代码上也能体现

method仅仅是替代了form的作用

并没有体现出OO的什么特性

如果一个程序中有多个ALV  多个复杂的对象

唯一的好处就是每个对象的数据独立了出来

比一股脑的全局数据要好一点

 

一个BO对象既要关注取数逻辑

又要关注显示功能

从设计模式中类的角度而言

无法满足单一职责的要求

显然这种代码的组织方式还可以改进

 

未完待续…

下一篇 数据与显示的分离

ABAP OO与ALV结合方式探索(1)