2012 Jun 08 7:47 AM
Hi Experts,
I have requirement to handle 2 events using one method.
For Eg:
- I have a class LCL_CAR.
- I have 2 events inside that class.( brake and swith on )
- I want to handle both events using one method
Is it possible? if yes How?
Actually I achieved this by below code....But am not sure is this correct way or not.. below is the code
class lcl_car definition.
public section.
Events: switch_on, brake.
methods : event_raise.
methods : event_handler1 for event switch_on of lcl_car.
methods : event_handler2 for event brake of lcl_car.
endclass.
class lcl_car implementation.
method event_raise.
raise event switch_on.
raise event brake.
endmethod.
method event_handler1.
write:/'Light is switched ON'.
endmethod.
method event_handler2.
raise event switch_on.
endmethod.
endclass.
start-of-selection.
data: ref_car type ref to lcl_car.
create object ref_car.
set handler ref_car->event_handler1 for ref_car.
set handler ref_car->event_handler2 for ref_car.
ref_car->event_raise( ).
2012 Jun 08 9:34 AM
Hello Muhammed,
Imho if you want the same event handling to be same for two events, then what is the use of creating two different events?
In your case 'Light is switched ON' if you switch_on as well as when you brake, correct? So what is the purpose of these 2 events - there is nothing to differentiate between these?
Suppose you application wants to turn on the front lights when you switch on the lights & turn on the tail lights when you brake (more practical example).
Let's see how to achieve this:
CLASS lcl_car DEFINITION.
PUBLIC SECTION.
METHODS:
switch_light,
press_brakes.
EVENTS: switch_on EXPORTING value(e_type) TYPE char1.
ENDCLASS. "lcl_car DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_car IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_car IMPLEMENTATION.
METHOD switch_light.
RAISE EVENT switch_on
EXPORTING e_type = 'F'. "Switch on the front lights
ENDMETHOD. "event_raise
METHOD press_brakes.
RAISE EVENT switch_on
EXPORTING e_type = 'T'. "Switch on the tail lights
ENDMETHOD. "event_handler1
ENDCLASS. "lcl_car IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS lcl_lights DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_lights DEFINITION.
PUBLIC SECTION.
METHODS: handle_lights_on FOR EVENT switch_on OF lcl_car IMPORTING e_type.
PRIVATE SECTION.
METHODS: turn_on_light IMPORTING i_type TYPE char1.
ENDCLASS. "lcl_lights DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_lights IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_lights IMPLEMENTATION.
METHOD turn_on_light.
CASE i_type.
WHEN 'F'. "Front lights
WRITE: / `Turn on the front lights`.
WHEN 'T'. "Tail lights
WRITE: / `Turn on the tail lights`.
WHEN OTHERS.
ENDCASE.
ENDMETHOD. "turn_on_light
METHOD handle_lights_on.
me->turn_on_light( e_type ).
ENDMETHOD. "handle_lights_on
ENDCLASS. "lcl_lights IMPLEMENTATION
START-OF-SELECTION.
DATA: go_car TYPE REF TO lcl_car,
go_lights TYPE REF TO lcl_lights.
*Instantiate the objects
CREATE OBJECT: go_car, go_lights.
*Set the event handlers
SET HANDLER: go_lights->handle_lights_on FOR go_car.
go_car->switch_light( ). "Turn on the front light
go_car->press_brakes( ). "Turn on the rear light
Now suppose you want to turn on the right indicator when you take a right, do you define a new event for this or export an event param & handle it in the handler
Hope i'm clear.
BR,
Suhas
2012 Jun 08 9:34 AM
Hello Muhammed,
Imho if you want the same event handling to be same for two events, then what is the use of creating two different events?
In your case 'Light is switched ON' if you switch_on as well as when you brake, correct? So what is the purpose of these 2 events - there is nothing to differentiate between these?
Suppose you application wants to turn on the front lights when you switch on the lights & turn on the tail lights when you brake (more practical example).
Let's see how to achieve this:
CLASS lcl_car DEFINITION.
PUBLIC SECTION.
METHODS:
switch_light,
press_brakes.
EVENTS: switch_on EXPORTING value(e_type) TYPE char1.
ENDCLASS. "lcl_car DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_car IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_car IMPLEMENTATION.
METHOD switch_light.
RAISE EVENT switch_on
EXPORTING e_type = 'F'. "Switch on the front lights
ENDMETHOD. "event_raise
METHOD press_brakes.
RAISE EVENT switch_on
EXPORTING e_type = 'T'. "Switch on the tail lights
ENDMETHOD. "event_handler1
ENDCLASS. "lcl_car IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS lcl_lights DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_lights DEFINITION.
PUBLIC SECTION.
METHODS: handle_lights_on FOR EVENT switch_on OF lcl_car IMPORTING e_type.
PRIVATE SECTION.
METHODS: turn_on_light IMPORTING i_type TYPE char1.
ENDCLASS. "lcl_lights DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_lights IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_lights IMPLEMENTATION.
METHOD turn_on_light.
CASE i_type.
WHEN 'F'. "Front lights
WRITE: / `Turn on the front lights`.
WHEN 'T'. "Tail lights
WRITE: / `Turn on the tail lights`.
WHEN OTHERS.
ENDCASE.
ENDMETHOD. "turn_on_light
METHOD handle_lights_on.
me->turn_on_light( e_type ).
ENDMETHOD. "handle_lights_on
ENDCLASS. "lcl_lights IMPLEMENTATION
START-OF-SELECTION.
DATA: go_car TYPE REF TO lcl_car,
go_lights TYPE REF TO lcl_lights.
*Instantiate the objects
CREATE OBJECT: go_car, go_lights.
*Set the event handlers
SET HANDLER: go_lights->handle_lights_on FOR go_car.
go_car->switch_light( ). "Turn on the front light
go_car->press_brakes( ). "Turn on the rear light
Now suppose you want to turn on the right indicator when you take a right, do you define a new event for this or export an event param & handle it in the handler
Hope i'm clear.
BR,
Suhas
2012 Jun 08 10:24 AM
Hai Suhas,
Thank you for your reply..... Its very helpful..... But still am not clear.... Is it possible to create one handler method for 2 different events.....
Eg: In a vehicle FRONT BRAKE and BACK BRAKE is there,,,,
Consider these 2 as event and method which handling both events are STOP
My question is Is this possible or not?
2012 Jun 08 10:52 AM
Hello Muhammed,
I don't agree with your perception of the situation. As per my understanding events are "actionable items". Both front & rear brakes do the same thing - STOP the vehicle.
So the class design should look something like this:
Technically it is not possible to have the same method handle multiple events. In ABAP there exists an 1:n relation between events & event handlers.
BR,
Suhas
2012 Jun 08 11:14 AM
Dear Suhas,
It very nice explanation. Keep it up... Thank u...