转载自 By
In this post, we will see how we can implement the Views which will access the Controller and model which is encapsulated in the controller. This post is in continuation of previous post: ABAP Objects Design Patterns – Model View Controller (MVC) Part 2.
First Demo Application – ALV
For our first Application view will be ALV output. To get the data for the ALV into the application, we will use the reference of the MODEL class created in the Controller. This way our model class is entrily separated by the view.
Code Snippet for View 1: ALV of MVC design
*&---------------------------------------------------------------------* *& Purpose - ABAP Design Patterns - Model View Controller MVC - ALV demo *& Author - Naimesh Patel *& URL - http://zevolving.com/?p=52 *&---------------------------------------------------------------------* * REPORT ztest_mvc_alv. * START-OF-SELECTION. *--------- * Controller *--------- DATA: lo_control TYPE REF TO zcl_control. * * Iniiate controller CREATE OBJECT lo_control. * * Get the object from Control CALL METHOD lo_control->get_object EXPORTING if_name = 'ZCL_MODEL'. * *--------- * Model - Business Logic *--------- * Date Range DATA: r_erdat TYPE RANGE OF vbak-erdat, la_erdat LIKE LINE OF r_erdat. * la_erdat-SIGN = 'I'. la_erdat-OPTION = 'BT'. la_erdat-LOW = sy-datum - 10. la_erdat-HIGH = sy-datum. APPEND la_erdat TO r_erdat. * * Get data method CALL METHOD lo_control->o_model->get_data EXPORTING ir_erdat = r_erdat. * *--------- * View - ALV output *--------- DATA: lo_alv TYPE REF TO cl_salv_table. * DATA: lx_msg TYPE REF TO cx_salv_msg. TRY. cl_salv_table=>factory( IMPORTING r_salv_table = lo_alv CHANGING t_table = lo_control->o_model->t_vbak ). CATCH cx_salv_msg INTO lx_msg. ENDTRY. * * * Displaying the ALV lo_alv->display( ).
First Demo Application – SmartForms Output
Our Second application is fairly simple once we have implemented the first application as our core logic of getting the data is out of the report. In this application only part which differs is calling the Smartform.
Code Snippet for View 2: Smartforms of MVC design
*&---------------------------------------------------------------------* *& Purpose - ABAP Design Patterns - Model View Controller MVC - *& SmartForms Demo *& Author - Naimesh Patel *& URL - http://zevolving.com/?p=52 *&---------------------------------------------------------------------* * REPORT ztest_mvc_view_2_ssf. * START-OF-SELECTION. *--------- * Controller *--------- DATA: lo_control TYPE REF TO zcl_control. * * Iniiate controller CREATE OBJECT lo_control. * * Get the object from Control CALL METHOD lo_control->get_object EXPORTING if_name = 'ZCL_MODEL'. * *--------- * Model - Business Logic *--------- * Date Range DATA: r_erdat TYPE RANGE OF vbak-erdat, la_erdat LIKE LINE OF r_erdat. * la_erdat-SIGN = 'I'. la_erdat-OPTION = 'BT'. la_erdat-LOW = sy-datum - 10. la_erdat-HIGH = sy-datum. APPEND la_erdat TO r_erdat. * * Get data method CALL METHOD lo_control->o_model->get_data EXPORTING ir_erdat = r_erdat. * *--------- * View - Smartform Output *--------- * Smartform FM DATA: l_form TYPE tdsfname VALUE 'ZTEST_MVC_VIEW_2', l_fm TYPE rs38l_fnam. * CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME' EXPORTING formname = l_form IMPORTING fm_name = l_fm EXCEPTIONS no_form = 1 no_function_module = 2 OTHERS = 3. * * calling Smartform FM DATA: ls_control TYPE ssfctrlop. " Controlling info DATA: ls_composer TYPE ssfcompop. " Output info * CALL FUNCTION l_fm EXPORTING control_parameters = ls_control output_options = ls_composer user_settings = ' ' t_vbak = lo_control->o_model->t_vbak EXCEPTIONS formatting_error = 1 internal_error = 2 send_error = 3 user_canceled = 4 OTHERS = 5. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF.
Let’s say, in future we enhanced the business logic model class and now we want to implement for the business. In this case, we just have to change the object reference created in the Controller and we are good to go. Obviously, we have to take care of the newly created methods or methods which parameters are enhanced.
Related Links:
ABAP Objects Design Patterns