Function to Oops

download Function to Oops

of 3

Transcript of Function to Oops

  • 7/27/2019 Function to Oops

    1/3

    PRINT FROM SAP HELP PORTAL

    Document:From Function Groups to Objects

    URL:http://help.sap.com/saphelp_nw73ehp1/helpdata/en/c3/225b5954f411d194a60000e8353423/content.htm

    Date created:August 07, 2013

    2013 SAP AG or an SAP affiliate company. All rights reserved. No part of this publication may be reproduced or transmitted in any form or for any purpose without the expresspermission of SAP AG. The information contained herein may be changed without prior notice. Some software products marketed by SAP AG and i ts distributors contain proprietary

    software components of other software vendors. National product specifications m ay vary. These materials are provided by SAP AG and its affiliated companies ("SAP Group") for

    informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors or omissions with respect to the materials. The only

    warranties for SAP Group products and services are those that are set forth in the express warranty statements accompanying such products and services, if any. Nothing herein

    should be construed as constituting an additional warranty. SAP and other SAP products and services mentioned herein as well as thei r respective logos are trademarks or

    registered trademarks of SAP AG in G ermany and other countries. Please see www.sap.com/corporate-en/legal/copyright/index.epx#trademark for additional trademark information

    and notices.

    Note

    This PDF document contains the selected topic and its subtopics (max. 150) in the selected structure.Subtopics from other structures are not included.

    PUBLIC 2013 SAP AG or an SAP affiliate company. All rights reserved.

    Page 1 of 3

    http://help.sap.com/saphelp_nw73ehp1/helpdata/en/c3/225b5954f411d194a60000e8353423/content.htmhttp://help.sap.com/
  • 7/27/2019 Function to Oops

    2/3

    From Function Groups to ObjectsAt the center of any object-oriented model are objects, which contain attributes (data) and methods (functions). Objects s hould enable programmers to map a real

    problem and its proposed software solution on a one-to-one basis. Typical objects in a business environment are, for example, Customer, Order, or Invoice.

    From Release 3 .1 onwards, the Business Object Repository (BOR) of SAP Web AS ABAP has contained examples of such objects. The object model of ABAP

    Objects, the object-oriented extension of ABAP, is compatible with the object model of the BOR.

    Before R/3 Release 4.0, the nearest equivalent of objects in ABAP were function modules and function groups. Suppose we have a function group for processing

    orders. The attributes of an order correspond to the global data of the function group, while the individual function modules represent actions that manipulate that

    data (methods). This means that the actual order data is encapsulated in the function group, and is never directly addressed, but instead only through the function

    modules. In this way, the function modules can ensure that the data is consistent.

    When you run an ABAP program, the system starts a new internal session. The internal session has a memory area that contains the ABAP program and its

    associated data. When you call a function module, an instance of its function group plus its data, is loaded into the memory area of the internal session. An ABAP

    program can load several instances by calling function modules from different function groups.

    The instance of a function group in the memory area of the internal session almost represents an object in the sense of object orientation. See also the definition in

    the section What is O bject O rientation?. When you call a function module, the calling program uses the instance of a function group, based on its description in

    the Function Builder. The program cannot access the data in the function group directly, but only through the function module. The function modules and their

    parameters are the interface between the function group and the user.

    The main difference between real object orientation and function groups is that although a program can work with the instances ofseveral function groups at the

    same time, it cannot work with several instances of a single function group. Suppose a program wanted to use several independent counters, or process several

    orders at the same time. In this case, you would have to adapt the function group to include instance administration, for example, by using numbers to differentiate

    between the instances.

    In practice, this is very awkward. Consequently, the data is usually stored in the calling program, and the function modules are called to work with it (structured

    programming). One problem is, for example, that all users of the function module must use the same data structures as the function group itself. Changing theinternal data structure of a function group affects many users, and it is often difficult to predict the implications. The only way to avoid this is to rely heavily on

    interfaces and a technique that guarantees that the internal structures of instances will remain hidden, allowing you to change them later without causing any

    problems.

    This requirement is met by object orientation. ABAP Objects allows you to define data and functions in classes instead of function groups. Using classes, an

    ABAP program can work with any number of instances (objects) based on the same template.

    Instead of loading a single instance of a function group into memory implicitly when a function module is called, the ABAP program can now generate the

    instances of classes explicitly using the new ABAP statement CREATE OBJECT. The individual instances represent unique objects. You address these using

    object references. The object references allow the ABAP program to access the interfaces of the instaces.

    The following sections contain more information about classes, objects, interfaces, and object references in ABAP Objects.

    PUBLIC 2013 SAP AG or an SAP affiliate company. All rights reserved.

    Page 2 of 3

    http://help.sap.com/saphelp_nw73ehp1/helpdata/en/c3/225b5654f411d194a60000e8353423/content.htm
  • 7/27/2019 Function to Oops

    3/3

    Instances of Function Groups: ExampleThe following example shows the object-oriented aspect of function groups in the simple case of a counter.

    Suppose we have a function group called counter:

    FUNCTION-POOL counter.

    DATA count TYPE i.

    FUNCTION set_counter.

    * Local Interface IMPORTING VALUE(SET_VALUE)

    count = set_value.

    ENDFUNCTION.

    FUNCTION increment_counter.

    ADD 1 TO count.

    ENDFUNCTION.

    FUNCTION get_counter.

    * Local Interface: EXPORTING VALUE(GET_VALUE)

    get_value = count.

    ENDFUNCTION.

    The function group counter is used as a counter. It has a global integer field count, and three function modules, set_counter, increment_counter, and

    get_counter, that work with the field. Two of the function modules have input and output parameters. These form the data interface of the function group. From

    on object-oriented point of view, a function gorup has exclusively private attributes and public methods.

    Any ABAP program can then work with this function group. For example:

    REPORT demo_function_group_counter.

    DATA number TYPE i VALUE 5.

    CALL FUNCTION 'SET_COUNTER'

    EXPORTING

    set_value = number.

    DO 3 TIMES.

    CALL FUNCTION 'INCREMENT_COUNTER'.

    ENDDO.

    CALL FUNCTION 'GET_COUNTER'

    IMPORTING

    get_value = number.

    WRITE number .

    After this section of the program has been processed, the program variable numberwill have the value 8.

    The program itself cannot access the count field in the function group. Operations on this field are fully encapsulated in the function module. The program can

    only communicate with the function group by calling its function modules.

    PUBLIC 2013 SAP AG or an SAP affiliate company. All rights reserved.

    Page 3 of 3