A design pattern is a general, reusable solution to a frequently encountered problem in software design. Rather than being a ready-to-use design, a design pattern serves as a guideline or template for addressing specific issues, which can then be adapted and applied across various contexts.
There are numerous different design patterns to choose from. We will discuss few commonly used ones:
➢Singleton design pattern: The concept of ensuring a class has only a single instance is known as the Singleton pattern. As the name implies, this pattern restricts the creation of more than one instance of a class. It incorporates logic to prevent the application from generating additional instances beyond the initial one.
- The class guarantees that only one instance is created for each internal session.
- We can create static class reference object to hold the existing object reference and static method which will return us the object reference.
- The static methods (declared using CLASS-METHODS) of a class cannot be redefined in subclasses.

➢Factory design pattern: The factory pattern is a widely used design pattern that allows for the creation of object instances without revealing the specific details of the instantiation process. Instead, it provides a unified interface through which users can access these objects, effectively hiding the underlying implementation details.
- Create a superclass that includes a factory method.
- Create importing parameters.
- Create a subclass that inherits from the superclass and utilizes its features.
- Create an object reference of the superclass.
❖Example:
*&------------------------------------------------------------------------------*
* Title : Factory Design Pattern *
* Author : Sangeeta Singh *
*&------------------------------------------------------------------------------*
REPORT yrpt_factory_design.
“Data declarations
Data: lv_typ TYPE char3.
“Super class definition
CLASS lcl_vehicle DEFINITION ABSTRACT.
PUBLIC SECTION.
CLASS-METHODS: m_factory
IMPORTING iv_output_type TYPE lv_typ
RETURNING value(lo_obj) TYPE REF TO lcl_vehicle.
METHODS: m_print_vehicle ABSTRACT.
ENDCLASS.
“Subclass Definition
CLASS lcl_car DEFINITION INHERITING FROM lcl_vehicle.
PUBLIC SECTION.
METHODS: m_print_vehicle REDEFINITION.
ENDCLASS.
“Subclass Implementation
CLASS lcl_car IMPLEMENTATION.
METHOD m_print_vehicle.
WRITE: / ' I am car'.
ENDMETHOD.
ENDCLASS.
CLASS lcl_vehicle IMPLEMENTATION.
METHOD m_factory.
CASE iv_output_type.
WHEN 'CAR'.
CREATE OBJECT lo_obj TYPE lcl_car.
WHEN 'BUS'.
"create another object
WHEN OTHERS.
" raise exception
ENDCASE.
ENDMETHOD.
ENDCLASS.
“Super class definition
CLASS lcl_main DEFINITION.
PUBLIC SECTION.
CLASS-METHODS: run.
ENDCLASS.
“Subclass Implementation
CLASS lcl_main IMPLEMENTATION.
METHOD m_run.
DATA: lo_output TYPE REF TO lcl_vehicle.
lo_output = lcl_vehicle=>m_factory( 'CAR' ).
lo_output-> m_print_vehicle( ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
lcl_main=>m_run( ).
➢Model View Controller (MVC): In the MVC pattern, the User Interface serves as the View, the Business Logic functions as the Model, and the Controller acts as the intermediary that connects the View with the Model.
- The pattern requires that each of these be placed into separate objects.
- The MVC pattern is used in modern applications because it makes them scalable, maintainable, and easy to expand
❖Example:
- Create a global model class.

Model attribute definition:

Method GET_DATA code:

2. Create a global controller class.



3. Create ALV report with MVC design.
*&------------------------------------------------------------------------------* *& Report YRPT_MVC_ALV
*&------------------------------------------------------------------------------* * Purpose - OOPS ABAP Design Patterns - Model View Controller(MVC) *
* Author - Sangeeta Singh *
*&------------------------------------------------------------------------------*
REPORT yrpt_mvc_alv.
START-OF-SELECTION.
*--------- *
Controller
*---------
DATA: lo_control TYPE REF TO ycl_controller.
"Initiate controller
CREATE OBJECT lo_control.
"Get the object from Control
CALL METHOD lo_control->get_object
EXPORTING
iv_name = 'YCL_MODEL'.
*
*--------- *
Model - Business Logic
*---------
*Date Range
DATA: lv_erdat TYPE ekko-aedat.
"Get data method
CALL METHOD lo_control->go_model->get_data
EXPORTING
ir_aedat = lv_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->go_model->gt_ekko ).
CATCH cx_salv_msg INTO lx_msg.
ENDTRY.
"Displaying the ALV
lo_alv->display( ).
Thanks for visiting!
Sangeeta Singh

Leave a Reply