Vous êtes sur la page 1sur 9

Practical Use of OOPs ABAP – Model View Controller (MVC).

Part I

The subject (ABAP Objects) I would like to share with you here today might have already been
discussed numerous times in different forums and blogs, but still, I decided to write this blog
because I want to present my case with a real time SAP Business example. Trust me, I will not
waste your time with Vehicle Class examples (remember cars, trucks etc) or Animal Class
examples. My focus today would be a real project program with a selection screen, data

fetching, and display.

Being a traditional ABAP developer I was actively participating in the tug of war between the
ABAP traditional approach and ABAP object oriented approach. The question is therefore NOT
about using ABAP Objects or not. It is about knowing When to use it and How to use it. Along
with these questions, another question strikes in mind ‘How to utilize MVC Design Pattern in
ABAP? Well, I am still working on this question to ascertain the best solution, especially when
screen design comes into the picture.

Let’s describe the Complex MVC (Model View Controller) in simple words. ‘Isolate
the business logicfrom the user interface to provide the flexibility to change business logic
independently‘. Wow!! We have finished complex MVC in just one sentence. Now let’s see how
we can decouple the logic and what are the upside of this approach.

The advantage of using this approach is to reuse the existing logic in multiple similar
requirements. Let’s say we have a sales order report to prepare. We can design
our Model (business logic classes) in such a way that the same Class (model) can be used
for creating the different views like report, smart forms. This approach gives us the flexibility to
reuse that logic as it is part of the Model class.

Also Read: Just 4 Versions of the same program to understand OOPs ABAP

Here is a simple report to present a real time practical scenario.

PS: This is Part 1 of the 3 Parts series on MVC Concept.

Scenario 1:

Create a report to display the Sales Documents (sales order, quotation,


contract) based on Plant.

Let’s design our Selection screen.


To follow MVC pattern, I have created three separate local classes.

 Selection screen CL_SEL


 Fetch Data CL_FETCH(Act as MODEL)
 Display data CL_ALV (Act as VIEW).

1. Class for Selection Screen – CL_SEL:

We have one method here GET_SCREEN. This method will get the data from the selection
screen.
This class CL_SEL will just get the data from selection parameters to make it available for our
next class CL_FETCH. (All the validations we can perform in class CL_SEL, let’s say Plant
validations, which we will cover it in next part)

2. Class for Database Fetch – CL_FETCH:

This class has two methods CONSTRUCTOR and FETCH_DATA.

CONSTRUCTOR: contain the ref of the selection class CL_SEL.

FETCH_DATA: will select the data from DB table.


Now if you notice here in CL_FETCH class we have created object of CL_SEL class, as the
objective is to decouple as much as possible so we are just passing the reference in Constructor.

Q: Why in the constructor?

Ans: Simply because constructor method will get called automatically when the object is created.

Now if you see our FETCH_DATA method we are fetching the records based on our selection
screen parameters which are in another class (Magic of using Object).

3. Class for Display – CL_ALV:

This class is acting as the View and we have two methods here CONSTRUCTOR and
DISPLAY_ALV.

CONSTRUCTOR: contain the ref of class CL_FETCH.

DISPLAY_ALV: will display the data using SALV table FACTORY method.
Now we have created the Object and we are good to go for calling our Methods (so our dish is
almost ready, we are simply garnishing our dish).
Do you Know? How to convert an existing ABAP Report to OOPs ABAP?

Did you Notice, we are using all the events of ABAP report?

This is a very simple report to demonstrate the concept of MVC so I am using local classes but it
is always recommended to use global classes (i.e. class created in t-code SE24). One more
reason behind local classes; we lazy ABAPers are very good in copy and paste options, so this is
an extra tip for you.

Why global classes?

Ans: Being lazy ABAPers, we often don’t listen to our Experts. I did the same (ignored global
class and used my local class in the program). After some time, I got another requirement. Using
the same input parameters, develop another report which will send our forms in PDF format to
our special Customers. Then I realized the power of MVC and global classes …. as yet gaining
from my oversights.

Here is the fresh hot DISH from the oven i.e. our First Real OOPs Program.

