ABAP Factory Method Using SWITCH

By | October 26, 2015 | ABAP Objects, OO Design Patterns | 18,559 | 3

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( ).
 

Table of Content – Design Patterns

Explore all articles in ABAP Design Patterns

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

3 Comments

  • Radouane

    Nice !

    I will try to apply it as soon as possible and, for sure, it’s very appreciated you keep us up to date =)

  • Swadhin

    Simply Excellent Article.

  • A very helpful post! Thanks for sharing!
    For more information on SAP ABAP services you can visit http://www.apprisia.com/sap-r3/sap-abap-development-services/

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.