Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
RobinV
Participant

Why do we use these design patterns

The delegation design pattern and the event handling design pattern provide loose coupling communication between objects.

When to use the delegation design pattern

  • You need bi-directional communication between objects
  • You need to know if there is a delegate that handles the communication


When to use the event handling design pattern

  • You need a very flexible one way communication where every object can attach himself to the event.


How to implement the delegation design pattern

  1. Create an interface for the delegate
  2. Implement the interface
  3. Set your object as the delegate


How to implement the event handling design pattern

  1. Create an event handler method
  2. Attach the event handler method to the event


In practice

Okay we have talked enough theoretically. I have created an example which implements both design patterns.
The example has only one object of an person and you can have two confirmations:

  • Confirm the name
  • Confirm to save


The name confirmation implements the delegation design pattern.
The save confirmation implements the event handling design pattern.

UML

Source of the example

REPORT  zz_rv_test.


PARAMETERS p_name TYPE text20 ##sel_wrong.


*----------------------------------------------------------------------*

* CLASS lcl_confirm_save DEFINITION

*----------------------------------------------------------------------*

CLASS lcl_confirm_save DEFINITION FINAL.

  PUBLIC SECTION.

    METHODS show_confirmation.

    EVENTS evt_save.


  PRIVATE SECTION.

    DATA mv_answer TYPE char1.

    CONSTANTS gc_confirm_save TYPE text40 VALUE 'Do you want to save?'  ##no_text.

ENDCLASS.                    "lcl_confirm_save DEFINITION


*----------------------------------------------------------------------*

* CLASS lcl_confirm_save IMPLEMENTATION

*----------------------------------------------------------------------*

CLASS lcl_confirm_save IMPLEMENTATION.

  METHOD show_confirmation.

    CALL FUNCTION 'POPUP_TO_CONFIRM'

      EXPORTING

        text_question = gc_confirm_save

      IMPORTING

        answer        = mv_answer

      EXCEPTIONS

        OTHERS        = 1.

    IF sy-subrc = 0

    AND mv_answer = '1'.

      RAISE EVENT evt_save.

    ENDIF.

  ENDMETHOD.                    "show_confirmation

ENDCLASS.                    "lcl_confirm_save IMPLEMENTATION


*----------------------------------------------------------------------*

* INTERFACE zif_confirm_name_delegate

*----------------------------------------------------------------------*

INTERFACE zif_confirm_name_delegate.

  METHODS get_name RETURNING value(rv_name) TYPE char20.

  METHODS handle_confirmed.

ENDINTERFACE.                    "zif_confirm_name_delegate

*----------------------------------------------------------------------*

* CLASS lcl_confirm_name DEFINITION

*----------------------------------------------------------------------*

CLASS lcl_confirm_name DEFINITION FINAL.

  PUBLIC SECTION.

    METHODS constructor IMPORTING io_delegate TYPE REF TO zif_confirm_name_delegate.

    METHODS show_confirmation.

  PRIVATE SECTION.

    DATA mv_answer TYPE char1.

    DATA mo_delegate TYPE REF TO zif_confirm_name_delegate.

    CONSTANTS gc_confirm_name TYPE text40 VALUE 'Is your name:' ##no_text.

ENDCLASS.                    "lcl_confirm_name DEFINITION

*----------------------------------------------------------------------*

* CLASS lcl_confirm_name IMPLEMENTATION

*----------------------------------------------------------------------*

CLASS lcl_confirm_name IMPLEMENTATION.

  METHOD constructor.

    "IF mo_delegate IS NOT BOUND.

    "Raise exception

    "ENDIF.

    mo_delegate = io_delegate.

  ENDMETHOD.                    "constructor

  METHOD show_confirmation.

    DATA lv_confirm TYPE text40.

    lv_confirm = gc_confirm_name && ` ` && mo_delegate->get_name( ).

    CALL FUNCTION 'POPUP_TO_CONFIRM'

      EXPORTING

        text_question = lv_confirm

      IMPORTING

        answer        = mv_answer

      EXCEPTIONS

        OTHERS        = 1.

    IF sy-subrc = 0

    AND mv_answer = '1'.

      mo_delegate->handle_confirmed( ).

    ENDIF.

  ENDMETHOD.                    "show_confirmation

