Hide creation details when choosing a concrete type is a real decision
A dispatch application may have different notifier implementations for email, in-app messages, or a test. A factory centralizes which concrete class to create and can return a stable interface to its caller.
A small factory method
CLASS lcl_notifier_factory DEFINITION.
PUBLIC SECTION.
CLASS-METHODS create
RETURNING VALUE(result) TYPE REF TO lif_notifier.
ENDCLASS.
CLASS lcl_notifier_factory IMPLEMENTATION.
METHOD create.
result = NEW lcl_log_notifier( ).
ENDMETHOD.
ENDCLASS.
The caller receives the lif_notifier capability, not a concrete implementation detail:
DATA(notifier) = lcl_notifier_factory=>create( ).
A real factory might accept a configuration value, validate it, and choose among supported implementations. Do not add a factory when a plain NEW is already clear and there is no creation policy to hide.
Factory or Singleton?
A factory answers “which object should be created?” A Singleton answers “how do callers share one controlled instance?” They solve different problems and may be combined, but combining them can add lifecycle and test complexity. Prefer a simple constructor/injected dependency unless shared identity or variable construction makes a factory valuable.
Practice
Declare a class method returning REF TO lif_notifier, implement a concrete return, and assign the result. Add a message in result and the selected output statement. Checker is structural only.
Version note: Constructor expressions and reference-returning methods are modern ABAP syntax; verify language version and released APIs in the target system.
Sources
- SAP Samples, ABAP Object Orientation: classes, methods, and patterns
- SAP Samples, ABAP for Cloud Development
Developer practice · ABAP
Practice: return a notifier through its interface
Create a factory method that returns a notifier interface reference and assign a factory result to notifier.
This choice changes the output pattern checked here: WRITE belongs in a classic executable report, while out->write( ) uses the supplied output reference in an ABAP Cloud class-run method. It does not switch the language version of the other code; check release and API availability in your target system.
Complete the task and include the output statement shown for your selected environment. Variable names can differ if you use them consistently. This browser checker checks structure only; it does not compile or run ABAP.
0 required structures detected · 0 checks
Write your solution, then check its structure.
One possible solution
CLASS lcl_notifier_factory DEFINITION.
PUBLIC SECTION.
CLASS-METHODS create
RETURNING VALUE(result) TYPE REF TO lif_notifier.
ENDCLASS.
CLASS lcl_notifier_factory IMPLEMENTATION.
METHOD create.
result = NEW lcl_log_notifier( ).
ENDMETHOD.
ENDCLASS.
DATA(notifier) = lcl_notifier_factory=>create( ).
DATA(result) = 'Notifier created'.
WRITE result.out->write( result ).These are context-specific output examples. In your own program, use the result variable from your solution and the output mechanism available in that program.