event handler

14
Define a Class Define a local class CL_EVENTS and follow the below steps. Step1:Define an event . Step2:Define a method and meke it as event handler. Step4:Create a triggering method which will raise the event. Step5:Use set handler and register event handler method to a particular instance in the program. Define an event inside the local class definition under public section. EVENTS : NO_MATERIAL. "event Define a method and makt it as Event handler method METHODS : EVENT_HANDLER FOR EVENT NO_MATERIAL OF CL_EVENTS. "event handler method Create one more method for this example. METHODS : GET_MATERIAL_DETAILS IMPORTING IM_MATNR TYPE MARA-MATNR EXPORTING EX_MARA TYPE MARA. Create a triggering point Create a triggering poing with keyword RAISE. CLASS CL_EVENTS IMPLEMENTATION. METHOD GET_MATERIAL_DETAILS. SELECT SINGLE * FROM MARA INTO EX_MARA WHERE MATNR = IM_MATNR. IF SY-SUBRC NE 0. RAISE EVENT NO_MATERIAL. "triggering point ENDIF. ENDMETHOD. METHOD EVENT_HANDLER. WRITE :/ 'No material found'. ENDMETHOD.

Upload: vineetkumarsingh

Post on 03-Oct-2015

10 views

Category:

Documents


1 download

DESCRIPTION

event in abap

TRANSCRIPT

Define a Class Define a local class CL_EVENTS and follow the below steps. Step1:Define an event . Step2:Define a method and meke it as event handler. Step4:Create a triggering method which will raise the event. Step5:Use set handler and register event handler method to a particular instance in the program.Define an event inside the local class definition under public section. EVENTS : NO_MATERIAL. "eventDefine a method and makt it as Event handler method METHODS : EVENT_HANDLER FOR EVENT NO_MATERIAL OF CL_EVENTS. "event handler methodCreate one more method for this example. METHODS : GET_MATERIAL_DETAILS IMPORTING IM_MATNR TYPE MARA-MATNR EXPORTING EX_MARA TYPE MARA.Create a triggering point Create a triggering poing with keyword RAISE. CLASS CL_EVENTS IMPLEMENTATION. METHOD GET_MATERIAL_DETAILS. SELECT SINGLE * FROM MARA INTO EX_MARA WHERE MATNR = IM_MATNR. IF SY-SUBRC NE 0. RAISE EVENT NO_MATERIAL. "triggering point ENDIF. ENDMETHOD. METHOD EVENT_HANDLER. WRITE :/ 'No material found'. ENDMETHOD.ENDCLASS. "CL_EVENTSRegister event using SET HANDLER and use it in our program Define class, create object and register the event with th eobject using SET HANDLER keyword. DATA LO_EVENT TYPE REF TO CL_EVENTS.DATA : WA_MARA TYPE MARA.CREATE OBJECT LO_EVENT.SET HANDLER LO_EVENT->EVENT_HANDLER FOR LO_EVENT.CALL METHOD LO_EVENT->GET_MATERIAL_DETAILS EXPORTING IM_MATNR = P_MATNR IMPORTING EX_MARA = WA_MARA.

Final Program code will be REPORT ZSAPN_LOCAL_CLASS_EVENTS.CLASS CL_EVENTS DEFINITION DEFERRED.DATA LO_EVENT TYPE REF TO CL_EVENTS. "declare classDATA : WA_MARA TYPE MARA. "declare work areaPARAMETERS P_MATNR TYPE MARA-MATNR. "Material no inputCLASS CL_EVENTS DEFINITION. "class definition PUBLIC SECTION. EVENTS : NO_MATERIAL. "event METHODS : GET_MATERIAL_DETAILS IMPORTING IM_MATNR TYPE MARA-MATNR EXPORTING EX_MARA TYPE MARA. METHODS : EVENT_HANDLER FOR EVENT NO_MATERIAL OF CL_EVENTS. "event handler methodENDCLASS.

START-OF-SELECTION. CREATE OBJECT LO_EVENT. "create object SET HANDLER LO_EVENT->EVENT_HANDLER FOR LO_EVENT. "register event handler method for the object CALL METHOD LO_EVENT->GET_MATERIAL_DETAILS "call method to get material details EXPORTING IM_MATNR = P_MATNR IMPORTING EX_MARA = WA_MARA. WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.CLASS CL_EVENTS IMPLEMENTATION. "class implementation METHOD GET_MATERIAL_DETAILS. SELECT SINGLE * FROM MARA INTO EX_MARA WHERE MATNR = IM_MATNR. IF SY-SUBRC NE 0. RAISE EVENT NO_MATERIAL. "trigger the event ENDIF. ENDMETHOD. METHOD EVENT_HANDLER. WRITE :/ 'No material found'. "event handler method implementation ENDMETHOD.ENDCLASS. "CL_EVENTS

***************************************************************8 CLASS lcl_main DEFINITION. PUBLIC SECTION. DATA: v_num TYPE i. METHODS: process IMPORTING iv_num TYPE i. EVENTS: cutoff_reached.ENDCLASS. "lcl_main DEFINITION*CLASS lcl_main IMPLEMENTATION. METHOD process. v_num = iv_num. IF iv_num GE 2. RAISE EVENT cutoff_reached. ENDIF. ENDMETHOD. "processENDCLASS. "lcl_main IMPLEMENTATION

