14.01.2020 Views

ABAP_to_the_Future

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Creating and Using Shared Memory Objects 14.2

METHOD get_monsters.

* Local Variables

DATA: ls_monsters LIKE LINE OF mt_monsters.

IF id_monster IS SUPPLIED.

READ TABLE mt_monsters INTO ls_monsters WITH TABLE KEY

monster_number = id_monster.

INSERT ls_monsters INTO TABLE rt_monsters.

ELSE.

rt_monsters[] = mt_monsters[].

ENDIF.

ENDMETHOD.

Listing 14.3 Root Class Monster Retrieval Method

The code in Listing 14.3, in which you fi ll up a table with the required monster

details, is a very simplistic example, but you could opt for something more complicated

with no effort at all. You coul d have lots of different retrieval methods;

for example, there could be one that returned all the monsters of a certain color.

In fact, because this is all standard ABAP, it’s not difficult to imagine a situation in

which the root class gets information fr om a dozen database tables and three

external systems during its LOAD method and in the retrieval method combines all

that assorted data into one return structure based on the most complicated business

logic ever invented.

For example, often you end up with an internal table in your program that comprises

the contents of one database tabl e plus some text descriptions of sales

offices (from customizing table TVKBT) and the like, maybe some address details

from ADRC, and some calculated fields. Perhaps also one of the fields in the internal

table contains a reference to an object. Normally, you do a database read on a

single table, and then loop through the resulting internal table, filling in the missing

bits. That might be quite a lot of work, and in some circumstances it may

make sense to simply go through this process once and store the result in shared

memory.

Remember, though, that retrieving, manipu lating, and sending back data is all

that the root class is supposed to do. Th is is the single responsibility principle.

There is nothing in the code that is do ne differently because an instance of the

class is going to live in shared memory. If you had an existing class that just dealt

639

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!