OOP ABAP Part 8: Interfaces and Factory Methods
Preview · a free account unlocks the full lesson
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 sectionsHide sections
Visual overview
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.
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
Interview questions on this topic
Related SAP topics
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.