Gyan Factory

Gyan Factory
SAP Technical Project Support

Tuesday, February 16, 2016

ABAP Factory Method Using SWITCH

SWITCH usage with returning the object reference. This would be great to reduce lot of clutter from the design pattern implementation i.e. Factory design pattern.

Preface

In the last article ABAP 740 SWITCH – Conditional Operator, I mentioned – the returned value is the object reference, the up-casting should be possible. This means that the Switch is capable of handling the object reference. Let see how.

Factory Design pattern using Switch

A while back, I demonstrated the implementation of the Factory Design patterns in ABAP objects. This was using the simple IF, ELSE. To recap, the Factory method design pattern hides all the complexity of object creation from the consumer of the object. Its good idea to read the article Factory Design patterns in ABAP objects before moving further.
ABAP_740_Switch_Usage_Factory_Method
Now, with ABAP 740 – we can use the SWITCH instead of the IF .. ELSE .. ENDIF

Factory Design pattern Using SWITCH

 
*=====
CLASS ophandler DEFINITION ABSTRACT.
  PUBLIC SECTION.
    CLASS-METHODS: factory
      IMPORTING iv_output_type TYPE kschl
      RETURNING VALUE(ro_obj)  TYPE REF TO ophandler.
    METHODS: process_output ABSTRACT.
ENDCLASS.                    "ophandler DEFINITION
 
*=====
CLASS ophandler_zabc DEFINITION INHERITING FROM ophandler.
  PUBLIC SECTION.
    METHODS: process_output REDEFINITION.
ENDCLASS.                    "ophandler_zabc DEFINITION
*
CLASS ophandler_zabc IMPLEMENTATION.
  METHOD process_output.
    WRITE: / 'Processing ZABC'.
  ENDMETHOD.                    "process_output
ENDCLASS.                    "ophandler_zabc IMPLEMENTATION
 
*=====
CLASS ophandler_zxyz DEFINITION INHERITING FROM ophandler.
  PUBLIC SECTION.
    METHODS: process_output REDEFINITION.
ENDCLASS.
*
CLASS ophandler_zxyz IMPLEMENTATION.
  METHOD process_output.
    WRITE: / 'Processing ZXYZ'.
  ENDMETHOD.                    "process_output
ENDCLASS.
 
*
CLASS ophandler IMPLEMENTATION.
  METHOD factory.
    ro_obj =
              SWITCH #(
                        iv_output_type
                        WHEN 'ZABC' THEN NEW ophandler_zabc( )
                        WHEN 'ZXYZ' THEN NEW ophandler_zxyz( )
                        ELSE THROW cx_sy_no_handler( )
                      ).
*
*    CASE iv_output_type.
*      WHEN 'ZABC'.
*        CREATE OBJECT ro_obj TYPE ophandler_zabc.
*      WHEN 'ZXYZ'.
*        "create another object
*      WHEN OTHERS.
*        " raise exception
*    ENDCASE.
  ENDMETHOD.                    "factory
ENDCLASS.                    "ophandler IMPLEMENTATION
 
*=====
CLASS lcl_main_app DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS: run.
ENDCLASS.                    "lcl_main_app DEFINITION
*
CLASS lcl_main_app IMPLEMENTATION.
  METHOD run.
    DATA: lo_output TYPE REF TO ophandler.
    lo_output = ophandler=>factory( 'ZABC' ).
    lo_output->process_output( ).
  ENDMETHOD.                    "run
ENDCLASS.                    "lcl_main_app IMPLEMENTATION
 
 
START-OF-SELECTION.
  lcl_main_app=>run( ).
 


No comments:

Post a Comment