专栏首页Jerry的SAP技术分享ABAP实现设计模式里的观察者-发布者模式

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

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • SAP CRM Fiori My task应用里roundtrip取舍的讨论

    问题是点了My task tile之后,进到task list view之后的第一个batch 操作是否可以去掉:

    Jerry Wang
  • Gradle中的默认任务和任务依赖关系设置

    Jerry Wang
  • SAP Spartacus Organization Unit List三个按钮的技术实现

    如下图所示:Expand all,Collapse All和Add三个button的技术实现:

    Jerry Wang
  • 在python里创建一个任务(Task)实例

    与事件循环进行交互,最基本的方式就是任务,任务封装了协程和自动跟踪它的状态。任务是Future类的子类,所以其它协程可以等待任务完成,或当这些任务完成获取返回结...

    砸漏
  • 使用宝塔控制面板建站时出现网页出现404错误怎么办?

    发迹创业网,最近在使用宝塔面板建站的时候发现,网站建设好之后,打开网页提示“404错误,文件不存在”,这是怎么回事呢?重新检查了一下,所有的步骤都是按照以前的流...

    发迹创业网www.082765.com
  • Kafka运维填坑Kafka源码分析-汇总

    调用Runtime.getRuntime.halt(1)直接暴力退出了. 可参考Kafka issue: Unclean leader election an...

    扫帚的影子
  • VS Code的keil插件使用方法_真香!

    作为一个51单片机或STM32单片机的使用者,keil一直是我们的必备的一款工具之一。但kei的一些问题也一直存在,被人吐槽最多的就是编辑器功能单一、提示功能不...

    MCU起航
  • Radix通过优质域名销售额超138万美元

    根据1月12日外媒报道,Radix注册局近日发布了一份报告:在2017年下半年,Radix通过销售优质域名,销售额总超138万美元,约合人民币8...

    躲在树上的域小名
  • Kubernetes 1.13:Kubeadm简化群集管理、容器存储接口(CSI)和CoreDNS作为默认DNS现已普遍可用

    Kubernetes 1.13是迄今为止发布的最短版本之一,10周内。此版本继续关注Kubernetes的稳定性和可扩展性,其中在存储和群集生命周期领域的三个主...

    CNCF
  • CRM WebClient UI outbound plug的使用

    An example why we need to configure outbound plug mapping

    Jerry Wang

扫码关注云+社区

领取腾讯云代金券