Events are recognized in particular by programming interfaces of the GUIs (Windows, Motif, etc.), for example, you can "ask" the GUI to trigger an event if the user moves the mouse over a specific part of the screen. When the event occurs you are telling the GUI to change the shape of the mouse pointer.
Events allow for the loose coupling of components (classes or objects) in a system. The event trigger does not normally know at the time of coding who is going to react to the event. Those components, which want to react to the event, register at the event runtime, in that they tell the runtime environment which method is to be executed when the event is raised. In this way many components can register for an event.
Event handler methods can proceed synchronously as well as asynchronously. At present, ABAP Objects only supports synchronous calling of the event handler method.
Code listing for: Z_006_EVENT
Description: EXAMPLE OF EVENTS
*---------------------------------------------------------------
* THIS EXAMPLE SHOWS THE USE OF EVENTS.
*---------------------------------------------------------------
*
* The event trigger does not normally know at the time of
* coding who is going to react to the event. Those components,
* which want to react to the event, register at the event
* runtime, in that they tell the runtime environment which
* method is to be executed when the event is raised. In this way
* many components can register for an event.
*
*---------------------------------------------------------------
REPORT Z_006_EVENT.
*----------------------------------------------------------------
* CLASS CL_NAME
*----------------------------------------------------------------
CLASS CL_NAME DEFINITION.
PUBLIC SECTION.
" DEFINE EVENT
EVENTS OBJECT_CREATED
EXPORTING VALUE(EX_OBJ) TYPE REF TO CL_NAME.
METHODS: CONSTRUCTOR,
" DEFINE EVENT HANDLER METHOD
PROCESS_EVENT FOR EVENT OBJECT_CREATED OF CL_NAME.
PRIVATE SECTION.
DATA MSG(16) TYPE C.
" register method with runtime will be executed
" when event OBJECT_CREATED fires.
ENDCLASS. "CL_NAME
*----------------------------------------------------------------
* CLASS CL_NAME IMPLEMENTATION
*----------------------------------------------------------------
CLASS CL_NAME IMPLEMENTATION.
METHOD CONSTRUCTOR.
MSG = 'OBJECT CREATED'.
" Register the event handlers for the corresponding/all
" instance/s.
SET HANDLER PROCESS_EVENT FOR ALL INSTANCES.
" Raise event OBJECT_CREATED.
RAISE EVENT OBJECT_CREATED EXPORTING EX_OBJ = ME.
"ME refers to current instance
ENDMETHOD. "CL_NAME
" EVENT HANDLER
METHOD PROCESS_EVENT.
WRITE: 'EVENT FIRED :', ME->MSG.
ENDMETHOD. "PROCESS_EVENT
ENDCLASS. "CL_NAME IMPLEMENTATION
DATA INSTANCE TYPE REF TO CL_NAME.
START-OF-SELECTION.
CREATE OBJECT INSTANCE.
CLEAR INSTANCE.
Program Output : 006
EVENT FIRED : OBJECT CREATED