Skip to main content

Module 30: ABAP Design Patterns

Design patterns provide proven solutions to recurring design problems.
In ABAP, patterns help achieve maintainability, testability, and clean architecture, especially in large enterprise systems.


1. Why Design Patterns in ABAP?

Without patterns:

  • Tight coupling
  • Hard-to-test code
  • Massive procedural classes
  • Difficult maintenance
Clean ABAP Philosophy

Good ABAP is not about writing more code — it’s about structuring responsibility correctly.


2. Singleton Pattern

Purpose

Ensure only one instance of a class exists.


Use Cases

  • Configuration readers
  • Application settings
  • Central caches
  • Logger classes

ABAP Implementation (Modern)

CLASS zcl_app_config DEFINITION
PUBLIC
FINAL
CREATE PRIVATE.

PUBLIC SECTION.
CLASS-METHODS get_instance
RETURNING VALUE(ro_instance) TYPE REF TO zcl_app_config.

PRIVATE SECTION.
CLASS-DATA go_instance TYPE REF TO zcl_app_config.
ENDCLASS.
CLASS zcl_app_config IMPLEMENTATION.

METHOD get_instance.
ro_instance = COND #(
WHEN go_instance IS BOUND THEN go_instance
ELSE NEW zcl_app_config( ) ).
go_instance = ro_instance.
ENDMETHOD.

ENDCLASS.
Interview Insight

Singleton is useful, but overuse leads to hidden dependencies.

3. Factory Pattern

Purpose Encapsulate object creation logic.

Use Cases

  • Different implementations
  • Runtime decision making
  • Decoupling instantiation

Example Scenario Create different tax calculators based on country.

Interface

INTERFACE zif_tax_calc.
METHODS calculate
IMPORTING iv_amount TYPE p
RETURNING VALUE(rv_tax) TYPE p.
ENDINTERFACE.

Factory Class

CLASS zcl_tax_factory DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.

PUBLIC SECTION.
CLASS-METHODS get_calculator
IMPORTING iv_country TYPE land1
RETURNING VALUE(ro_calc) TYPE REF TO zif_tax_calc.
ENDCLASS.

CLASS zcl_tax_factory IMPLEMENTATION.

METHOD get_calculator.
ro_calc = SWITCH #( iv_country
WHEN 'IN' THEN NEW zcl_tax_india( )
WHEN 'US' THEN NEW zcl_tax_usa( )
ELSE NEW zcl_tax_default( ) ).
ENDMETHOD.

ENDCLASS.
Benefit

Factory removes IF/CASE clutter from business logic.

4. Strategy Pattern

Purpose Select behavior at runtime, not implementation.

Use Cases

  • Pricing rules
  • Discount logic
  • Validation rules

Strategy Example

DATA(lo_strategy) = zcl_tax_factory=>get_calculator( iv_country ).
DATA(lv_tax) = lo_strategy->calculate( iv_amount ).
Difference from Factory
  • Factory → creates objects

  • Strategy → uses interchangeable behavior

5. MVC in ABAP (Very Important)

What is MVC?

MVC separates:

  • Model → Business logic & data

  • View → UI (ALV, UI5)

  • Controller → Flow control

MVC in Classical / OO ALV

Controller

Model (Data & Logic)

View (ALV / UI)

Example Mapping

MVC ComponentABAP Example
ModelData provider class
ViewALV class (SALV / CL_GUI_ALV)
ControllerReport or application class
Clean ABAP Rule

Never mix SELECT logic inside ALV display code.

6. Clean ABAP Principles (Must-Know)

Clean ABAP is SAP’s official coding guideline.

6.1 Core Clean ABAP Rules

Follow These
  • Small methods (do one thing)

  • Meaningful names

  • Avoid deep nesting

  • Prefer expressions over statements

  • Avoid global state

6.2 Example: Legacy vs Clean

❌ Legacy

IF sy-subrc = 0.
IF lv_flag = 'X'.
IF lv_count > 10.
...
ENDIF.
ENDIF.
ENDIF.

✅ Clean

CHECK sy-subrc = 0.
CHECK lv_flag = abap_true.
CHECK lv_count > 10.

6.3 Dependency Inversion (ABAP View)

❌ Bad

DATA(lo_calc) = NEW zcl_tax_india( ).

✅ Good

DATA(lo_calc) TYPE REF TO zif_tax_calc.
lo_calc = zcl_tax_factory=>get_calculator( iv_country ).

7. When NOT to Use Patterns

Avoid Pattern Abuse
  • Don’t introduce patterns for trivial logic

  • Don’t create factories with only one implementation

  • Keep code readable

8. Interview-Grade Explanation

Q: Which design patterns do you commonly use in ABAP?

Answer:

I frequently use Factory and Strategy to decouple logic, Singleton for controlled shared resources, MVC for reports and UI separation, and follow Clean ABAP principles to keep code readable and testable.

This Answer Scores High

Shows practical pattern usage, not textbook recitation.

9. Summary

  • Singleton controls instance lifecycle
  • Factory decouples object creation
  • Strategy enables runtime behavior selection
  • MVC separates concerns
  • Clean ABAP ensures maintainability
  • Patterns must serve clarity, not complexity

10. Practice Exercises

  • Refactor procedural code using Factory.
  • Implement Strategy for pricing logic.
  • Split a report into MVC layers.
  • Identify Singleton candidates.
  • Clean up a legacy method using Clean ABAP rules.

11. What’s Next?

➡️ Module 31: Testing & Quality

Learning Tip

Patterns don’t make you senior — knowing when to use them does.