Skip to Content
Technical Articles

Santa Claus and the christmas elves

Dear community, Santa Claus has a lot to do. In his christmas workshop, he has to check the letters and emails from his millions followers and biggest fans: children. As he wants to fulfill every whish to every child, he has great little helpers: the christmas elves.

The good thing is: He just has to think of a name of an elf and if it’s not there, it appears magically at the same moment to start the work Santa Claus will give the great little helper.

So how does this look in ABAP?

*&---------------------------------------------------------------------*
*& Report ZSANTA_CLAUS_AND_ELVES
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zsanta_claus_and_elves.

CLASS lcl_elf DEFINITION FINAL CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS get_instance
      IMPORTING
        iv_name          TYPE string
      RETURNING
        VALUE(ro_result) TYPE REF TO lcl_elf.

    CLASS-METHODS count_elves
      RETURNING VALUE(rv_result) TYPE i.

    METHODS set_work_to_do
      IMPORTING
        iv_work_to_do TYPE string.

    METHODS constructor.

    METHODS get_work_to_do
      RETURNING
        VALUE(rv_result) TYPE string.

  PROTECTED SECTION.

  PRIVATE SECTION.
    TYPES: BEGIN OF ty_elves_administration_entry,
             name TYPE string,
             elf TYPE REF TO lcl_elf,
           END OF ty_elves_administration_entry.

    TYPES ty_elves_administration_table TYPE TABLE OF ty_elves_administration_entry WITH KEY name.

    CLASS-DATA mt_elves_administration TYPE ty_elves_administration_table.
    DATA mv_work_to_do TYPE string.
ENDCLASS.

CLASS lcl_elf IMPLEMENTATION.
  METHOD constructor.
   mv_work_to_do = 'nothing'.
  ENDMETHOD.

  METHOD get_instance.
    FIELD-SYMBOLS <elves_administration_entry> TYPE ty_elves_administration_entry.

    ASSIGN mt_elves_administration[ name = iv_name ] TO <elves_administration_entry>.
    IF sy-subrc <> 0.
      DATA(lo_elve) = NEW lcl_elf( ).
      DATA(ls_elves_administration_entry) = VALUE ty_elves_administration_entry( name = iv_name elf = lo_elve ).
      INSERT ls_elves_administration_entry INTO TABLE mt_elves_administration ASSIGNING <elves_administration_entry>.
    ENDIF.

    ro_result = <elves_administration_entry>-elf.
  ENDMETHOD.

  METHOD set_work_to_do.
    mv_work_to_do = iv_work_to_do.
  ENDMETHOD.

  METHOD get_work_to_do.
    rv_result = me->mv_work_to_do.
  ENDMETHOD.

  METHOD count_elves.
    rv_result = lines( mt_elves_administration ).
  ENDMETHOD.
ENDCLASS.

CLASS lcl_santa_claus DEFINITION FINAL CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS get_instance
      RETURNING
        VALUE(ro_result) TYPE REF TO lcl_santa_claus.

    METHODS send_elf_to_work
      IMPORTING
        iv_name TYPE string
        iv_work TYPE string.

    METHODS check_what_is_elf_doing
      IMPORTING
        iv_name          TYPE string
      RETURNING
        VALUE(rv_result) TYPE string.

    METHODS count_elves_under_control
      RETURNING VALUE(rv_result) TYPE i.

  PROTECTED SECTION.

  PRIVATE SECTION.
    CLASS-DATA mo_instance TYPE REF TO lcl_santa_claus.
ENDCLASS.

CLASS lcl_santa_claus IMPLEMENTATION.
  METHOD get_instance.
    IF mo_instance IS NOT BOUND.
      mo_instance = NEW lcl_santa_claus( ).
    ENDIF.

    ro_result = mo_instance.
  ENDMETHOD.

  METHOD send_elf_to_work.
    IF iv_name IS INITIAL.
      RETURN.
    ENDIF.

    DATA(lo_elve) = lcl_elf=>get_instance( iv_name ).
    lo_elve->set_work_to_do( iv_work ).
  ENDMETHOD.

  METHOD check_what_is_elf_doing.
    DATA(lo_elve) = lcl_elf=>get_instance( iv_name ).
    rv_result = lo_elve->get_work_to_do( ).
  ENDMETHOD.

  METHOD count_elves_under_control.
    rv_result = lcl_elf=>count_elves( ).
  ENDMETHOD.
ENDCLASS.


START-OF-SELECTION.
  DATA(lo_santa_claus) = lcl_santa_claus=>get_instance(  ).

  lo_santa_claus->send_elf_to_work( iv_name = 'Herbert' iv_work = 'building toys' ).
  WRITE: / 'Elf Herbert is doing: ', lo_santa_claus->check_what_is_elf_doing( 'Herbert' ).
  WRITE: / 'Elves to take care of: ', lo_santa_claus->count_elves_under_control( ) LEFT-JUSTIFIED.

  lo_santa_claus->send_elf_to_work( iv_name = 'Herbert' iv_work = 'loading christmas sleigh' ).
  WRITE: / 'Elf Herbert is now doing: ', lo_santa_claus->check_what_is_elf_doing( 'Herbert' ).

  WRITE: / 'Elf Paula is doing: ', lo_santa_claus->check_what_is_elf_doing( 'Paula' ).
  WRITE: / 'Elves to take care of: ', lo_santa_claus->count_elves_under_control( ) LEFT-JUSTIFIED.

As happened in the last years, I try to entertain you with a little christmas surprise. This time, it’s about Singleton und Multiton in connection with christmas. I’ve read for the first time about Multiton by Kerem Koseoglu in this blog. Many thanks to him.

