Friday, April 5, 2013

Single Event can have Multiple Event Handler Methods

One event can have more than one event handler method in the same class or in different classes. At the run time only one handler method will be triggered at a time. After triggering the one that has to be deactivated and then another handler method will have to be registered to be triggered. In this way system can register multiple event handler method to the same event.

The following program contains a class cls1 where in the public section we declare data v_cls1, event evnt, triggering method trig and event handler method evnthand1 for event evnt of class cls1. Now at the time of implementation we display a text in event handler method evnthand1 then we display another text in triggering method trig.

Next we have defined another class cls2 where in the public section we declare data and another event handler method evnthand2 for event evnt of class cls1. In the implementation we display a text in this method.

Finally in the start of selection we have created object obj1 for cls1 and obj2 for cls2. Now we set handler method evnthand1 for object obj1 and then call triggering method trig. After that we deactivate the registration of evnthand1 and then register again evnthand2 for obj1 by obj2. Then again we call the method trig. Hence initially we have registered the first handler method to the event and call the trigger. Then we have deactivated the registration to register the second handler method and called the same trigger again.

*&---------------------------------------------------------------------*
*& Report  ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zsr_test.

*----------------------------------------------------------------------*
*       CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.
  PUBLIC SECTION.
    DATA v_cls1 TYPE char50.
    EVENTS evnt.
    METHODS: trig,
             evnthand1 FOR EVENT evnt OF cls1.
ENDCLASS.                    "cls1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 DEFINITION.
  PUBLIC SECTION.
    DATA v_cls2 TYPE char50.
    METHODS evnthand2 FOR EVENT evnt OF cls1.
ENDCLASS.                    "cls2 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
  METHOD evnthand1.
    v_cls1 = 'Event Handler in Class One'.
    WRITE / v_cls1.
    SKIP.
  ENDMETHOD.                                                "evnthand1

  METHOD trig.
    v_cls1 = 'Event Triggers in Class One'.
    WRITE / v_cls1.
    RAISE EVENT evnt.
  ENDMETHOD.                    "trig
ENDCLASS.                    "cls1 IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS cls2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 IMPLEMENTATION.
  METHOD evnthand2.
    v_cls2 = 'Event Handler in Class Two'.
    WRITE / v_cls2.
    SKIP.
  ENDMETHOD.                                                "evnthand2
ENDCLASS.                    "cls2 IMPLEMENTATION

START-OF-SELECTION.
  DATA: obj1 TYPE REF TO cls1,
        obj2 TYPE REF TO cls2.

  CREATE OBJECT: obj1, obj2.
  SET HANDLER obj1->evnthand1 FOR obj1.
  CALL METHOD obj1->trig.

  SET HANDLER: obj1->evnthand1 FOR obj1 ACTIVATION space,
               obj2->evnthand2 FOR obj1.
  CALL METHOD obj1->trig.


Below is the output:

3 comments:

yektek training said...

very thanks for sharing this information reeally nice

Unknown said...

Nice!
However, is there any way that the trigger will only be called once? Just a thought. Thanks for sharing!

Unknown said...

I like your post its very useful for me and I follow your all post .I am waiting for your next essential article.sap hr abap training