Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Multiple events and one event handler.....

former_member209696
Participant
0 Likes
1,781

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( ).

1 ACCEPTED SOLUTION
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,110

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

4 REPLIES 4
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,111

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

Read only

0 Likes
1,110

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?

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,110

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:

  • Front & Rear Brakes - Characteristic of a vehicle - Class attributes,
  • Operating the front and/or rear brakes - Action performed by the vehicle - Class method
  • What action to take when brakes are pressed? - STOP - Event raised when the brakes are pressed. Now you have to identify which wheels (front/rear) to stop: pass an exporting param through this event E_WHEEL.
  • Braking system - It handles the event STOP & identifies which wheel to work upon based on the param E_WHEEL.

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

Read only

0 Likes
1,110

Dear Suhas,

     It very nice explanation. Keep it up... Thank u...