Skip to end of metadata
Go to start of metadata

Author: R B
Submitted: 01-July-2014
 

Event is an action items using which object of different types can interact with each other on specific actions.

In ABAP Objects, triggering and handling an event means that certain methods act as triggers and trigger events, to which other methods - the handlers - react. This means that the handler methods are executed when the event occurs.

Event can be raised by event handler method which is declared in same/another class.

There are two types of events:

Instance Event   are always specific to an object instance. Therefore they need an object instance to raise them.

Static events: Static events can be raised without need of creating object instance and can be raised stand alone without any instance.  Static events can be triggered by any method. However, static methods can only trigger static events.

  1. In the following example, we have declared instance events ev1,ev2 and static event ev_cl inside class lcl_class1.
  2. Triggering method trig_ev1 is defined for event ev1 in same class lcl_class1.
  3. Triggering method trig_ev2 is defined for event ev2 in same class lcl_class1.
  4. There are two triggering method for the static event cv_cl. One is instance method trig_ev_cl and another is static method trig_ev_cl_stat.

 

  1. The handler method for instance event ev1 is defined in same class lcl_class1.
  2. The handler method for instance event ev2 is defined in another class lcl_class2.
  3. The handler method for static method ev_cl is defined in lcl_class2.

 

 

CLASS lcl_class1 DEFINITION.
  PUBLIC SECTION.
    DATA a1 TYPE I.
    EVENTS: ev1,
            ev2 EXPORTING value(val1) TYPE I.
    CLASS-EVENTS ev_cl EXPORTING value(val1) TYPE string. “static event
    METHODS: handling_ev1 FOR EVENT ev1 OF lcl_class1,
             trig_ev1,
             trig_ev2,
             trig_ev_cl.
CLASS-METHODS trig_ev_cl_stat. “static method used as trigger for event ev_cl

ENDCLASS.

CLASS lcl_class1 IMPLEMENTATION.
  METHOD handling_ev1.
    WRITE: / 'Handling method for event ev1 defined in LCL_CLASS1'.
  ENDMETHOD.                   
  METHOD trig_ev1.
    WRITE / 'Trigerring Method for event EV1 defined in lcl_class1'.
    RAISE EVENT ev1.
  ENDMETHOD.                    
  METHOD trig_ev2.
    a1 = 10.
    WRITE / 'Trigerring Method for event EV2 defined in lcl_class1'.
    RAISE EVENT ev2 EXPORTING val1 = a1.
  ENDMETHOD.                    
  METHOD trig_ev_cl.
    WRITE: / 'Trigerring Method for event EV_CL defined in lcl_class1'.
    RAISE EVENT ev_cl EXPORTING val1 = 'static event'.
  ENDMETHOD.                    
  METHOD trig_ev_cl_stat.
    WRITE: /'Trigger static method for event EV_CL from lcl_CLAS1'.
    RAISE EVENT ev_cl EXPORTING val1 = 'static event'.
  ENDMETHOD.

ENDCLASS. 

CLASS lcl_class2 DEFINITION.
  PUBLIC SECTION.
    METHODS:  handling_ev2 FOR EVENT ev2 OF lcl_class1 IMPORTING val1,
              handling_ev_cl FOR EVENT ev_cl OF lcl_class1 IMPORTING val1.
  PRIVATE SECTION.
    DATA a2 TYPE I.
ENDCLASS.

CLASS lcl_class2 IMPLEMENTATION.
  METHOD handling_ev2.
    a2 = 10.    “a2 is a private data of class lcl_class2
    val1 = val1 + a2.
    WRITE: / 'Handling method defined in LCL_CLASS2 for event ev2'.
    WRITE val1.
  ENDMETHOD.                    "handling_ev2
  METHOD handling_ev_cl.
    WRITE: / 'Handling method defined in LCL_CLASS2 for event ev_cl'.
    WRITE val1.
  ENDMETHOD.                    "handling_ev_cl
ENDCLASS.

START-OF-SELECTION.

  DATA: obj TYPE REF TO lcl_class1,
        obj2 TYPE REF TO lcl_class2.
  CREATE OBJECT obj.
  CREATE OBJECT obj2.
  SET HANDLER obj->handling_ev1 FOR obj.
  SET HANDLER obj2->handling_ev2 FOR obj.
  SET HANDLER obj2->handling_ev_cl.

  CALL METHOD obj->trig_ev1.
  CALL METHOD obj->trig_ev2.
  CALL METHOD obj->trig_ev_cl.
 call METHOD lcl_class1=>trig_ev_cl_stat.

Output:

Trigerring Method for event EV1 defined in lcl_class1

Handling method for event ev1 defined in LCL_CLASS1

Trigerring Method for event EV2 defined in lcl_class1

Handling method defined in LCL_CLASS2 for event ev2         20

Trigerring Method for event EV_CL defined in lcl_class1

Handling method defined in LCL_CLASS2 for event ev_cl static event

Trigger static method for event EV_CL from lcl_CLAS1

Handling method defined in LCL_CLASS2 for event ev_cl static event                

 

 

  • No labels