Skip to Content

Object oriented Events in ABAP

As i was moving into object oriented world ı come across with the concept EVENTS

Why Events?

Well, something important has happened

Someone needs to know!!

I started looking at the events, which was understandable but not really!!

There is a channel where there is the

  • publishers
  • subscribers

Publishers are the ones that raise the event or expose the event.

Subscribers are the ones that are interested in these events.

When i look at the examples they all give similar examples mostly on the Gui side of the things.

Events make more sense in the user interface at the end

Where as it makes more sense as the application get bigger and bigger

Before events in oo world there was Business Transaction Events that i get to know.

so there was events in that sense before

For e.g. Class CL_GUI_ALV_GRID for ALV Grid has published event like USER_COMMAND, DOUBLE_CLICK and ONDRAG etc which could be used by other objects (programs to subscribe and use).

How do we use Events in Our Apps

We need to decide whether we need events in our applications.

Define: is there an important news that involves some objects to know( coupling)

If the answer is yes and you are enthusiastic

Apply the events in your objects!!

Scenario:  i  am going to use a scenario here, lets say we have an application of  editors like word, excel

And the user is using these tools to create documents.

So we have word application excel application etc…

Document creation is going to be our main event!!

in my opinion better that all the events are exposed through interfaces!!

important:The full code is attached as a zip file you can see the other object like lcl_document below!!

Step 1 — Define the publisher interface ( events are here)

in Code:

**Publisher

interface lif_event.

   EVENTS: document_created.

ENDINTERFACE.

Step 2 Define the Listener interface( who needs to know)

***Listener

INTERFACE lif_listener.

   METHODs: on_document_created FOR EVENT document_created

   of lif_event.

ENDINTERFACE.

Step 3 – Then comes to play the event publishers that implement the publishing interface.

All the listeners are registered through the interface

class lcl_word_app DEFINITION.

   PUBLIC SECTION.

     INTERFACES lif_publisher.

methods: create_document EXPORTING eo_document TYPE REF TO lcl_document.

ENDCLASS.

class lcl_excel_app DEFINITION.

   PUBLIC SECTION.

     INTERFACEs lif_publisher.

methods: create_document EXPORTING eo_document TYPE REF TO lcl_document.

ENDCLASS.

Step 4– Event listeners who are interested in the events

******Listeners go here!!!!!!

class lcl_listener_1 DEFINITION FINAL.

   PUBLIC SECTION.

     INTERFACES lif_listener.

     ALIASES on_document_created for lif_listener~on_document_created.

ENDCLASS.

class lcl_listener_2 DEFINITION FINAL.

   PUBLIC SECTION.

     INTERFACES lif_listener.

     ALIASES on_document_created for lif_listener~on_document_created.

ENDCLASS.

Last Steps of course:

***** is the User

class lcl_user DEFINITION.

   PUBLIC SECTION.

     class-data: lcl_word_app TYPE REF TO lcl_word_app,

                 lcl_excel_app  TYPE REF TO lcl_excel_app.

     CLASS-METHODS: class_constructor.

     METHODS: raise_word_event.

     METHODS: raise_excel_event.

ENDCLASS.

You can see the full code in the report z_test_events

I hope it was clear and you find it useful!!

Please comment on anything

To report this post you need to login first.

4 Comments

You must be Logged on to comment or reply to a post.

  1. Suhas Saha

    I started looking at the events, which was understandable but not really!!

    There is a channel where there is the

    • publishers
    • subscribers

    OO Events are used to implement the “Observer” pattern.

    Define: is there an important news that involves some objects to know( coupling)

    You have correctly observed (pun intended) the advantage of using events. They can loosely couple your objects 🙂

    Cheers,

    Suhas

    (0) 

Leave a Reply