CLASS zcl_person DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
METHODS done .
METHODS constructor
IMPORTING
!iv_name TYPE string .
METHODS eat
IMPORTING
!iv_sth TYPE string
RETURNING
VALUE(ro_this) TYPE REF TO zcl_person .
METHODS sleep
IMPORTING
!iv_seconds TYPE int4
RETURNING
VALUE(ro_this) TYPE REF TO zcl_person .
METHODS sleep_first
IMPORTING
!iv_seconds TYPE int4
RETURNING
VALUE(ro_this) TYPE REF TO zcl_person .
PROTECTED SECTION.
PRIVATE SECTION.
TYPES:
BEGIN OF ty_task,
task_type TYPE string,
task_param TYPE string,
END OF ty_task .
TYPES:
tt_task TYPE STANDARD TABLE OF ty_task WITH KEY task_type task_param .
DATA mt_task TYPE tt_task .
METHODS perform_eat
IMPORTING
!iv_sth TYPE string .
METHODS subscribe
IMPORTING
!iv_type TYPE string
!iv_param TYPE string .
METHODS publish .
METHODS do_task
IMPORTING
!is_task TYPE ty_task .
METHODS perform_sleep
IMPORTING
!iv_param TYPE string .
ENDCLASS.
CLASS ZCL_PERSON IMPLEMENTATION.
METHOD constructor.
WRITE: / 'Hello, I am ' , iv_name.
ENDMETHOD.
METHOD done.
publish( ).
ENDMETHOD.
METHOD do_task.
CASE is_task-task_type.
WHEN 'eat'.
perform_eat( is_task-task_param ).
WHEN 'sleep' OR 'sleepFirst'.
perform_sleep( is_task-task_param ).
WHEN OTHERS.
ENDCASE.
ENDMETHOD.
METHOD eat.
subscribe( iv_type = 'eat' iv_param = iv_sth ).
ro_this = me.
ENDMETHOD.
METHOD perform_eat.
WRITE: / 'eat ' , iv_sth.
ENDMETHOD.
METHOD perform_sleep.
DATA(lv_second) = CONV int4( iv_param ).
WAIT UP TO lv_second SECONDS.
WRITE:/ ' wake up after sleep for ' , iv_param, ' seconds.'.
ENDMETHOD.
METHOD publish.
LOOP AT mt_task ASSIGNING FIELD-SYMBOL(<task>).
do_task( <task> ).
ENDLOOP.
CLEAR: mt_task.
ENDMETHOD.
METHOD sleep.
subscribe( iv_type = 'sleep' iv_param = CONV string( iv_seconds ) ).
ro_this = me.
ENDMETHOD.
METHOD sleep_first.
subscribe( iv_type = 'sleepFirst' iv_param = CONV string( iv_seconds ) ).
ro_this = me.
ENDMETHOD.
METHOD subscribe.
DATA(ls_task) = VALUE ty_task( task_type = iv_type task_param = iv_param ).
IF iv_type = 'sleepFirst'.
INSERT ls_task INTO mt_task INDEX 1.
ELSE.
APPEND ls_task TO mt_task.
ENDIF.
ENDMETHOD.
ENDCLASS.
REPORT ZSUBSCRIBER.
data(jerry) = new ZCL_PERSON( 'Jerry' ).
jerry->eat( 'breakfast' )->eat( 'lun' )->eat( 'supper' )->done( ).
jerry->eat( 'apple' )->sleep( 1 )->eat( 'pear' )->done( ).
jerry->eat( 'banana' )->sleep_first( 1 )->eat( 'grape' )->done( ).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
4 | |
3 | |
3 | |
2 | |
2 | |
2 | |
2 | |
1 | |
1 | |
1 |