ABAP Objects Design Patterns
ABAP Objects Design Patterns
Basics of MVC
In MVC, User Interface acts as the View; Business logic acts as the Model and the link
which provides the link between the View and Model is known as the Controller. In real time
business, MVC is used anywhere, where users have a choice to select his or her view. For
example, Windows Media Player, Gmail or Yahoo mail and so on. All these type of
application provides us the option to select the skin as per our choice.
Lets take an example: We have to develop one Sales report for our corporate intranet. This
report must have the option to display the sales data in the classical report format; different
charts pie, bar, line or both report & chart and it must be based on the Users choice.
This type of requirements where we can easily separate the Business logic and the views
are the best candidates for MVC.
Generally, Model sends the data to controller and controller will pass that data to Views and
views will display the data as per their nature. In SAP, our business logic(model) will not
send data unless and until View request for an data because ABAP is event driven
language. Like user has to run some transaction to get the data, means application view
has to initiate the process and ask for the data from the Model. In this requesting process,
Controller will help the view to be apart from the model.
As we can see here, Model will send the data to the Controller and controller will pass that
information to the View. Now, its a views responsibility to create a user specific view
Report, or Chart.
Demo Application
To implement the MVC, we will create two applications One will generate an output in ALV
and other will generate an output Smartforms. We will put our business logic in the MODEL
class. We will create one CONTROL class to establish control between Model and Views.
Our business logic for this example is fairly simple select the Sales Orders from the VBAK
which were created in last ten days. The public method GET_DATA in the class
ZCL_MODEL will act as the business logic. Additionally, this class has the public attribute
T_VBAK which will be set by the GET_DATA and hold our data from the table.
UML
The UML diagram for any of the application would be like:
In the next post we will see, how we will use the controller class in our view and access the
business logic encapsulated in Model class.
Related Links:
*
*--------* 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( ).
*& 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
Lets 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
ABAP Object Design Patterns: Singleton (or file )
ABAP Objects Design Patterns Model View Controller (MVC) Part 1
ABAP Objects Design Patterns Model View Controller (MVC) Part 2.