Quantcast
Channel: Jive Syndication Feed
Viewing all articles
Browse latest Browse all 11

OO ABAP and Design Patterns: itab lookup as Decorator pattern

$
0
0
Introduction

There were many discussions on design patterns in OO ABAP and wether they're worth the learning effort of or not. I believe yes,
but I won't discuss it here.
I'll rather give a live example and try to describe here how I have implemented the Decorator pattern in OO ABAP.
I'll handle here a simple action of providing lookup data for a structure or an
internal table in an unusual, reusable and chainable, way.

This weblog contains the following subjects:


    • The problem: internal table witout lookup data

    • Usual solution: tons of SELECTs and READs

    • Is this good?

    • The idea: reusable components

    • The Decorator pattern

    • Implementation - how it works?

    • Concrete components

    • Conclusion


The problem: internal table without lookup data


Let's say that you have an internal table of plants, like this:


 

...and that you filled it's rows with only WERKS (plant code) fields:







t_itab

      <font face="Tahoma" style="font-size: 8pt">WERKS</font>
      <font face="Tahoma" style="font-size: 8pt">PLANT_NAME</font>


















1030

 

1130

 

1330

 

2030

 



Now you need plant names to show them somewhere, for example, on a report.
You need:







t_itab

      <font face="Tahoma" style="font-size: 8pt">WERKS</font>
      <font face="Tahoma" style="font-size: 8pt">PLANT_NAME</font>First you need variable declarations. Although any component can be final as long as it's the last in a chain, I created here a dummy final to show flexibility. +Dummy final+ is generic and doesn't do anything but abstract all the others at the moment of usage.



 



Instantiation

You need, ofcourse, to create the objects. Let's assume the most complicated case: you have yourself an internal table
which needs all the lookup functionalities that you've created. You'll
instantiate a chain of decorators as follows:


 



Usage:

Here you're fetching data and filling your itab's lookup fields:

 


I won't explain the attributes here, since they're all initiated by the CONSTRUCTOR and I'll explain them as it's parameters.I'll only say that this NEXT_DECORATOR has a confusing name - it would be PREVIOUS_DECORATOR if I didn't experience semantic confusion at that time. First I believed it was next, and later realized that the name previousbetter matches the instantiation sequence. I corrected the public interface, but forgot the attribute. And it's hard to correct it now because I had to leave the company, I'm not on the original system and modification assistant harrasses me. Sorry, I hope you'll still be able to catch it.


The methods: they almost do nothing except initiate attributes and call
corresponding methods of the previous decorator in chain. That's what the Decorator
pattern is about in the first place.


!https://weblogs.sdn.sap.com/weblogs/images/28520/AbsDecMethods.gif|height=218|width=290|src=https://weblogs.sdn.sap.com/weblogs/images/28520/AbsDecMethods.gif|border=0!



    1. GET_FIELDS has only one changing parameter:


    Name

    Type

    Description







    CH_STRUC

    ANY

    itab line with fields, say
    LIFNR, VENDOR_NAME etc. This method returns them filled




      1. CONSTRUCTOR has only importing parameters:



      Name

      Type

      DescriptionMany thanks to Thomas Jung whose hints helped me to make this weblog look better than plain text.

      Viewing all articles
      Browse latest Browse all 11

      Trending Articles



      <script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>