Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member196098
Participant

Hello guys

I am trying to get a hand of the patterns in ABAP and how to apply them.

I hope this is going to be one of the many posts i will do on the Patterns

As we write more code things get so much similar and what i learnt from reading Java design Patterns

Keep your code open for extension and closed for modification as much as possible!!

MVC Design Pattern

In Model view controller the main goal is:

to be able to loose couple the presentation layer from the backend layer!!

  • View is how i get input from user and display output
  • Model is my Business data
  • Controller is the manager between these two

There is always two components in a ABAP report:

Lets say an ALV  report

  • Selection screen(Presentation layer)
  • Data layer( Fetching of the data-SQL queries etcc-Model layer)
  • Displaying the data(View Layer)

So we could divide the code into 3 pieces Model-View-Controller

Controller might not be very necessary for small reports.

having controller is a good approach as this will be like a mediator between the model and the view

So what are the don'ts:

  • View should never know much about the model
  • Model should never be referencing the vie

Here is an example:

Lets say that you write a report and this report users should be able to display the data as an ALV or as a smartforms or even in pdf format!!

This is a great example of MVC as the view layer will be changing like a skin as most of the apps use these days even a web browser

Here is the code sample

Lets start with the example ALV that will display sales order data from tables vbak and vbap

VIEW

Define an abstract view class where you can extend it to many different types of views!!

***Display

class lcl_view DEFINITION ABSTRACT .

  PUBLIC SECTION.

    METHODS: display  ABSTRACT CHANGING it_data TYPE STANDARD TABLE.

ENDCLASS.

class lcl_view_alv  DEFINITION INHERITING FROM lcl_view .

  PUBLIC SECTION.

    METHODS: display REDEFINITION.

endclass.

****Smartforms view

CLASS lcl_view_smartforms DEFINITION INHERITING FROM lcl_view.

  PUBLIC SECTION.

    METHODS: display REDEFINITION.

ENDCLASS.

****PDF view

CLASS lcl_view_pdf DEFINITION INHERITING FROM lcl_view.

  PUBLIC SECTION.

    METHODS: display REDEFINITION.

ENDCLASS.

So now we have 3 diffrent types of views!!

MODEL

Model is the business data that we will be getting

We could also make model an abstract class and then extend it but for simplicity not now!!

here is an example:

*****DATA Layer

class lcl_model DEFINITION.

  PUBLIC SECTION.

    TYPES: BEGIN OF ty_out,

      vbeln type vbap-vbeln,

      posnr type vbap-posnr,

      matnr type vbap-matnr,

      vkorg type vbak-vkorg,

      END OF ty_out

      .

    TYPES: tt_out TYPE STANDARD TABLE OF ty_out.

    TYPES: gr_vbeln TYPE RANGE OF vbap-vbeln.

    DATA: gt_output TYPE tt_out.

    methods: constructor,

              select_data IMPORTING VALUE(rs_vbeln) TYPE gr_vbeln.

    .

  ENDclass.

CONTROLLER

Controller needs to know about both the view and the model

so that it can notify

The responsibility is with the controller to manage the tasks !!

class lcl_controller DEFINITION.

  PUBLIC SECTION.

    METHODS: constructor IMPORTING io_view_type TYPE CLIKE OPTIONAL.

    methods: get_data IMPORTING ir_vbeln TYPE lcl_model=>GR_VBELN.

    methods: set_view IMPORTING io_view_type TYPE REF TO lcl_view.

    methods: main IMPORTING  ir_vbeln TYPE lcl_model=>GR_VBELN

                              VALUE(iv_view_type) TYPE string.

    methods: display.

  PROTECTED SECTION.

    DATA: lo_view type REF TO lcl_view.

    DATA: lo_model type REF TO lcl_model.

ENDCLASS.

SUMMARY

You need to define a loose coupled application and in the future when you need a new View all you need is write a new one extending the abstract view class!!

MVC is a great pattern and design and get out there to try as much as you can!!

All the code i provide is attached see below

Sample code: is in text format as well as Saplink format can be provided if you request on <email address removed by moderator> as a nugget!!

i couldnt attach the nugget into this failed somehow!!