SAP ABAP OO Event

1. Define Event: Event would be defined at the time of class declaration.
When using standard SAP class, this is need not required generally.
However for custom classes you can define events.      

*----------------------------------------------------------------------*
*       CLASS c1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class c1 definition.
  public section.
    events: e1, e2.
    methods m1.
endclass.                    "c1 DEFINITION


2. Raise Event: Events can be raised by any method of class. This is to be
done using ABAP statement.

*----------------------------------------------------------------------*
*       CLASS c1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class c1 implementation.
  method m1.
    raise event e1.
    raise event e2.
  endmethod.                    "m1
endclass.                    "c1 IMPLEMENTATION

3. Define Handler Class: Handler class is the one which is responsible for taking
action on the events subscribed by other object. It could include events of
different object types (classes). Handler class can group handler methods of
different events from one/ many classes.

*----------------------------------------------------------------------*
*       CLASS c2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class c2 definition.
  public section.
    methods m2 for event e1 of c1.
endclass.                    "c2 DEFINITION

*----------------------------------------------------------------------*
*       CLASS c3 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class c3 definition.
  public section.
    methods m3 for event e2 of c1.
endclass.                    "c3 DEFINITION


4. Define Handler Method: Handler Methods are the one which describe the action to
be performed when the event is raised.

*----------------------------------------------------------------------*
*       CLASS c2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class c2 implementation.
  method m2.
    write / 'M2 handling event'.
  endmethod.                    "m2
endclass.                    "c2 IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS c3 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class c3 implementation.
  method m3.
    write / 'M3 handling event'.
  endmethod.                    "m3
endclass.                    "c3 IMPLEMENTATION

5. Register Event Handler: This would mean subscribing to the event of
a particular class.

data: cref1 type ref to c1,
      cref2 type ref to c2,
      cref3 type ref to c3.

create object: cref1, cref2, cref3.

set handler cref2->m2 cref3->m3 for cref1.

call method cref1->m1.

 

*----------------------------------------------------------------------*
*       CLASS c1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class c1 definition.
  public section.
    events: e1, e2.
    methods m1.
endclass.                    "c1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS c2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class c2 definition.
  public section.
    methods m2 for event e1 of c1.
endclass.                    "c2 DEFINITION

*----------------------------------------------------------------------*
*       CLASS c3 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class c3 definition.
  public section.
    methods m3 for event e2 of c1.
endclass.                    "c3 DEFINITION

data: cref1 type ref to c1,
      cref2 type ref to c2,
      cref3 type ref to c3.

create object: cref1, cref2, cref3.

set handler cref2->m2 cref3->m3 for cref1.

call method cref1->m1.

*----------------------------------------------------------------------*
*       CLASS c1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class c1 implementation.
  method m1.
    raise event e1.
    raise event e2.
  endmethod.                    "m1
endclass.                    "c1 IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS c2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class c2 implementation.
  method m2.
    write / 'M2 handling event'.
  endmethod.                    "m2
endclass.                    "c2 IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS c3 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class c3 implementation.
  method m3.
    write / 'M3 handling event'.
  endmethod.                    "m3
endclass.                    "c3 IMPLEMENTATION


你可能感兴趣的:(SAP,ABAP,OO)