Please check my example if the design patterns are applied correctly. To be honest, it was made in a little break in between. I had the idea and didn’t want to postpone the implementation. As a thought: To implement the example with global classes might be much clearer and nicer.

Ok, that’s all for now. I wish everyone who celebrates Christmas a Merry Christmas – I know this time it’s certainly different, but we’re making the most of it! 🙂

In any case, I wish you all that your families, your friends and colleagues stay healthy!

 

Best regards

Michael

 

P.S.: Check our new “Virtual Wishing Well for Blogging“.

P.S.S.: Not tired of reading? Check the Clean ABAP book.

7 Comments
You must be Logged on to comment or reply to a post.
  • Merry Christmas to you!   And to those who celebrate.  This is a fun program for this time of year.

    I'm looking forward to more time with my family.

    And I wish all and their families to stay healthy.  A couple or more vaccines are supposed to be out yet this year.  Yes, not for everyone (not for me or my family)  But that means we are on our way to a better world.

    It is time to remain hopeful for 2021.  And celebrate every day!

    • Vaccines are one of the blessings of the age of reason.  Sad, that you and your family are not physically able to use them.  Good luck, good health, good new year.

      • We’ll eventually get to use them.   They will go to those that need them the most first.   First our medical staff.   I’m so glad about that.   I know so many of them that have worked way too hard.   Next our nursing homes.   Then….  Well our government doesn’t have that worked out yet.   I just figure we are not going to get them this year.  Probably at some point next year.   But such a wonderful thing – just to know that they are out there.

        Stay healthy!

  • Lovely. But I would have done it like this as more in accordance, in my slightly subjective opinion, with clean code principles. (Only slightly different).

    REPORT zsanta_claus_and_elves.
    
    CLASS lcl_elf DEFINITION FINAL CREATE PRIVATE.
      PUBLIC SECTION.
        CLASS-METHODS get_instance
          IMPORTING
            i_name          TYPE string
          RETURNING
            VALUE(r_result) TYPE REF TO lcl_elf.
        CLASS-METHODS count_elves
          RETURNING VALUE(r_result) TYPE i.
        METHODS set_work_to_do
          IMPORTING
            i_work_to_do TYPE string.
        METHODS constructor.
        METHODS get_work_to_do
          RETURNING
            VALUE(r_result) TYPE string.
    
      PROTECTED SECTION.
    
      PRIVATE SECTION.
        TYPES: BEGIN OF ty_elves_administration_entry,
                 name TYPE string,
                 elf TYPE REF TO lcl_elf,
               END OF ty_elves_administration_entry.
        TYPES ty_elves_administration_table TYPE TABLE OF ty_elves_administration_entry WITH KEY name.
        CLASS-DATA elves_administration TYPE ty_elves_administration_table.
        DATA work_to_do TYPE string.
    ENDCLASS.
    
    CLASS lcl_elf IMPLEMENTATION.
      METHOD constructor.
        work_to_do = 'nothing'.
      ENDMETHOD.
    
      METHOD get_instance.
        ASSIGN elves_administration[ name = i_name ] TO FIELD-SYMBOL(<elves_administration_entry>).
        IF sy-subrc <> 0.
          DATA(elf) = NEW lcl_elf( ).
          INSERT VALUE ty_elves_administration_entry( name = i_name elf = elf )
    	      INTO TABLE elves_administration 
                  ASSIGNING <elves_administration_entry>.
        ENDIF.
        r_result = <elves_administration_entry>-elf.
      ENDMETHOD.
    
      METHOD set_work_to_do.
        work_to_do = i_work_to_do.
      ENDMETHOD.
    
      METHOD get_work_to_do.
        r_result = work_to_do.
      ENDMETHOD.
    
      METHOD count_elves.
        r_result = lines( elves_administration ).
      ENDMETHOD.
    ENDCLASS.
    
    CLASS lcl_santa_claus DEFINITION FINAL CREATE PRIVATE.
      PUBLIC SECTION.
        CLASS-METHODS get_instance
          RETURNING
            VALUE(r_result) TYPE REF TO lcl_santa_claus.
        METHODS send_elf_to_work
          IMPORTING
            i_name TYPE string
            i_work TYPE string.
        METHODS check_what_is_elf_doing
          IMPORTING
            i_name          TYPE string
          RETURNING
            VALUE(r_result) TYPE string.
        METHODS count_elves_under_control
          RETURNING VALUE(r_result) TYPE i.
    
      PROTECTED SECTION.
    
      PRIVATE SECTION.
        CLASS-DATA instance TYPE REF TO lcl_santa_claus.
    ENDCLASS.
    
    CLASS lcl_santa_claus IMPLEMENTATION.
      METHOD get_instance.
        IF instance IS NOT BOUND.
          instance = NEW lcl_santa_claus( ).
        ENDIF.
        r_result = instance.
      ENDMETHOD.
    
      METHOD send_elf_to_work.
        IF i_name IS INITIAL.
          RETURN.
        ENDIF.
        DATA(elf) = lcl_elf=>get_instance( i_name ).
        elf->set_work_to_do( i_work ).
      ENDMETHOD.
    
      METHOD check_what_is_elf_doing.
        DATA(elf) = lcl_elf=>get_instance( i_name ).
        r_result = elf->get_work_to_do( ).
      ENDMETHOD.
    
      METHOD count_elves_under_control.
        r_result = lcl_elf=>count_elves( ).
      ENDMETHOD.
    ENDCLASS.

    And I use CL_DEMO_OUTPUT instead of WRITE.