🔥 EARLY BIRD SPECIAL:Save 10% on all SAP Online Courses! (Limited Slots)

SAP ABAP Factory Method with Real-Time and ALV Examples

E
ERPVITS Team
Author
2026-04-28
8 min read
SAP ABAP Factory Method with Real-Time and ALV Examples

Factory Method in SAP ABAP: What Is Factory Method, ALV Factory Method & Examples

In the SAP ABAP development evolves from an object-oriented to procedural programming, design patterns have become essential for writing clean, reuseable and maintainable code. In this regard patterns, the factory technique in SAP ABAP is recognized among the best useful and widely utilized patterns especially when dealing with ALV grids reporting outputs, report outputs and the instantiation of objects.

If you're a novice ABAP developer just starting out with OOP concepts, or an experienced architect creating enterprise-level SAP applications, knowing how to use the SAP ABAP manufacturing process can sharpen your design skills and enhance your code base significantly.

In this article We'll discuss:

  • What is factory method? SAP ABAP?
  • How the factory method of design pattern operates
  • Factory method ALV from SAP ABAP using examples
  • Real-world scenarios and best practices

Let's dive into it.

What Is Factory Method in SAP ABAP?

Factory method: The factory technique is a design pattern that offers an interface for the creation of objects, without specifying the exact type of object to be created. instead of directly calling constructors with CREATE ORDER and NEW You delegate the creation of objects to a separate static method, which is typically known as a factory method.

In simpler terms, instead of being asked "how do I create this object?" Instead, you need a factory procedure "please give me the right object for my situation."

Why Does This Matter in ABAP?

In old ABAP the instantiation of objects was easy: you just needed to call CREATE the object. In more complicated OOP-based apps it is common to:

  • Different subclass instances are returned in accordance with the conditions at runtime
  • To hide the complexity of object construction from the user
  • Controlled in-built instantiation (e.g. Singleton like behavior)
  • Create objects that are decoupled from business logic

Its factory method of SAP ABAP resolves these issues elegantly.

Key Characteristics

  • It is usually an static method that is public of the class
  • It gives an object reference (object referent) that is a superclass, interface
  • The class that is created will differ based on the parameters input
  • The constructor is usually configured to Private to avoid the direct instantiation.

Factory Method Design Pattern -- Core Concept

Before we jump to ALV, let's look at the basic pattern using an easy example.

Scenario

If you've got different kinds of documents for delivery in SAP the standard delivery, returns deliveries, and transfer orders. Each one has its own class but all have an interface.

Step 1: Define an Interface

INTERFACE zif_delivery.
  METHODS: process.
ENDINTERFACE.

Step 2: Implement Concrete Classes

CLASS zcl_standard_delivery DEFINITION.
  PUBLIC SECTION.
    INTERFACES zif_delivery.
ENDCLASS.

CLASS zcl_standard_delivery IMPLEMENTATION.
  METHOD zif_delivery~process.
    WRITE: / 'Processing Standard Delivery'.
  ENDMETHOD.
ENDCLASS.

CLASS zcl_returns_delivery DEFINITION.
  PUBLIC SECTION.
    INTERFACES zif_delivery.
ENDCLASS.

CLASS zcl_returns_delivery IMPLEMENTATION.
  METHOD zif_delivery~process.
    WRITE: / 'Processing Returns Delivery'.
  ENDMETHOD.
ENDCLASS.

Step 3: Create the Factory Class

CLASS zcl_delivery_factory DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS: get_instance
      IMPORTING iv_type TYPE string
      RETURNING VALUE(ro_delivery) TYPE REF TO zif_delivery.
ENDCLASS.

CLASS zcl_delivery_factory IMPLEMENTATION.
  METHOD get_instance.
    CASE Iv_Type.
      WHEN 'STANDARD'.
        ro_delivery = NEW zcl_standard_delivery( ).
      WHEN 'RETURNS'.
        ro_delivery = NEW zcl_returns_delivery( ).
      ELSE.
        RAISE EXCEPTION TYPE cx_sy_create_object_error.
    ENDCASE.
  ENDMETHOD.
ENDCLASS.

Step 4: Call the Factory in Your Program

DATA: lo_delivery TYPE REF TO zif_delivery.
lo_delivery = zcl_delivery_factory=>get_instance( iv_type = 'STANDARD' ).
lo_delivery->process( ).

It's the SAP factory method of ABAP in its most basic form. The caller doesn't even know or care about the concrete class that is being used. It simply receives an object that is in compliance with the requirements of the interface contract.

Private Constructor Pattern

