An event is a mechanism set within a class which can help a class to trigger methods of another class.
Steps to code an application using events:
1) Declare an event in a class.
2) Define and implement a method that raises (triggers) the event. The method that raises the event
should be in the same class in which the event has been declared (or in any of its subclasses
where the event has been inherited.)
3) Define another class with a method that is associated with the event of the former class.
(Whenever the method in the former class raises the event, the method of current class will react
to it; in other words, the method handles the event. For this reason the method that handles the
event is called event-handler-method, and the class containing this method as handler-class.)
4) Create objects of the class that raises the event and the handler class.
5) Register the event-handler-method with the object of the class that raises the event.
6) Call the method that raises the event.
*----------------------------------------------------------------------*
* CLASS lcl_er DEFINITION - Class with an event ev1 and a method that raises the
event
*----------------------------------------------------------------------*
class lcl_er definition.
public section.
events: ev1. "Declaration of an event step 1
methods: m1.
endclass.
*----------------------------------------------------------------------*
*
CLASS lcl_er IMPLEMENTATION
*----------------------------------------------------------------------*
class lcl_er implementation.
method m1.
raise event ev1. "Raising the event in a method step 2
endmethod.
endclass.
*----------------------------------------------------------------------*
* CLASS lcl_eh DEFINITION - Class that defines a method to handle
*
the event ev1 of class lcl_er
*----------------------------------------------------------------------*
class lcl_eh definition.
public section.
methods: ev1_handler for event ev1 of lcl_er.
endclass.
*----------------------------------------------------------------------*
*
CLASS lcl_eh IMPLEMENTATION
*----------------------------------------------------------------------*
class lcl_eh implementation.
method ev1_handler.
message 'Handling the event ev1 of lcl_er.' type 'I'. "Handling the event in a
method step 3
endmethod.
endclass.
********************************************
start-of-selection.
data: er type ref to lcl_er,
eh type ref to lcl_eh.
create object: er, eh. "Creating objects event raiser and event handler classes
step 4
set handler eh->ev1_handler for er. "Register the event-handler-method step 5
er->m1( ). "Call the method that raises the event step - 6
*& If stock is not available display appropriate message and avoid further processing
START-OF-SELECTION.
*& Object of storage_location has to be created first and only then the
*& seller object has to be created. Else, there will a runtime error of
*& type CX_SY_REF_IS_INITIAL.
CREATE OBJECT : sl EXPORTING imc_av_stk = p_av_stk,
STEP-4
sdept EXPORTING imc_sl = sl,
sd_evh.
SET HANDLER sd_evh->no_stock_handler FOR sdept.
sdept->sell_stock( ).
WRITE: /3 'Sale Completed.'.
STEP-5
STEP-6
NOTE: Execute the above program in debugging mode and carefully observe the flow control. As soon
as the RAISE EVENT command raises the event no_stock in the method sell_stock of sales_dept class,
the control jumps to no_stock_handler method of sd_event_handler class. Nowhere in the code above
the no_stock_handler method of sd_event_handler class is explicitly called, but still it gets invoked by the
event handling mechanism.