ABAP Objects Design Patterns – Observer

By | October 31, 2011 | ABAP Objects, OO Design Patterns | 17,880 | 0

The intent behind the Observer Design Pattern is to define a dependecy between objects so that when main object changes its state, all the dependent objects are notified. Its upto the dependent objects to update themselves or ignore the notification.

Design time consideration

  1. In the main object, AKA Subject, create a Event. Also add create a signature to the event.
  2. Create class with Event Handler method for the event of the Subject.
  3. Inherit the Subclasses for different behavior. Redefine the event handler method for which any behavior is required.
  4. Register the event handler using the statement SET EVENT HANDLER ... FOR O_SUBJECT.

UML

Lets see the UML for the example.

We have a Subject class as MAINPROCESS. This class has the event as STATE_CHANGED. We would trigger the event whenever there is any change in the state of the object. Thus, we raise the event when we set the new status of the object – attribute CURRENT_STATE using the method SET_STATE( ).

We have an abstract super class MYFUNCTION. We created event handler method ON_STATE_CHANGED for the event STATE_CHANGED of class MAINPROCESS. We inherite two classes MYALV and MYDB from it. We redefine the methods to refresh their status.

Code Lines

Here is the Code lines.

program znp_dp_observer.

*====
CLASS mainprocess DEFINITION.
  PUBLIC SECTION.
    METHODSset_state IMPORTING iv_state TYPE char1.
    EVENTSstate_changed EXPORTING value(new_stateTYPE char1.
  PRIVATE SECTION.
    DATAcurrent_state TYPE char1.
ENDCLASS.                    "mainprocess DEFINITION
*
CLASS mainprocess IMPLEMENTATION.
  METHOD set_state.
    current_state iv_state.
    SKIP 2.
    WRITE'Main Process new state'current_state.
    RAISE EVENT state_changed EXPORTING new_state current_state.
  ENDMETHOD.                    "set_state
ENDCLASS.                    "mainprocess IMPLEMENTATION

*====
CLASS myfunction DEFINITION ABSTRACT.
  PUBLIC SECTION.
    METHODSon_state_changed ABSTRACT
      FOR EVENT state_changed OF mainprocess
      IMPORTING new_state.
ENDCLASS.                    "myfunction DEFINITION

*====
CLASS myalv DEFINITION INHERITING FROM myfunction.
  PUBLIC SECTION.
    METHODSon_state_changed REDEFINITION.
ENDCLASS.                    "myalv DEFINITION
*
CLASS myalv IMPLEMENTATION.
  METHOD on_state_changed.
    WRITE'New state in ALV processing'new_state.
  ENDMETHOD.                    "on_state_changed
ENDCLASS.                    "myalv IMPLEMENTATION
*====
CLASS mydb DEFINITION INHERITING FROM myfunction.
  PUBLIC SECTION.
    METHODSon_state_changed REDEFINITION.
ENDCLASS.                    "mydb DEFINITION
*
CLASS mydb IMPLEMENTATION.
  METHOD on_state_changed.
    WRITE'New State in DB processing'new_state.
  ENDMETHOD.                    "on_state_changed
ENDCLASS.                    "mydb IMPLEMENTATION

*====
CLASS mainapp DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODSrun.
ENDCLASS.                    "mainapp DEFINITION
*
CLASS mainapp IMPLEMENTATION.
  METHOD run.

    DATAlo_process TYPE REF TO mainprocess.
    DATAlo_alv TYPE REF TO myalv.
    DATAlo_db TYPE REF TO mydb.

*   Instantiate the objects
    CREATE OBJECT lo_process.
    CREATE OBJECTlo_alvlo_db.

*   Event handlers
    SET HANDLER lo_alv->on_state_changed FOR lo_process.
    SET HANDLER lo_db->on_state_changed FOR lo_process.

*   Set new state
    lo_process->set_state'A' ).
    lo_process->set_state'B' ).
    lo_process->set_state'C' ).

  ENDMETHOD.                    "run
ENDCLASS.                    "mainapp IMPLEMENTATION

START-OF-SELECTION.
  mainapp=>run).

Explanation

When we call method SET_STATUS of the object LO_PROCESS, it raises the event STATE_CHANGED. This would be caught in the event handler ON_STATE_CHANGED. This is possible with the statement, SET HANDLER lo_alv->on_state_changed FOR lo_process.

Output

The test program generate this output:

Don’t miss to read, The Case Study: Observer Design Pattern Usage to know more about how observer can be implemented in real-world.

SDN WikiObserver Design Pattern
SDN ForumObserver Design Pattern: Looking for redesign ABAP OO code example

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.