ABAP实现设计模式里的观察者-发布者模式-阿里云开发者社区

开发者社区> jerrywangsap> 正文

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.
+关注继续查看

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.


image.png


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:


image.png


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

image.png



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.

image.png


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


image.png


The complete source code of ZCL_PERSON.

image.pngimage.pngimage.pngimage.png

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
有限状态机问题编程实践(下)
有限状态机问题编程实践(下)
8 0
关于设计模式--我有话说
掌握设计模式并不是件很难的事情,关键在于多思考,多实践,不要听到人家说懂几个设计模式就很“牛”,只要用心学习,设计模式也就那么回事,你也可以很“牛”的,一定要有信心。
17 0
学习go语言国内最全资料链接
就最近和各位大佬认识下来,以前觉得学习go语言,可能资料比较少,可是后来才发现,原来资料并不少,甚至可以说通过大家的努力,go社区已经非常包容且完善了
21 0
【技术干货】40页PPT分享万亿级交易量下的支付平台设计(2)
【技术干货】40页PPT分享万亿级交易量下的支付平台设计(2)
13 0
备战5月软考--软件设计师
1.上午场需要做的就是不停的刷题,每个科目的题都要会个大概(大概有11个大考点) 2.下午场需要做的也是不停的做题,下午场的题型比较固定,做来做出都是那几类,自己做完总结适合自己的做题技巧第一题:数据流图方面的设计(实体Ei的名称、数据存储Di的名称,数据流图等)第二题:数据库设计(E-R图、完整性约束等)第三题:用例描述(识别参与者、用例名称、类名等)第四题:算法题第五题:设计模式的Java代码第六题:设计模式的C++代码
12 0
看完这篇,你不可能不懂「动态代理」
四个版本迭代,带你一步步模拟JDK动态代理
13 0
iLogtail 与Filebeat 性能对比
前段时间, iLogtail 阿里千万实例可观测采集器开源,其中介绍了iLogtail采集性能可以达到单核100MB/s,相比开源采集Agent有5-10倍性能优势。很多小伙伴好奇iLogtail具体的性能数据和资源消耗如何,本文将针对目前业界使用度较高且性能相对较优的Agent FileBeat进行对比,测试这两个Agent在不同压力场景下的表现如何。
9 0
2022年的Flag从搭建博客开始,设计模式,数据结构,算法汇总【手摸手系列】
2022年的Flag从搭建博客开始,设计模式,数据结构,算法汇总【手摸手系列】
4 0
【技术干货】40页PPT分享万亿级交易量下的支付平台设计(3)
【技术干货】40页PPT分享万亿级交易量下的支付平台设计(3)
15 0
QT和MFC的优缺点比较
QT和MFC的优缺点比较
5 0
+关注
2628
文章
0
问答
来源圈子
更多
+ 订阅
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载