ENDCLASS.                    "lcl_confirm_name IMPLEMENTATION

*----------------------------------------------------------------------*

* CLASS LCL_PERSON DEFINITION

*----------------------------------------------------------------------*

CLASS lcl_person DEFINITION FINAL.

  PUBLIC SECTION.

    INTERFACES zif_confirm_name_delegate.

    METHODS constructor IMPORTING iv_name TYPE char20.

    METHODS confirm_my_name.

    METHODS save.

  PRIVATE SECTION.

    METHODS handle_save FOR EVENT evt_save OF lcl_confirm_save.

    DATA mv_name TYPE char20.

ENDCLASS.                    "LCL_PERSON DEFINITION

*----------------------------------------------------------------------*

* CLASS LCL_PERSON IMPLEMENTATION

*----------------------------------------------------------------------*

CLASS lcl_person IMPLEMENTATION.

  METHOD constructor.

    mv_name = iv_name.

  ENDMETHOD.                    "constructor

  METHOD confirm_my_name.

    DATA lo_confirm TYPE REF TO lcl_confirm_name.

    CREATE OBJECT lo_confirm

      EXPORTING

        io_delegate = me.

    lo_confirm->show_confirmation( ).

  ENDMETHOD.                    "CONFIRM_my_name

  METHOD save.

    DATA lo_confirm TYPE REF TO lcl_confirm_save.

    CREATE OBJECT lo_confirm.

    SET HANDLER handle_save FOR lo_confirm ACTIVATION abap_true.

    lo_confirm->show_confirmation( ).

    SET HANDLER handle_save FOR lo_confirm ACTIVATION abap_false.

  ENDMETHOD.                    "save

  METHOD handle_save.

    WRITE: / 'SAVED:', mv_name.

  ENDMETHOD.                    "handle_save

  METHOD zif_confirm_name_delegate~get_name.

    rv_name = mv_name.

  ENDMETHOD.                    "zif_confirm_name_delegate~get_name

  METHOD zif_confirm_name_delegate~handle_confirmed.

    WRITE: / 'CONFIRMED:', mv_name.

  ENDMETHOD.                    "zif_confirm_name_delegate~handle_confirmed

ENDCLASS.                    "LCL_PERSON IMPLEMENTATION

*----------------------------------------------------------------------*

* CLASS lcl_main_ctr DEFINITION

*----------------------------------------------------------------------*

CLASS lcl_main_ctr DEFINITION FINAL CREATE PRIVATE.

  PUBLIC SECTION.

    CLASS-METHODS class_constructor.

    METHODS process.

    CLASS-DATA so_main_ctr TYPE REF TO lcl_main_ctr.

ENDCLASS.                    "lcl_main_ctr DEFINITION

*----------------------------------------------------------------------*

* CLASS lcl_main_ctr IMPLEMENTATION

*----------------------------------------------------------------------*

CLASS lcl_main_ctr IMPLEMENTATION.

  METHOD class_constructor.

    CREATE OBJECT so_main_ctr.

  ENDMETHOD.                    "class_constructor

  METHOD process.

    DATA lo_person TYPE REF TO lcl_person.

    CREATE OBJECT lo_person

      EXPORTING

        iv_name = p_name.

    lo_person->confirm_my_name( ).

    lo_person->save( ).

    WRITE: / space.

  ENDMETHOD.                    "process

ENDCLASS.                    "lcl_main_ctr IMPLEMENTATION

*--------------------------------------------------------------------*

* Start of selection

*--------------------------------------------------------------------*

START-OF-SELECTION.

  lcl_main_ctr=>so_main_ctr->process( ).

Please let me know if you have any questions.

Regards,

Robin Vleeschhouwer
RV SAP Consultancy

Labels in this area