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
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,
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
In the above program, instances of the Class CL_NAMEcreate an event OBJECT_CREATED. The class method PROCESS_EVENT processes this event. With the help of the constructor the class registers the method PROCESS_EVENT at the event OBJECT_CREATED for all instances of the class CL_NAME. When an object is created for the class CL_NAME (CREATE OBJECT instance.), the event OBJECT_CREATED is raised in the constructor. The consequence is that the method PROCESS_EVENT{{ }}is executed.
Code listing for: Z_007_EVENT2
Description: EVENT OF ONE CLASS HANDLED BY ANOTHER CLASS
Description: EVENT OF ONE CLASS HANDLED BY ANOTHER CLASS
REPORT Z_007_EVENT2.
*----------------------------------------------------------------
* CLASS CL_CLASS1 DEFINITION
*----------------------------------------------------------------
CLASS CL_CLASS1 DEFINITION.
PUBLIC SECTION.
EVENTS OBJECT_CREATED. " DEFINES EVENT
METHODS CONSTRUCTOR.
ENDCLASS. "CL_CLASS1 DEFINITION
*----------------------------------------------------------------
* CLASS CL_CLASS1 IMPLEMENTATION
*----------------------------------------------------------------
CLASS CL_CLASS1 IMPLEMENTATION.
METHOD CONSTRUCTOR.
RAISE EVENT OBJECT_CREATED. " RAISES EVENT
ENDMETHOD. "CONSTRUCTOR
ENDCLASS. "CL_CLASS1 IMPLEMENTATION
*----------------------------------------------------------------
* CLASS CL_CLASS2 DEFINITION
*----------------------------------------------------------------
CLASS CL_CLASS2 DEFINITION.
PUBLIC SECTION.
CLASS-METHODS INITIALISE.
PRIVATE SECTION.
CLASS-METHODS PROCESS_EVENT FOR EVENT OBJECT_CREATED
OF CL_CLASS1. " REGISTERS METHOD
ENDCLASS. "CL_CLASS2 DEFINITION
*----------------------------------------------------------------
* CLASS CL_CLASS2 IMPLEMENTATION
*----------------------------------------------------------------
CLASS CL_CLASS2 IMPLEMENTATION.
METHOD INITIALISE.
"SET HANDLER FOR ALL INSTANCES
SET HANDLER PROCESS_EVENT FOR ALL INSTANCES.
ENDMETHOD. "INITIALISE
" EVENT HANDLER
METHOD PROCESS_EVENT.
WRITE 'EVENT FIRED.'.
ENDMETHOD. "PROCESS_EVENT
ENDCLASS. "CL_CLASS2 IMPLEMENTATION
DATA INSTANCES TYPE REF TO CL_CLASS1.
START-OF-SELECTION.
CALL METHOD CL_CLASS2=>INITIALISE.
CREATE OBJECT INSTANCES.
CLEAR INSTANCES.
Program Output : 007
EVENT FIRED.
No comments:
Post a Comment