The most popular methods used by using the factory technique within SAP ABAP is that it's combined with the use of a private constructor. This means that nobody could directly create the class -- they have to use using the factory procedure.

CLASS zcl_report_engine DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS: get_instance
      RETURNING VALUE(ro_instance) TYPE REF TO zcl_report_engine.
    METHODS: execute.
  PRIVATE SECTION.
    CLASS-DATA: go_instance TYPE REF TO zcl_report_engine.
    METHODS: constructor.
ENDCLASS.

CLASS zcl_report_engine IMPLEMENTATION.
  METHOD get_instance.
    IF go_instance IS INITIAL.
      go_instance = NEW zcl_report_engine( ).
    ENDIF.
    ro_instance = go_instance.
  ENDMETHOD.

  METHOD constructor.
    " Initialization process here
  ENDMETHOD.

  METHOD execute.
    WRITE: / 'Report Engine Running'.
  ENDMETHOD.
ENDCLASS.

In this case, get_instance acts as a factory method and makes use of the singleton pattern. This is a popular and powerful combination used in the enterprise ABAP development.

ALV Factory Method in SAP ABAP

The most common and commonly utilized factory method in daily ABAP development is the ALV (ABAP List Viewer) programming. SAP offers an inbuilt factory method for developing ALV grid instances using CL_SALV_TABLE.

What Is the ALV Factory Method?

The ALV factory method in SAP ABAP refers to the static method CL_SALV_TABLE=>FACTORY( ). Instead of manually creating ALV instances and customizing each component by hand, you can call this factory method. It does the heavy lifting for the creation of objects, and returns fully set up CL_SALV_TABLE instance that is ready to display.

This is an excellent concrete example that shows the Factory Method pattern that is baked right into SAP's class library.

Basic ALV Factory Method Syntax

CL_SALV_TABLE=>FACTORY(
  IMPORTING r_salv_table = lo_alv
  CHANGING  t_table      = lt_data
).

Full Example: ALV Factory Method in SAP ABAP

REPORT zerpvits_alv_factory.

" Step 1: Define data types
TYPES: BEGIN OF ty_material,
         matnr TYPE mara-matnr,
         mtart TYPE mara-mtart,
         matkl TYPE mara-matkl,
         meins TYPE mara-meins,
       END OF ty_material.

DATA: lt_material  TYPE TABLE OF ty_material,
      lo_alv       TYPE REF TO cl_salv_table,
      lo_columns   TYPE REF TO cl_salv_columns_table,
      lo_column    TYPE REF TO cl_salv_column_table,
      lo_functions TYPE REF TO cl_salv_functions_list,
      lx_error     TYPE REF TO cx_salv_error.

" Step 2: Fetch data from database
SELECT matnr mtart matkl meins
  FROM mara
  INTO TABLE lt_material
  UP TO 100 ROWS.

" Step 3: Call the ALV factory method
TRY.
  CL_SALV_TABLE=>FACTORY(
    IMPORTING r_salv_table = lo_alv
    CHANGING  t_table      = lt_material
  ).

  " Step 4: Enable standard ALV toolbar functions
  lo_functions = lo_alv->get_functions( ).
  lo_functions->set_all( abap_true ).

  " Step 5: Configure columns
  lo_columns = lo_alv->get_columns( ).
  lo_columns->set_optimize( abap_true ).

  " Step 6: Set column headers
  lo_column ?= lo_columns->get_column( 'MATNR' ).
  lo_column->set_long_text( 'Material Number' ).

  lo_column ?= lo_columns->get_column( 'MTART' ).
  lo_column->set_long_text( 'Material Type' ).

  " Step 7: Display the ALV
  lo_alv->display( ).

CATCH cx_salv_error INTO lx_error.
  MESSAGE lx_error->get_text( ) TYPE 'E'.
ENDTRY.

This is professional-grade, clean ABAP code that uses ALV's factory method of ALV within SAP ABAP. Notice how CL_SALV_TABLE=>FACTORY( ) abstracts all the complexity of ALV object creation -- you simply pass your data table and get back a ready-to-use ALV object.

ALV Factory Method using Container (for Screens / Module Pools)

When embedding ALV in a screen, or your own container (common in module pool software) You must send the container's address in the factory method.

DATA: lo_container TYPE REF TO cl_gui_custom_container,
      lo_alv       TYPE REF TO cl_salv_table.

" Create custom container linked to screen element 'ALV_CONTAINER'
lo_container = NEW cl_gui_custom_container(
  container_name = 'ALV_CONTAINER'
).

" Call factory method with container
CL_SALV_TABLE=>FACTORY(
  EXPORTING r_container  = lo_container
  IMPORTING r_salv_table = lo_alv
  CHANGING  t_table      = lt_material
).

