Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
JerryWang
Advisor
Advisor
1,158
This is an interview question from Wechat development team. The candidates are required to answer with JavaScript. Nevertheless I think it is also beneficial for an ABAPer if we master the design pattern contained in this question - Publish and Subscribe pattern.

The requirement


1. The chain operations could be performed on instance of class ZCL_PERSON.

For example, the red line 10 in ABAP code should generate the following output highlighted in red, and blue and green color accordingly.

2. The "sleep_first" operation has the highest priority, see ABAP line 14 and its output for reference.



Some keypoints to finish this question


1. the operation call must support chain invoke style, which means each call must return current instance as returning parameter.

2. In order to support the prioritization of Sleep first operation, during each operation call, the activity must not be executed immediately, or else the Sleep first operation will never have chance to be shifted to call before others. Instead, the detail information of each operation should be subscribed into a task queue, so that when a new element is inserted into that queue, we can have flexibility to adapt the order of each element in the queue, that is, if the Sleep first call is enqueued, it must always be positioned in the queue header.

The signature and implementation of subscribe method:



When eat or sleep is called, simply log it via subscribe method and return current instance for chain invoke.



The declaration of the series of calls ends up with method done, which calls private publish method, which loops all elements in the task queue and deal with each one by one.



The do_task has a CASE-WHEN structure to dispatch the call to dedicated handler method according to operation type:



The complete source code of ZCL_PERSON.


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.

Test report:


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( ).







2 Comments
Labels in this area