前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ABAP实现设计模式里的观察者-发布者模式

ABAP实现设计模式里的观察者-发布者模式

作者头像
Jerry Wang
发布2020-09-01 09:50:15
3280
发布2020-09-01 09:50:15
举报

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.

代码语言:javascript
复制
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:

代码语言:javascript
复制
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( ).
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-08-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • The requirement
  • Some keypoints to finish this question
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档