Advertisement
rplantiko

Decorate a selection with a cache

Apr 19th, 2014
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ABAP 3.90 KB | None | 0 0
  1. *&---------------------------------------------------------------------*
  2. *& Report  ZZ_DECORATOR_EXAMPLE
  3. *&---------------------------------------------------------------------*
  4. *& Example of the decorator pattern
  5. *& T100 select, decorated by a cache
  6. *& Idea from
  7. *& http://scn.sap.com/community/abap/blog/2014/04/04/caching-with-decorator-pattern#comment-483323
  8. *&
  9. *& Too much to write - unless there is an assistant to generate most of its code,
  10. *& this will never become popular in the ABAP community.
  11. *&---------------------------------------------------------------------*
  12.  
  13. report zz_decorator_example.
  14.  
  15. * An exception for "T100 not found"
  16. class lcx_t100_not_found definition inheriting from cx_static_check.
  17.   public section.
  18.     data key type scx_t100key.
  19.     methods constructor importing key type scx_t100key.
  20. endclass.
  21.  
  22. * Select a line from table T100 (by default, in sy-langu)
  23. interface lif_t100_selector.
  24.   methods:
  25.     select
  26.       importing is_key type scx_t100key
  27.       returning value(es_t100) type t100
  28.       raising lcx_t100_not_found.
  29. endinterface.
  30.  
  31. class lcl_t100_select_decorator definition abstract.
  32.   public section.
  33.     methods constructor importing io_decorated_selector type ref to lif_t100_selector.
  34.     interfaces lif_t100_selector.
  35.   private section.
  36.     data: go_decorated_selector type ref to lif_t100_selector.
  37. endclass.
  38.  
  39. * Decorated objects of the "plain" class contain additions
  40. class lcl_t100_select_decorator implementation.
  41.   method constructor.
  42.     go_decorated_selector = io_decorated_selector.
  43.   endmethod.
  44.   method lif_t100_selector~select.
  45.     es_t100 = go_decorated_selector->select( is_key ).
  46.   endmethod.
  47. endclass.
  48.  
  49. * "Plain" T100 selection class
  50. * Implementation detail: Keep the language as parameter
  51. class lcl_t100_selector definition.
  52.   public section.
  53.     methods constructor importing iv_langu type langu default sy-langu.
  54.     interfaces lif_t100_selector.
  55.   private section.
  56.     data gv_langu type langu.
  57. endclass.
  58.  
  59. * Decoration example: Cache
  60. class lcl_t100_cached definition inheriting from lcl_t100_select_decorator.
  61.   public section.
  62.     methods lif_t100_selector~select redefinition.
  63.   private section.
  64.     types:
  65.       t100_tab type sorted table of t100
  66.                with unique key arbgb msgnr.
  67.     data: gt_t100 type t100_tab.
  68. endclass.
  69.  
  70. class lcl_t100_cached implementation.
  71.   method lif_t100_selector~select.
  72.     read table gt_t100 into es_t100
  73.          from value #( arbgb = is_key-msgid msgnr = is_key-msgno ).
  74.     if sy-subrc ne 0.
  75.       es_t100 = super->lif_t100_selector~select( is_key ).
  76.       insert es_t100 into table gt_t100.
  77.     endif.
  78.   endmethod.
  79. endclass.
  80.  
  81. class lcl_t100_selector implementation.
  82.   method constructor.
  83.     gv_langu = iv_langu.
  84.   endmethod.
  85.   method lif_t100_selector~select.
  86.     select single * from t100
  87.       into es_t100
  88.       where arbgb = is_key-msgid
  89.         and msgnr = is_key-msgno
  90.         and sprsl = gv_langu.
  91.     if sy-subrc ne 0.
  92.       raise exception type lcx_t100_not_found
  93.         exporting
  94.           key = is_key.
  95.     endif.
  96.   endmethod.
  97. endclass.
  98.  
  99. class lcx_t100_not_found implementation.
  100.   method constructor.
  101.     super->constructor( ).
  102.     me->key = key.
  103.   endmethod.
  104. endclass.
  105.  
  106.  
  107.  
  108. start-of-selection.
  109.   perform start.
  110.  
  111. *
  112. form start.
  113.  
  114.  
  115.   data(lo_t100) = cast lif_t100_selector( new lcl_t100_cached( new lcl_t100_selector( ) ) ).
  116.  
  117.   data(ls_key)  = value scx_t100key( msgid = '00' msgno = '172' ).
  118.   cl_demo_input=>add_field( exporting text = 'MSGID' changing field = ls_key-msgid ).
  119.   cl_demo_input=>add_field( exporting text = 'MSGNO' changing field = ls_key-msgno ).
  120.   cl_demo_input=>request( ).
  121.  
  122.   try.
  123.       data(ls_t100) = lo_t100->select( ls_key ).
  124.       cl_demo_output=>write_data( ls_t100 ).
  125.     catch lcx_t100_not_found.
  126.       cl_demo_output=>write( 'Kein T100-Satz zum Schlüssel gefunden' ).
  127.   endtry.
  128.   cl_demo_output=>display( ).
  129.  
  130. endform.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement