0% found this document useful (0 votes)
16 views9 pages

Design Patterns in OO ABAP

The document discusses various design patterns in Object-Oriented ABAP, including Singleton, Factory, Decorator, Observer, Facade, and Proxy patterns. Each pattern is explained with its use cases, benefits, and example implementations in ABAP. The document serves as a guide for SAP Technical Consultants to apply these patterns effectively in their projects.

Uploaded by

mohamedsuhaib.sn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views9 pages

Design Patterns in OO ABAP

The document discusses various design patterns in Object-Oriented ABAP, including Singleton, Factory, Decorator, Observer, Facade, and Proxy patterns. Each pattern is explained with its use cases, benefits, and example implementations in ABAP. The document serves as a guide for SAP Technical Consultants to apply these patterns effectively in their projects.

Uploaded by

mohamedsuhaib.sn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Abid Ali

SAP Technical Consultant

Design
Patterns
In OO ABAP
Abid Ali
SAP Technical Consultant

Singleton Pattern

The Singleton Pattern ensures that only one instance of a


class exists throughout the program.

When to Use?
✅ Database Connection Management – Prevents multiple
unnecessary connections.
✅ Logging Framework – Ensures centralized logging.
✅ Global Configuration Settings – Stores system-wide
configuration.
✅ Cache Management – Avoids repeated calculations by
storing results.
Abid Ali
SAP Technical Consultant

Singleton Pattern
CLASS zcl_db_connection DEFINITION CREATE PRIVATE.
PUBLIC SECTION.
CLASS-METHODS get_instance RETURNING VALUE(ro_instance)
TYPE REF TO zcl_db_connection.
METHODS execute_query RETURNING VALUE(rt_data) TYPE TABLE
OF mara.
PRIVATE SECTION.
CLASS-DATA: go_instance TYPE REF TO zcl_db_connection.
ENDCLASS.

CLASS zcl_db_connection IMPLEMENTATION.


METHOD get_instance.
IF go_instance IS INITIAL.
CREATE OBJECT go_instance.
ENDIF.
ro_instance = go_instance.
ENDMETHOD.

METHOD execute_query.
SELECT * FROM mara INTO TABLE rt_data UP TO 10 ROWS.
ENDMETHOD.
ENDCLASS.

🔹 Why Singleton Here?


•Ensures one database connection is used throughout the program.
•Prevents unnecessary object creation, improving performance.
Abid Ali
SAP Technical Consultant

Factory Pattern

The Factory Pattern provides a centralized way to create


objects dynamically without specifying their concrete class.

When to Use?
✅ When you need multiple object instances dynamically
✅ When the object type is unknown at compile-time
✅ When object creation logic is complex
✅ When you want to decouple object creation from
implementation

Example: Factory for Material Object Creation


CLASS zcl_material_factory DEFINITION.
PUBLIC SECTION.
CLASS-METHODS create_material RETURNING VALUE(ro_material) TYPE REF TO
zif_material.
ENDCLASS.

CLASS zcl_material_factory IMPLEMENTATION.


METHOD create_material.
CREATE OBJECT ro_material TYPE zcl_material.
ENDMETHOD.
ENDCLASS.

Why Factory Here?


✅ Allows dynamic creation of different material types.
✅ Hides complex object creation logic inside the factory.
✅ Helps switch implementations without modifying existing code.
Abid Ali
SAP Technical Consultant

Decorator Pattern

Adds extra functionality to an object dynamically without


modifying its structure.

📌 Use Case: Sales Order Pricing Calculation


✅ Add different pricing components (e.g., discount, tax,
shipping) dynamically to a sales order.

CLASS zcl_base_price DEFINITION.


PUBLIC SECTION.
METHODS get_price RETURNING VALUE(rv_price) TYPE p DECIMALS 2.
ENDCLASS.

CLASS zcl_discount DEFINITION INHERITING FROM zcl_base_price.


PUBLIC SECTION.
METHODS get_price REDEFINITION.
ENDCLASS.

CLASS zcl_discount IMPLEMENTATION.


METHOD get_price.
rv_price = super->get_price( ) - 10. " Apply Discount
ENDMETHOD.
ENDCLASS.

✅ Sales orders can be extended with different pricing


rules dynamically.
Abid Ali
SAP Technical Consultant

Observer Pattern

✅ When one object changes state, multiple dependent


objects get notified automatically.

📌 Use Case: Stock Update Notification for Multiple Systems


✅ When stock is updated in SAP MM, SD, WM, and FI should
automatically get notified.
INTERFACE zif_observer.
METHODS update.
ENDINTERFACE.

CLASS zcl_stock_manager DEFINITION.


PUBLIC SECTION.
DATA observers TYPE TABLE OF REF TO zif_observer.
METHODS add_observer IMPORTING io_observer TYPE REF TO zif_observer.
METHODS notify_observers.
ENDCLASS.

CLASS zcl_stock_manager IMPLEMENTATION.


METHOD add_observer.
APPEND io_observer TO observers.
ENDMETHOD.

METHOD notify_observers.
LOOP AT observers INTO DATA(lo_observer).
lo_observer->update( ).
ENDLOOP.
ENDMETHOD.
ENDCLASS.

✅ Now, all systems linked to stock updates get


automatically notified.
Abid Ali
SAP Technical Consultant

Facade Pattern

✅ Provides a simplified interface to a complex system.

📌 Use Case: Simplifying Order Processing (SD, FI, MM


Integration)

✅ Instead of calling multiple SAP modules separately, we create


a single entry point.
CLASS zcl_order_facade DEFINITION.
PUBLIC SECTION.
METHODS process_order.
ENDCLASS.

CLASS zcl_order_facade IMPLEMENTATION.


METHOD process_order.
DATA(lo_sd) = NEW zcl_sd_process( ).
DATA(lo_mm) = NEW zcl_mm_process( ).
DATA(lo_fi) = NEW zcl_fi_process( ).

lo_sd->create_sales_order( ).
lo_mm->check_inventory( ).
lo_fi->create_invoice( ).
ENDMETHOD.
ENDCLASS.

✅ One method call processes the entire order instead of


multiple function calls.
Abid Ali
SAP Technical Consultant

Proxy Pattern

✅ Acts as a placeholder for another object to control access.

📌 Use Case: Restrict Access to Sensitive SAP Data (e.g.,


Salary Info)
CLASS zcl_salary_proxy DEFINITION.
PUBLIC SECTION.
METHODS get_salary IMPORTING iv_user TYPE string RETURNING
VALUE(rv_salary) TYPE p DECIMALS 2.
ENDCLASS.

CLASS zcl_salary_proxy IMPLEMENTATION.


METHOD get_salary.
IF iv_user = 'HR_MANAGER'.
rv_salary = 5000. " Allowed
ELSE.
rv_salary = 0. " Restricted
ENDIF.
ENDMETHOD.
ENDCLASS.

✅ Now, salary details are restricted based on roles.


Abid Ali
SAP Technical Consultant

Thank You
& Keep
Reposting

SAVE AND
SHARE

You might also like