You are on page 1of 6

Events:

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.

Syntax to declare an event:


EVENTS <event_name>.
(Events can be declared in any of the 3 visibility sections of a class, or in an interface too.)
Syntax to raise (trigger) an event:
RAISE EVENT <event_name>.
Syntax for associating a method in the handler class with an event:
METHODS: <method_name> FOR EVENT <event_name> OF <*class_name>.
*class_name = Name of the class that contains the event and raises the event.
Syntax to register event-handler-method with the object of the class that raises the event:
SET HANDLER <event-handler-method> FOR <*object_ref>
*object ref = reference variable pointing to the object of the class that raises it.

*----------------------------------------------------------------------*
* 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

write: 'Execution completed'.


REPORT zoop_event0A.
*----------------------------------------------------------------------*
* CLASS lcl_er DEFINITION - Class with an event ev1 exporting some data, and a
method that raises the * event
*----------------------------------------------------------------------*
CLASS lcl_er DEFINITION.
PUBLIC SECTION.
EVENTS: ev1 exporting value(im_x) type char4. "Declaration of an event
METHODS: m1 importing arg1 type char4.
ENDCLASS.
"lcl_er DEFINITION
CLASS lcl_er IMPLEMENTATION.
METHOD m1 .
RAISE EVENT ev1 exporting im_x = arg1. "Raising the event in a method
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
importing im_x.
ENDCLASS.
CLASS lcl_eh IMPLEMENTATION.
METHOD ev1_handler.
data: mesg type string.
concatenate 'Value of passed thru event ev1 is:'
im_x
into mesg separated by space.
MESSAGE mesg TYPE 'I'.
ENDMETHOD.
ENDCLASS.
********************************************
START-OF-SELECTION.
parameters: p_arg type char4.
DATA: er TYPE REF TO lcl_er,
eh TYPE REF TO lcl_eh.
CREATE OBJECT: er, eh.
SET HANDLER eh->ev1_handler FOR er.
er->m1( arg1 = p_arg ).
write: 'Execution completed'.

Let us discuss a scenario in which event handling can be used:


A Sales Department has-a Storage Location and an activity called sell. The sell activity picks one unit
of stock form the storage location and sells one unit of stock at a time. If some stock is available the sellactivity sells one unit of stock, but how should the sell-activity respond in the event of zero stock in
storage location? The sell-activity, should raise an alarm (raise the event) that no stock is available and
leave the things to those who are interested in (responsible for) responding to the alarm (handling the
event). The one who is responsible to respond to the alarm (event) is known as an Event Handler .
In the above explained scenario we come across:
Three important nouns Sales Department, Storage Location, and Event Handler.
Two important verbs Sell and Handle the event.
One event no stock.
In Object Oriented Programming every important noun will have to be defined as a class and verb (an
activity) as a method.
The code below is an object oriented representation of the above discussed scenario.
*----------------------------------------------------------------------*
*
CLASS storage_location
*----------------------------------------------------------------------*
CLASS storage_location DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING imc_av_stk TYPE i,
*& Method to return the available qty of stock
get_av_stk RETURNING value(r_av_stk) TYPE i,
*& Method to update the available stock qty
set_av_stk IMPORTING im_av_stk TYPE i.
PRIVATE SECTION.
DATA : av_stk TYPE i. "Available stock in units
ENDCLASS.
CLASS storage_location IMPLEMENTATION.
METHOD constructor.
av_stk = imc_av_stk.
ENDMETHOD.
METHOD get_av_stk.
r_av_stk = av_stk.
ENDMETHOD.
METHOD set_av_stk.
av_stk = im_av_stk.
ENDMETHOD.
ENDCLASS.
*----------------------------------------------------------------------*
*
CLASS sales
*----------------------------------------------------------------------*
CLASS sales_dept DEFINITION.
PUBLIC SECTION.
METHODS: constructor
IMPORTING imc_sl TYPE REF TO storage_location,

*& Method to sell 1 unit of stock


sell_stock.
EVENTS :
STEP-1
*& Event to be raised when available stock is zero
no_stock.
PRIVATE SECTION.
DATA: sl TYPE REF TO storage_location. Sales Dept has-a Storage Location
ENDCLASS.
CLASS sales_dept IMPLEMENTATION.
METHOD constructor.
sl = imc_sl.
ENDMETHOD.
METHOD sell_stock.
DATA: newstk TYPE i.
*& Before making a sale check available stock, and if stock is zero raise
*& the event "no_stock"
IF sl->get_av_stk( ) = 0.
RAISE EVENT no_stock.
STEP-2
ELSE.
*& Reduce the available stock qty by 1, as 1 unit is being sold
*& and update the available stock to decreased qty
newstk = sl->get_av_stk( ) - 1.
sl->set_av_stk( newstk ).
ENDIF.
ENDMETHOD.
ENDCLASS.
*----------------------------------------------------------------------*
*
CLASS sd_event_handler (Sales Department Event Handler) STEP-3
*----------------------------------------------------------------------*
CLASS sd_event_handler DEFINITION.
PUBLIC SECTION.
*& If no_stock event of SALES class is raised, the logic to handle such event
*& is to be provided in this method
METHODS : no_stock_handler FOR EVENT no_stock OF sales_dept.
ENDCLASS.
CLASS sd_event_handler IMPLEMENTATION.
METHOD no_stock_handler.

*& If stock is not available display appropriate message and avoid further processing

MESSAGE 'Stocks not available for sale' TYPE 'I'.


LEAVE LIST-PROCESSING.
ENDMETHOD.
ENDCLASS.
**************************************
DATA : sdept TYPE REF TO sales_dept,
" Sales Department object
sd_evh TYPE REF TO sd_event_handler, " Event Handler object
sl TYPE REF TO storage_location.
" Storage Location object
PARAMETERS: p_av_stk TYPE i OBLIGATORY.

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.