1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
*&---------------------------------------------------------------------* *& Report ZP_DEMO_CLASS_CNT_EVENT *&---------------------------------------------------------------------* REPORT zp_demo_class_cnt_event. *----------------------------------------------------------------------* * CLASS counter DEFINITION *----------------------------------------------------------------------* CLASS counter DEFINITION. PUBLIC SECTION. METHODS increment_counter. EVENTS critical_value EXPORTING value(excess) TYPE i. PRIVATE SECTION. DATA: count TYPE i, threshold TYPE i VALUE 10. ENDCLASS. "counter DEFINITION *----------------------------------------------------------------------* * CLASS counter IMPLEMENTATION *----------------------------------------------------------------------* CLASS counter IMPLEMENTATION. METHOD increment_counter. DATA diff TYPE i. ADD 1 TO count. IF count > threshold. diff = count - threshold. RAISE EVENT critical_value EXPORTING excess = diff. ENDIF. ENDMETHOD. "increment_counter ENDCLASS. "counter IMPLEMENTATION *----------------------------------------------------------------------* * CLASS handler DEFINITION *----------------------------------------------------------------------* CLASS handler DEFINITION. PUBLIC SECTION. METHODS handle_excess FOR EVENT critical_value OF counter IMPORTING excess. ENDCLASS. "handler DEFINITION *----------------------------------------------------------------------* * CLASS handler IMPLEMENTATION *----------------------------------------------------------------------* CLASS handler IMPLEMENTATION. METHOD handle_excess. WRITE: / 'Excess is', excess. ENDMETHOD. "handle_excess ENDCLASS. "handler IMPLEMENTATION DATA: r1 TYPE REF TO counter, h1 TYPE REF TO handler. START-OF-SELECTION. CREATE OBJECT: r1, h1. SET HANDLER h1->handle_excess FOR ALL INSTANCES. DO 20 TIMES. CALL METHOD r1->increment_counter. ENDDO. |
정리하면, 3가지 역할이 필요하다. 즉, 1) Event를 등록하고 2) Event를 발생(Raise)하고 3) 발생된 Event를 handling 하는 것. 여기서는 ‘Event Trigger’ 클래스에서 1)과 2)를, ‘Event Handler’ 클래스에서 3)을 처리하는 방식을 취하고 있다.
구체적으로 보면,
- SET HANDLER 구문은 handler table를 만든다. 이 테이블은 ① handler methods의 이름과 ② 그 handler를 갖고 있는 클래스 인스턴스의 reference로 구성되어있다.
- 상기 클래스 인스턴스는 초기화 되더라도 garbage collection 대상이 되지 않는다. Handler table에 등록되어 떡 버티고 있기 때문이다.
- For static events, the system creates an instance-independent handler table for the relevant class.