Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *&---------------------------------------------------------------------*
- *& Report ZZ_DECORATOR_EXAMPLE
- *&---------------------------------------------------------------------*
- *& Example of the decorator pattern
- *& T100 select, decorated by a cache
- *& Idea from
- *& http://scn.sap.com/community/abap/blog/2014/04/04/caching-with-decorator-pattern#comment-483323
- *&
- *& Too much to write - unless there is an assistant to generate most of its code,
- *& this will never become popular in the ABAP community.
- *&---------------------------------------------------------------------*
- report zz_decorator_example.
- * An exception for "T100 not found"
- class lcx_t100_not_found definition inheriting from cx_static_check.
- public section.
- data key type scx_t100key.
- methods constructor importing key type scx_t100key.
- endclass.
- * Select a line from table T100 (by default, in sy-langu)
- interface lif_t100_selector.
- methods:
- select
- importing is_key type scx_t100key
- returning value(es_t100) type t100
- raising lcx_t100_not_found.
- endinterface.
- class lcl_t100_select_decorator definition abstract.
- public section.
- methods constructor importing io_decorated_selector type ref to lif_t100_selector.
- interfaces lif_t100_selector.
- private section.
- data: go_decorated_selector type ref to lif_t100_selector.
- endclass.
- * Decorated objects of the "plain" class contain additions
- class lcl_t100_select_decorator implementation.
- method constructor.
- go_decorated_selector = io_decorated_selector.
- endmethod.
- method lif_t100_selector~select.
- es_t100 = go_decorated_selector->select( is_key ).
- endmethod.
- endclass.
- * "Plain" T100 selection class
- * Implementation detail: Keep the language as parameter
- class lcl_t100_selector definition.
- public section.
- methods constructor importing iv_langu type langu default sy-langu.
- interfaces lif_t100_selector.
- private section.
- data gv_langu type langu.
- endclass.
- * Decoration example: Cache
- class lcl_t100_cached definition inheriting from lcl_t100_select_decorator.
- public section.
- methods lif_t100_selector~select redefinition.
- private section.
- types:
- t100_tab type sorted table of t100
- with unique key arbgb msgnr.
- data: gt_t100 type t100_tab.
- endclass.
- class lcl_t100_cached implementation.
- method lif_t100_selector~select.
- read table gt_t100 into es_t100
- from value #( arbgb = is_key-msgid msgnr = is_key-msgno ).
- if sy-subrc ne 0.
- es_t100 = super->lif_t100_selector~select( is_key ).
- insert es_t100 into table gt_t100.
- endif.
- endmethod.
- endclass.
- class lcl_t100_selector implementation.
- method constructor.
- gv_langu = iv_langu.
- endmethod.
- method lif_t100_selector~select.
- select single * from t100
- into es_t100
- where arbgb = is_key-msgid
- and msgnr = is_key-msgno
- and sprsl = gv_langu.
- if sy-subrc ne 0.
- raise exception type lcx_t100_not_found
- exporting
- key = is_key.
- endif.
- endmethod.
- endclass.
- class lcx_t100_not_found implementation.
- method constructor.
- super->constructor( ).
- me->key = key.
- endmethod.
- endclass.
- start-of-selection.
- perform start.
- *
- form start.
- data(lo_t100) = cast lif_t100_selector( new lcl_t100_cached( new lcl_t100_selector( ) ) ).
- data(ls_key) = value scx_t100key( msgid = '00' msgno = '172' ).
- cl_demo_input=>add_field( exporting text = 'MSGID' changing field = ls_key-msgid ).
- cl_demo_input=>add_field( exporting text = 'MSGNO' changing field = ls_key-msgno ).
- cl_demo_input=>request( ).
- try.
- data(ls_t100) = lo_t100->select( ls_key ).
- cl_demo_output=>write_data( ls_t100 ).
- catch lcx_t100_not_found.
- cl_demo_output=>write( 'Kein T100-Satz zum Schlüssel gefunden' ).
- endtry.
- cl_demo_output=>display( ).
- endform.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement