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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值