Fast and easy creation of a multition classe using an AdT template (EWM example)

I wrote at how – in EWM – I often want to use multiton classes.

And as I don’t want to type a lot, I made myself a AdT template. This is it:

Spoiler: the variable ${enclosing_object} is what makes it so god!

CLASS ${enclosing_object} DEFINITION PUBLIC FINAL CREATE PRIVATE. PUBLIC SECTION. CLASS-METHODS: get_instance IMPORTING iv_lgnum TYPE /scwm/lgnum RETURNING VALUE(rref_instance) TYPE REF TO ${enclosing_object}. PROTECTED SECTION. PRIVATE SECTION. TYPES: BEGIN OF gty_instance_buffer, lgnum TYPE /scwm/lgnum, instance TYPE REF TO ${enclosing_object}, END OF gty_instance_buffer. CLASS-DATA: st_instance_buffer TYPE HASHED TABLE OF gty_instance_buffer WITH UNIQUE KEY lgnum. METHODS constructor IMPORTING iv_lgnum TYPE /scwm/lgnum. DATA: mv_lgnum TYPE /scwm/lgnum.
ENDCLASS. CLASS ${enclosing_object} IMPLEMENTATION. METHOD constructor. me->mv_lgnum = iv_lgnum. ENDMETHOD. METHOD get_instance. READ TABLE st_instance_buffer WITH TABLE KEY lgnum = iv_lgnum ASSIGNING FIELD-SYMBOL(<lfs_instance>). IF sy-subrc EQ 0. rref_instance = <lfs_instance>-instance. ELSE. rref_instance = NEW ${enclosing_object}( iv_lgnum = iv_lgnum ). INSERT VALUE #( lgnum = iv_lgnum instance = rref_instance ) INTO TABLE st_instance_buffer. ENDIF. ENDMETHOD.

And here is ho to use it:

1. Creat a class in ATD. It will come with some code:

A newly created class in AdT

2. strg+A select all and delete everything.

3. type multi (or whatever you name the template) + space for completion suggestions, select the right template:


AdT template-suggestion

4. and hit enter.
5. Done! (as in now you can start coding the actual logic!)