ERPClimb logoERPClimb
OOP ABAP
ABAP Developmentadvanced 11 min read

OOP ABAP Part 8: Interfaces and Factory Methods

Preview · a free account unlocks the full lesson

Key takeaway

Create objects in one controlled place: static factory methods, CREATE PRIVATE, selecting an implementation from customizing, the singleton pattern, and how SAP itself uses factories (CL SALV TABLE= FACTORY, BAdI GET BADI).

In this lessonShow sections

Visual overview

OOP ABAP Part 8: Interfaces and Factory Methods — learning sheetFlow: Run the code example with countries IN and DE → Add a US class and extend only the factory → Replace the CASE with a dynamic class name and try an invalid name. Key ideas: WHY FACTORIES; STATIC FACTORY METHOD; CUSTOMIZING-DRIVEN SELECTION; SINGLETON. Watch out: Singletons keep state for the whole session — use them only for truly session-wide objects.ERPClimb.com / ABAP / LESSON 8/10 / LEARNING SHEETOOP ABAP Part 8: Interfaces and Factory Methods1 · HOW IT FLOWS1Run the code example withcountries IN and DE2Add a US class and extendonly the factory3Replace the CASE with adynamic class name andtry an invalid name2 · KEY IDEAS EXPLAINEDWhy FactoriesCallers depend only on the interface (ZIF_…).Adding an implementation changes only the factory.Static Factory MethodCLASS-METHODS create IMPORTING iv_country TYPEland1 RETURNING VALUE(ro_tax) TYPE REF TOzif_tax_calculator.Customizing-Driven SelectionInstead of a CASE, a Z-table maps country → classname. The factory reads it and uses dynamic CREATEOBJECT ro_tax TYPE (lv_classname). New countries…SingletonA class with CREATE PRIVATE, a private staticattribute go_instance and a static get_instance( )that creates it once. Typical for session-wide…3 · REMEMBER · WATCH OUTRememberA factory method creates and returns objects typed as an interface;callers never write CREATE OBJECT for the concrete class.The factory decides which implementation to create (by parameter,customizing or country).CREATE PRIVATE + static factory = the only way to get an instance(used for singletons).!Watch outSingletons keep state for the wholesession — use them only for trulysession-wide objects.4 · IN A REAL PROJECTA global rollout added country-specific e-invoicing: India, Germany and Mexico each needed different XMLformats. The first version had IF country = … branches in the output program, and every new country meantchanges and regression tests in shared code. The team introduced ZIF_EINVOICE_BUILDER, one class per country…Original ERPClimb learning sheet • Verify against your SAP release and edition

Explanation

Parts 3 and 7 of this series created implementations directly inside the report; real programs shouldn't know about every concrete class that way — a factory hides that decision instead. A factory method creates and returns an object typed as an interface, so callers never write CREATE OBJECT for the concrete class themselves, and the factory is the one that decides which implementation to create, based on a parameter, customizing, or something like country.

Factories earn their place for several reasons: callers depend only on the interface (ZIF_…); adding a new implementation changes only the factory, not the calling code; creation logic — validation, reading customizing, caching — lives in one place; and tests can make the factory return a test double instead, as covered in Part 9.

A static factory method typically looks like CLASS-METHODS create IMPORTING iv_country TYPE land1 RETURNING VALUE(ro_tax) TYPE REF TO zif_tax_calculator., with the body doing something like CASE iv_country and then CREATE OBJECT ro_tax TYPE zcl_tax_in, zcl_tax_de, or zcl_tax_default depending on the value. Note the syntax CREATE OBJECT ref TYPE class: the reference variable itself is typed as the interface, while the object actually created is a concrete class.

Free account

Create a free account to keep reading

You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.

  • Full explanation
  • Code example
  • Real project scenario
  • Common mistakes
  • Best practices
  • Interview angle

One tap with Google works too. Every topic keeps its first lesson open to everyone.

Keep exploring

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.