lo_alv->display( ).

Simply by passing the r_container this factory method can adapt the output of the ALV to display embedded on screen providing the flexibility the factory pattern offers.

Extending the Factory Pattern: Custom ALV Factory Class

For large-scale SAP projects, ERPVITS recommends wrapping CL_SALV_TABLE=>FACTORY inside your own custom factory class. This will give you a central location to apply the standard configurations to all ALV reports that you have in your project.

CLASS zcl_alv_factory DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS: create_alv
      CHANGING ct_data TYPE STANDARD TABLE
      RETURNING VALUE(ro_alv) TYPE REF TO cl_salv_table.
ENDCLASS.

CLASS zcl_alv_factory IMPLEMENTATION.
  METHOD create_alv.
    DATA: lx_error TYPE REF TO cx_salv_error.
    TRY.
      CL_SALV_TABLE=>FACTORY(
        IMPORTING r_salv_table = ro_alv
        CHANGING  t_table      = ct_data
      ).

      " Apply standard project settings
      ro_alv->get_functions( )->set_all( abap_true ).
      ro_alv->get_columns( )->set_optimize( abap_true ).
      ro_alv->get_display_settings( )->set_striped_pattern( abap_true ).

    CATCH cx_salv_error INTO lx_error.
      MESSAGE lx_error->get_text( ) TYPE 'E'.
    ENDTRY.
  ENDMETHOD.
ENDCLASS.

Any report that is in the system could be a call to:

lo_alv = zcl_alv_factory=>create_alv( CHANGING ct_data = lt_material ).
lo_alv->display( ).

This is a simple DRY (Don't repeat yourself) method to keep the ALV configuration of your project consistent throughout every project.

Factory Method vs. Direct Instantiation -- Key Differences

Aspect Direct Instantiation Factory Method
Control of object creation Caller selects class Factory determines the class
Flexibility Low -- closely linked High - loosely connected
Subclass substitution Manual code changes are required Made in factory
Constructor visibility Public Often private
Testability More difficult to mock/stub It is easier to practice mocking in unit tests
Code reuse Limited High - central logic

When Should You Use the Factory Method in SAP ABAP?

This pattern, known as the factory method is the best option for the following situations:

  • You must return various types of objects in response to the runtime conditions
  • You're trying to conceal complicated initialization logic from the person who is calling you
  • You're developing reusable frameworks, as well as toolkits (like the wrappers of ALV)
  • You should ensure controlled instantiation (e.g. there is no direct NEWs allowed)
  • You're working on dependency injection or unit-testing scenarios
  • You'd like to have only one point of change in the event that the object creation logic changes

Common Mistakes to Avoid

  • Forgetting to handle exceptions from CL_SALV_TABLE=>FACTORY -- always wrap in a TRY/CATCH block
  • The factory method should be made an instance method, instead of an static CLASS-METHOD factory methods must be able to be called without the need for an existing object
  • Incorrectly setting the constructor to private for the use of the pattern to create a controlled instantiation
  • The factory is overloaded with too many requirements -- especially if the CASE statement becomes too large think about using an alternative pattern for registry
  • Calling the function lo_alv->display( ) before configuring columns and functions -- make sure to have your ALV options set prior to display

Summary

This factory method that is used in SAP the ABAP language is among the most effective design patterns that you can add into the OOP toolkit. From an purely design pattern standpoint it dissociates the creation of objects from business logic and provides an adaptable, easily maintained code. In the practical world of SAP development, the ALV factory method in SAP ABAP via CL_SALV_TABLE=>FACTORY is something you'll encounter and use in virtually every modern ALV-based report.

Here's a quick summary of the topics we discussed:

  • What is the factory method in SAP ABAP -- a static method that generates and returns objects without providing the details of its construction
  • The factory method's core pattern, which includes concrete classes, interfaces, and an factory class
  • Private constructor pattern to control instantiation
  • ALV factory method in SAP ABAP using CL_SALV_TABLE=>FACTORY
  • Embedding ALV into containers employs the same factory procedure
  • Designing an ALV factory class that is custom-designed to ensure consistency across projects
  • What to avoid and when to be aware of

At ERPVITS, we specialize in helping SAP developers move from traditional procedural ABAP to clean, object-oriented, design-pattern-driven development. Explore our complete SAP ABAP tutorial series for additional guides about OOP designs, programming with ALV BAdIs, enhancements, as well as SAP HANA optimized ABAP.

Request More Info

Get expert guidance on your SAP career path.

0 + 0 = ?

By submitting, you agree to our privacy policy.