CLASS lcl_event_handler DEFINITION. PUBLIC SECTION. METHODS: handle_cutoff_reached FOR EVENT cutoff_reached OF lcl_main.ENDCLASS. "lcl_event_handler DEFINITION*CLASS lcl_event_handler IMPLEMENTATION. METHOD handle_cutoff_reached. WRITE: 'Event Processed'. ENDMETHOD. "handle_cutoff_reachedENDCLASS. "lcl_event_handler IMPLEMENTATION

START-OF-SELECTION. DATA: lo_main TYPE REF TO lcl_main. DATA: lo_event_handler TYPE REF TO lcl_event_handler.* CREATE OBJECT lo_main. CREATE OBJECT lo_event_handler. SET HANDLER lo_event_handler->handle_cutoff_reached FOR lo_main.* lo_main->process( 5 ).**

************************************EXAMPLE-2Event is a mechanism by which method of one class can raise method of another class, without the hazard of instantiating that class. It provides to raise the method (event handler method) of one class with help of another method in the same or different class (triggering method). The below steps is required to have the event handler in the class:- Create an event in a class. Create a triggering method in the same class which will raise the event. Create an event handler method for the event in same/other class. Register the event handler method in the program.Now, the above settings are complete for event handler in class. Create an object from the class containing the event and call the triggering method to raise the event. By taking the above steps, the following sample examples will demonstrate the event handler technique in Class. 1. Events with Handler Method in the same class. This example tells that how to raise method, if the triggering method and event handler method presents in the same class. Sample code and Output.

Next->

Output.

*********************************GLOBAL EVENTGo to SE24, provide class name as ZCL_SAPN_MATERIALS( we previously created this class see previous lesson) and click change.

Steps for creating and using event in SAP classes Step1:Define an event . Step2:Define a method. Step3:Link event and method and convert the method into event-handler method. Step4:Create a triggering method which will raise the event. Step5:Use set handler and register event handler method to a particular instance in the program.Define an event Go to Events tab and add a event as below.NO_MATERIAL-INSTANCE-PUBLIC-No material entered

Define a method Go to Methods tab and create a method as below NO_MATERIAL_HANDLER-INSTANCE-PUBLIC-Event Handler Method.

Save, double click on the method and add some code. WRITE:/ 'NO material entered'.

Save and activate immediately.Link event and method and convert the method into event-handler method.Now we have to link the method to event to make the method as event handler. Go to methods and put cursor on method NO_MATERIAL_HANDLER, click on detail view icon(see below image).

A pop up will open, provide description, select Event Handler for check box, provide our class name as ZCL_SAPN_MATERISLS and event name as NO_MATERIAL (Press F4), enter. You will see an icon(event handler icon), which means the method is event handler method.

Create a triggering method which will raise the eventEvent handler method is created, now we have to trigger that event, the vent can be triggered by using below syntax. RAISE EVENT .We will trigger the event for the method GET_MATERIAL_DTAILS (we previously created Get material details ), double click on the method GET_MATERIAL_DTAILS and add the below code. METHOD GET_MATERIAL_DTAILS.*Select material data from mara table into exporting parameter ex_mara (work area) for a material no im_matnr IF IM_MATNR IS INITIAL . RAISE EVENT NO_MATERIAL. ELSE. SELECT SINGLE * FROM MARA INTO EX_MARA WHERE MATNR = IM_MATNR.

ENDIF. ENDMETHOD.

Use set handler and register event handler method to a particular instance in the programNow we have to register this event in our SE38 program, to register a event handler method we use below syntax. SET HANDLER -> FOR . "here INSTANCE is object and EVENT HANDLER METHOD handler method created in se24Go to SE38, create a program ZSAPN_CLASS_EVENT and write the below code REPORT ZSAPN_CLASS_EVENT.DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS. "class decleration DATA WA_MARA TYPE MARA. "work area to store material data

PARAMETERS P_MATNR TYPE MARA-MATNR. "material input

CREATE OBJECT LO_MATERIAL. "create object for material calsssSET HANDLER LO_MATERIAL->NO_MATERIAL_HANDLER FOR LO_MATERIAL. "register event handler method

START-OF-SELECTION. CALL METHOD LO_MATERIAL->GET_MATERIAL_DTAILS "call method EXPORTING IM_MATNR = P_MATNR IMPORTING EX_MARA = WA_MARA.

WRITE : WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS.

Now execute the program with out giving any input, the result will be

SIGLETON CLASSA class which implements Singleton Pattern should have the following things other than the classs own data members and methods, 1. Private constructor(s) 2. A private static member of the same type of the class. 3. A static method which returns the only instance.Counter classs definition and implementation is as follows,REPORT ZFAR_SINGLETON.

class counter definition create private. "Rule 1

public section.

class-methods: get_counter returning value(obj) type ref to counter. "Rule 3

methods: increment, get_count returning value(cnt) type i.

private section. class-data instance type ref to counter. "Rule 2 data count type i.

endclass.

class counter implementation.

method get_counter. if instance is initial. create object instance. endif. obj = instance. endmethod.

method increment. add 1 to count. endmethod.

method get_count. cnt = count. endmethod.

endclass.

start-of-selection.

data: ctr1 type ref to counter, ctr2 type ref to counter, num1 type i, num2 type i.

ctr1 = counter=>get_counter( ).ctr1->increment( ).ctr2 = counter=>get_counter( ).ctr2->increment( ).

num1 = ctr1->get_count( ).num2 = ctr2->get_count( ).

write: num1, num2.