1
2 DATA : lv_vbeln TYPE vbap-vbeln .
3 * Create selection screen
4 SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-000.
5 PARAMETERS : p_werks TYPE vbap-werks OBLIGATORY.
6 SELECT-OPTIONS : s_vbeln1 FOR lv_vbeln .
7 SELECTION-SCREEN END OF BLOCK b1 .
8
9 * Create selection screen class
10 CLASS cl_sel DEFINITION FINAL .
11
12 PUBLIC SECTION .
13 TYPES : t_vbeln TYPE RANGE OF vbeln .
14 DATA : s_vbeln TYPE t_vbeln .
15 DATA : s_werks TYPE werks_ext .
16 METHODS : get_screen IMPORTING lp_werks TYPE werks_ext
17 ls_vbeln TYPE t_vbeln .
18 ENDCLASS .
19 *&---------------------------------------------------------------------*
20 *& CLASS (IMPLEMENTATION) SEL
21 *&---------------------------------------------------------------------*
22 * Get Selection Screen Information
23 *----------------------------------------------------------------------*
24 CLASS cl_sel IMPLEMENTATION.
25
26 METHOD get_screen .
27 me->s_werks = lp_werks.
28 me->s_vbeln = ls_vbeln[] .
29 ENDMETHOD .
30
31 ENDCLASS. "SEL
32
33 * Create DATA MODEL to fetch recordes
34 CLASS cl_fetch DEFINITION .
35
36 PUBLIC SECTION .
37 DATA : sel_obj TYPE REF TO cl_sel .
38 DATA : it_vbap TYPE STANDARD TABLE OF vbap .
39
40 METHODS constructor IMPORTING ref_sel TYPE REF TO cl_sel .
41 METHODS : fetch_data .
42
43 ENDCLASS .
44 *&---------------------------------------------------------------------*
45 *& CLASS (IMPLEMENTATION) FETCH
46 *&---------------------------------------------------------------------*
47 * Fetch Sales Info
48 *----------------------------------------------------------------------*
49 CLASS cl_fetch IMPLEMENTATION.
50
51 METHOD constructor.
52 me->sel_obj = ref_sel .
53 ENDMETHOD .
54
55 METHOD fetch_data .
56 SELECT * FROM vbap INTO TABLE me->it_vbap UP TO 10 ROWS WHERE vbeln
57 IN me->sel_obj->s_vbeln AND werks EQ me->sel_obj->s_werks .
58 ENDMETHOD .
59
60 ENDCLASS. "FETCH
61
62 * Display data class
63 CLASS cl_alv DEFINITION .
64
65 PUBLIC SECTION .
66 DATA : fetch_obj TYPE REF TO cl_fetch .
67 METHODS : constructor IMPORTING ref_fetch TYPE REF TO cl_fetch.
68 METHODS : display_alv .
69
70 ENDCLASS .
71 *&---------------------------------------------------------------------*
72 *& Class (Implementation) CL_ALV
73 *&---------------------------------------------------------------------*
74 * Display the Output
75 *----------------------------------------------------------------------*
76 CLASS cl_alv IMPLEMENTATION.
77
78 METHOD constructor .
79 me->fetch_obj = ref_fetch .
80 ENDMETHOD .
81
82 METHOD display_alv .
83
84 DATA: lx_msg TYPE REF TO cx_salv_msg.
85 DATA: o_alv TYPE REF TO cl_salv_table.
86
87 TRY.
88 cl_salv_table=>factory(
89 IMPORTING
90 r_salv_table = o_alv
91 CHANGING
92 t_table = me->fetch_obj->it_vbap ).
93 CATCH cx_salv_msg INTO lx_msg.
94 ENDTRY.
95
96 o_alv->display( ).
97
98 ENDMETHOD.
99
100 ENDCLASS. "CL_ALV
101
102 * Declare TYPE REF TO class objects
103 DATA: o_sel TYPE REF TO cl_sel,
104 o_fetch TYPE REF TO cl_fetch,
105 o_display TYPE REF TO cl_alv.
106
107 INITIALIZATION .
108 * Creating Objects of the Class
109 CREATE OBJECT : o_sel,
110 o_fetch EXPORTING ref_sel = o_sel, " ref_sel is in Constructor
111 o_display EXPORTING ref_fetch = o_fetch. " ref_fetch is in Constructor
112
113 START-OF-SELECTION .
114 * Import screen data to class screen
115 o_sel->get_screen( EXPORTING lp_werks = p_werks ls_vbeln = s_vbeln1[] ) .
116
117 * Call fetch data to fetch the records
118 o_fetch->fetch_data( ).
119
120 END-OF-SELECTION .
121 * Display data
122 o_display->display_alv( ).
Want to learn HANA ABAP? ABAP on SAP HANA. Part I. First Program in ABAP
HANA

Do you really think End-User will agree with this report?

Obviously No! They will request for a tasteful design (better user interface in the output
View) with huge numbers of options/alternatives in it (like print, sort, download etc). So let’s
cover them in our next article, how we can modify the layout (View) without changing the data
class (Model). Please stay tuned.

Vous aimerez peut-être aussi