ABAP和JavaScript的懒加载,单例和桥接模式的实现和比较

According to Wikipedia Lazy loading is a design pattern commonly used in computer programming to defer initialization of an object until the point at which it is needed. It can contribute to efficiency in the program’s operation if properly and appropriately used.

In this blog, I will use an example of SCN log on to illustrate its usage in JavaScript and how to simulate the implementation in ABAP.

When we click log on link in SCN:

ABAP和JavaScript的懒加载,单例和桥接模式的实现和比较ABAP和JavaScript的懒加载,单例和桥接模式的实现和比较ABAP和JavaScript的懒加载,单例和桥接模式的实现和比较In this version, we use closure in JavaScript to achieve the lazy load behavior. The free variable, declared within createMask will not be exposed to external consumer, so it acts actually as a private member attribute in OO world and is the best candidate to buffer the created div element instance. Till now we are very near to the final implementation but this version still have space to improve.


Drawback of this version

From single responsibility principle point of view, the function createMask mixes the mask div element creation with element buffering. Is there any approach to decouple these two different tasks?


Pseudo Implementation version 5 – the final oneABAP和JavaScript的懒加载,单例和桥接模式的实现和比较In this version we use a Bridge design pattern to decouple the concrete staff ( mask div element creation ) with the abstract staff ( created element must be singleton ), so that each staff can change independently without influence on the other.


Lazy Loading in ABAP

In ABAP actually it is also very easy to implement Lazy Loading: we just declare some private member attribute in class or static variable in function module for instance buffer. Nevertheless, let’s try to simulate the implementation done in JavaScript with Bridge pattern, in order to deepen our understanding on Bridge pattern as an ABAPer.

I have a dummy class to simulate DOM node:ABAP和JavaScript的懒加载,单例和桥接模式的实现和比较ABAP和JavaScript的懒加载,单例和桥接模式的实现和比较ABAP和JavaScript的懒加载,单例和桥接模式的实现和比较

REPORT zsingleton_test.

DATA(lv_new_fm) = zcl_singleton_factory=>get( iv_func = 'ZCREATE_MASK' ).
DATA: lo_node1 TYPE REF TO zcl_dom_node,
      lo_node2 TYPE REF TO zcl_dom_node,
      lo_node3 TYPE REF TO zcl_dom_node,
      lo_node4 TYPE REF TO zcl_dom_node.
CALL FUNCTION lv_new_fm
   EXPORTING
      iv_node_name = 'Jerry'
   IMPORTING
      eo_node = lo_node1.

CALL FUNCTION lv_new_fm
   EXPORTING
      iv_node_name = 'Jerry'
   IMPORTING
      eo_node = lo_node2.

CALL FUNCTION lv_new_fm
   EXPORTING
      iv_node_name = 'Java'
   IMPORTING
      eo_node = lo_node3.

CALL FUNCTION lv_new_fm
   EXPORTING
      iv_node_name = 'Java'
   IMPORTING
      eo_node = lo_node4.

zcl_singleton_factory=>cleanup( ).

A new function module is dynamically generated by ZCL_SINGLETON_FACTORY based on original function module ZCREATE_MASK. The new function module name is stored in variable lv_new_fm.

The new function module has exactly the same signature as ZCREATE_MASK. When it is called, we can observed that the singleton is fulfilled.

ABAP和JavaScript的懒加载,单例和桥接模式的实现和比较ABAP和JavaScript的懒加载,单例和桥接模式的实现和比较ABAP和JavaScript的懒加载,单例和桥接模式的实现和比较ABAP和JavaScript的懒加载,单例和桥接模式的实现和比较And in ABAP, due to lack of support in language perspective, I spend totally 351 lines of ABAP codes for simulation.

Hope this small simulation can help you as ABAPers to understand some language features in JavaScript.


Further reading

I have written a series of blogs which compare the language feature among ABAP, JavaScript and Java. You can find a list of them below:


Lazy Loading, Singleton and Bridge design pattern in JavaScript and in ABAP

Functional programming – Simulate Curry in ABAP

Functional Programming – Try Reduce in JavaScript and in ABAP

Simulate Mockito in ABAP

A simulation of Java Spring dependency injection annotation @Inject in ABAP

Singleton bypass – ABAP and Java

Weak reference in ABAP and Java

Fibonacci Sequence in ES5, ES6 and ABAP

Java byte code and ABAP Load

How to write a correct program rejected by compiler: Exception handling in Java and in ABAP

An small example to learn Garbage collection in Java and in ABAP

String Template in ABAP, ES6, Angular and React

Try to access static private attribute via ABAP RTTI and Java Reflection




上一篇:Photoshop鼠绘教程:绘制一副水墨映日荷花图


下一篇:我不是用户体验设计师,你呢?