Vous êtes sur la page 1sur 9

ABAP Objects Design Patterns

Model View Controller (MVC) Part


1
Today we will discuss about the Design Pattern: Model-View-Controller, which is also very
famous by its abbriviation MVC. By definition, MVC is to isolate the business logic from the
User Interface which gives great degree of flexibility to change business logic independently
to the user interface and vice versa.

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.

UML for MVC


UML diagram of the typical MVC application would be:

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.

Advantages of using MVC:


Since both our business logic and view logic are different, we can easily change any of the
logic without interrupting the other part.
In the next post, we will see how we can implement the MVC design pattern in ABAP usin
the ABAP Objects.

ABAP Objects Design Patterns Model View Controller (MVC) Part 2

ABAP Objects Design Patterns Model View Controller (MVC) Part 3

ABAP Objects Design Patterns


Model View Controller (MVC) Part
2
In this post, we will see how we can implement the MVC (Model-View-Controller) design
pattern in ABAP using the Objects. If you have not read the previous discussion about
MVC: ABAP Objects Design Patterns Model View Controller (MVC) Part 1, than I strongly
recommond to read that before moving forward.

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:

Model Class Setup


Model Method Definition

Model Attribute definition

Code Snippet of method GET_DATA


This code would be implemented in the method GET_DATA
* Parameters
* Importing IR_ERDAT TYPE TPMY_R_DATE Ranges for date
*
METHOD get_data.
*
* Get data and save into attribute T_VBAK
SELECT * FROM vbak
INTO TABLE t_vbak
WHERE erdat IN ir_erdat.
*
*
ENDMETHOD.

Controller Class Setup


Our controller class ZCL_CONTROL will have a method GET_OBJECT which will give us
an object of the model class. We require a public attribute which can refer to the object
created in the method GET_OBJECT.

Controller Method definition:

Controller Attributes definition:

Code Snippet for method GET_OBJECT:


* Parameters
* Importing IF_NAME TYPE CHAR30 Model class name
*
METHOD get_object .
*
DATA: lo_object TYPE REF TO OBJECT.
*
* Generic object reference to importing class
CREATE OBJECT lo_object TYPE (if_name).
IF sy-subrc = 0.
*
Downcasting to assign generic object to O_MODEL
o_model ?= lo_object.
ENDIF.
*
ENDMETHOD.

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:

ABAP Objects Design Patterns Model View Controller (MVC) Part 1

ABAP Objects Design Patterns Model View Controller (MVC) Part 3

ABAP Objects Design Patterns


Model View Controller (MVC) Part
3
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.

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.

Vous aimerez peut-être aussi