Object oriented Events in ABAP
As i was moving into object oriented world i 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 are the ones that raise the events or expose the events.
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).
When do we use Events in Our Apps
We need to decide whether we need events in our applications.
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!!
How do we use Events in Our Apps
Key Approach: INTERFACES IS THE KEY!!
I am going to use a scenario here
Scenario: we have an application of editors like word, excel
And users are interacting with 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
Advantages: Loose Coupling of the Objects
As a summary events are quite useful and enable loose coupling of the objects
They dont need to know each other verl well
And that gives great flexibility.
I hope it was clear and you find it useful!!
Please comment on anything