MichaelB.
7/28/2018 - 12:01 PM

Declare and trigger EVENTS

Events are the 3rd possible component of a class. Object or class can publish an event, while another object or class can respond to an event. Normal methods are closely coupled with the caller (the caller calls the method). Event methods are decoupled from the caller. EVENTS: Add EVENTS as components in a class. Any method within the class can trigger the events. HANDLER Methods: Declare Handler Methods in the same class or another class to handle a response to the triggered events. Registering Handlers: Handler methods added to objects, at run-time can get registered by the system as handlers for the objects of the classes which can trigger the event.

To create a relationship between objects and events, you must implement code for classes that trigger events and classes that handle the events. Start by declaring events to be triggered in the declaration part of a class. Have to belong to one of 3 visibility sections in a class, which determines what kind of caller can handle the event. Protected: event can only be handled by objects of its own class or subclasses. Can only use EXPORTING parameters when declaring an event. The EXPORTING parameter interface is optional and are always passed by value.

RAISE EVENT forces the runtime environment to pause execution of the method the statement is in and execute all the registered handler methods. Once the registered handler methods have finished executing, processing continues in the method that raised the event. RAISE EVENT be used in all instance methods to trigger all instance and static events in a class. In a static method only static events can be triggered. Events with the same name in separate classes are still separate events.

* Declare an instance event (object triggered events)
EVENTS myevent EXPORTING VALUE(e) TYPE i [OPTIONAL | DEFAULT].

* Declare a static event (class triggered events)
CLASS-EVENTS myevent EXPORTING VALUE(e) TYPE i [OPTIONAL | DEFAULT].

* To trigger an event use the syntax (assign the export parameter a value):
RAISE EVENT myevent EXPORTING e = v.

***************************************************************************
* Practical Example

CLASS chef DEFINITION.
  PUBLIC SECTION.
    METHODS call_service.
    EVENTS call_for_waiter.
ENDCLASS.

CLASS customer DEFINITION.
  PUBLIC SECTION.
    METHODS: constructor IMPORTING VALUE(i_tablenumber) TYPE i,
             call_for_assistance.
    EVENTS:  call_for_waiter EXPORTING VALUE(e_tablenumber) TYPE i.

  PROTECTED SECTION.
    DATA tablenumber TYPE i.
ENDCLASS.

CLASS chef IMPLEMENTATION.
  METHOD call_service.
    WRITE: / 'Chef calling WAITER EVENT'.
    RAISE EVENT call_for_waiter.
    WRITE: / 'Chef calling WAITER EVENT complete'.
    ULINE.
  ENDMETHOD.
ENDCLASS.

CLASS customer IMPLEMENTATION.
  METHOD constructor.
    tablenumber = i_tablenumber.
  ENDMETHOD.
  
    METHOD call_for_assistance.
    WRITE: / 'Customer calling WAITER EVENT'.
    RAISE EVENT call_for_waiter EXPORTING e_tablenumber = tablenumber.
    WRITE: / 'Customer calling WAITER EVENT complete'.
    ULINE.
  ENDMETHOD.
ENDCLASS.

* Global Data

DATA: o_chef        TYPE REF TO chef,
      o_customer_1  TYPE REF TO customer,
      o_customer_2  TYPE REF TO customer.
      
 Program Starts Here

START-OF-SELECTION.
  CREATE OBJECT o_chef.
  CREATE OBJECT o_customer_1  EXPORTING i_tablenumber = 2.
  CREATE OBJECT o_customer_2  EXPORTING i_tablenumber = 5.

  o_chef->call_service( ).
  o_customer_1->call_for_assistance( ).
  o_customer_2->call_for_assistance( ).