Why use ABAP Objects Horst Keller, Stefan Bresch ABAP Language, SAP AG
Learning Objectives
Explain the benefits of object oriented programming in general
Explain the benefits of ABAP Objects compared to classical ABAP
Understand why Unicode enabled ABAP Objects is the best language for business applications up to now
SAP AG2003, TechED_Basel / ABAP256, ABAP256, Horst Horst Keller, Stefan Bresch Bresch / 2
Agenda Background Principal Features of ABAP Objects
Encapsulation
Instantiation
Reuse via Inheritance
Interfacing
Events
Benefits of ABAP Objects
Simplicity
Explicitness
Maintainability
Purified ABAP
Future Orientation
Conclusion
ABAP256, Horst Horst Keller, Stefan Bresch Bresch / 3 SAP AG2003, TechED_Basel / ABAP256,
Next Topic Background Principal Features of ABAP Objects
Encapsulation
Instantiation
Reuse via Inheritance
Interfacing
Events
Benefits of ABAP Objects
Simplicity
Explicitness
Maintainability
Purified ABAP
Future Orientation
Conclusion
SAP AG2003, TechED_Basel / ABAP256, ABAP256, Horst Horst Keller, Stefan Bresch Bresch / 4
Agenda Background Principal Features of ABAP Objects
Encapsulation
Instantiation
Reuse via Inheritance
Interfacing
Events
Benefits of ABAP Objects
Simplicity
Explicitness
Maintainability
Purified ABAP
Future Orientation
Conclusion
ABAP256, Horst Horst Keller, Stefan Bresch Bresch / 3 SAP AG2003, TechED_Basel / ABAP256,
Next Topic Background Principal Features of ABAP Objects
Encapsulation
Instantiation
Reuse via Inheritance
Interfacing
Events
Benefits of ABAP Objects
Simplicity
Explicitness
Maintainability
Purified ABAP
Future Orientation
Conclusion
SAP AG2003, TechED_Basel / ABAP256, ABAP256, Horst Horst Keller, Stefan Bresch Bresch / 4
Backgr Backgroun ound d – History History
ABAP Objects was introduced with SAP Basis, Basis, Release 4.5. 4.5.
Classes
Interfaces
Events
ABAP Objects was completed with SAP Basis Release 4.6. 4.6.
Inheritance
Compound interfaces
Dynamic Invoke
Some enhancements were added with SAP Web Application Server , Releases 6.10, 6.20, ...
Friends
Object Services
Shared Objects
Preceding Tec TechEd hEd Work Worksho shops ps (available from
[email protected] [email protected]))
ABAP256, Horst Horst Keller, Stefan Bresch Bresch / 5 SAP AG2003, TechED_Basel / ABAP256,
Background Background – ABAP and and ABAP Objects Objects at the WAS WAS
User Interface (SAP GUI, Web, ...)
ABAP Web Application Server
... SELECT * FROM ... ...
ABAP Objects Persistent Data (Database, Files, ...) SAP AG2003, TechED_Basel / ABAP256, ABAP256, Horst Horst Keller, Stefan Bresch Bresch / 6
Background Background – Object Orientation Orientation in ABAP ABAP
Pure Object Oriented ABAP World ?
ABAP
Non-ABAP
Technically possible! ABAP256, Horst Horst Keller, Stefan Bresch Bresch / 7 SAP AG2003, TechED_Basel / ABAP256,
Background Background – Object Orientation Orientation in ABAP ABAP
Objects with Application Logic
ABAP
SAP AG2003, TechED_Basel / ABAP256, ABAP256, Horst Horst Keller, Stefan Bresch Bresch / 8
Background – Object Orientation in ABAP
Persistence and Transaction Services provided by ABAP Object Services, as of Release 6.10 SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 9
Background – Object Orientation in ABAP
GUI Control Framework, Office Integration, Business Server Pages, etc.
ABAP
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 10
Background – Object Orientation in ABAP
For example JavaScript (as of Release 6.10)
ABAP
Non-ABAP
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 11
Background – Object Orientation in ABAP
Application Coding is here!
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 12
System Classes from Libraries
Background – Object Orientation in ABAP
Inside Objects?
METHOD... DATA ... TYPE ... ... LOOP AT itab REFERENCE INTO ... ... ENDMETHOD.
Purified ABAP
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 13
Background – Object Orientation in ABAP
Reality ...
CALL SCREEN ... CALL FUNCTION DESTINATION ... SELECT ...
ABAP
Non-ABAP
ABAP + ABAP Objects SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 14
Next Topic Background Principal Features of ABAP Objects
Encapsulation
Instantiation
Reuse via Inheritance
Interfacing
Events
Benefits of ABAP Objects
Simplicity
Explicitness
Maintainability
Purified ABAP
Future Orientation
Conclusion
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 15
Encapsulation – ABAP/4 (1) Encapsulation of global data
Event Driven Modularization Processes
* Global Declarations... DATA ...
No local data PROCESS ON ... MODULE ... ...
MODULE ... ... ENDMODULE.
START-OF-SELECTION ...
No local data
Runtime Environment
Dynpro
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 16
ABAP Program
Encapsulation – ABAP/4 (2)
Procedural Programming Model * Global Declarations ... DATA ... MODULE ... ... CALL FUNCTION ... PERFORM ... ... ENDMODULE.
FORM ... DATA ... ... ENDFORM.
Local data
ABAP Program
Encapsulation can be circumvented!
* Global Declarations ... DATA ... FUNCTION ... DATA ... ... PERFORM ... ... ENDFUNCTION. FORM ... DATA ... ... SELECT * FROM ... ... ENDFORM.
Encapsulation of global data Local data
Local data
Function Group
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 17
Encapsulation – Example ABAP/4 (1)
FUNCTION-POOL account.
Encapsulation of an account
DATA current_amount TYPE accounts-amount.
FUNCTION deposit. current_amount = current_amount + amount. ENDFUNCTION.
FUNCTION withdraw. IF current_amount > amount. current_amount = current_amount - amount. ELSE. RAISE EXCEPTION TYPE cx_negative_amount. ENDIF. ENDFUNCTION.
Function Group SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 18
Function modules work with one account
No possibility to interact with other accounts
Encapsulation – Example ABAP/4 (2)
FUNCTION-POOL account. DATA account_tab TYPE SORTED TABLE OF accounts WITH UNIQUE KEY id . LOAD-OF-PROGRAM. SELECT * FROM accounts INTO TABLE account_tab.
FUNCTION deposit. DATA account_wa TYPE accounts. READ TABLE account_tab INTO account_wa WITH TABLE KEY id = id . account_wa-amount = account_wa-amount + amount. MODIFY TABLE account_tab FROM account_wa. ENDFUNCTION.
All data in one table
Function modules work on shared data
Function Group SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 19
Encapsulation – Example ABAP/4 (3) FUNCTION withdraw. DATA account_wa TYPE accounts. READ TABLE account_tab INTO account_wa WITH TABLE KEY id = id . IF account_wa-amount > amount. account_wa-amount = account_wa-amount - amount. MODIFY TABLE account_tab FROM account_wa. ELSE. RAISE EXCEPTION TYPE cx_negative_amount. ENDIF. ENDFUNCTION. FUNCTION transfer. CALL FUNCTION 'WITHDRAW' EXPORTING id = id_from amount = amount. CALL FUNCTION 'DEPOSIT' EXPORTING id = id_to amount = amount. ENDFUNCTION.
Function Group SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 20
Data identified by input parameters
Encapsulation – ABAP Objects
Classes CLASS ... DEFINITION. PUBLIC SECTION. METHODS ... PRIVATE SECTION. DATA ... Encapsulation ... of Attributes ENDMETHOD. ENDCLASS.
CLASS ... DEFINITION. PUBLIC SECTION. METHODS ... PRIVATE SECTION. DATA ... Encapsulation ... of Attributes ENDMETHOD. ENDCLASS.
CLASS ... IMPLEMENTATION. METHOD... Local data DATA ... ... CALL METHOD ... ... ENDMETHOD. ENDCLASS.
CLASS ... IMPLEMENTATION. METHOD... Local data DATA ... ... SELECT * FROM ... ... ENDMETHOD. ENDCLASS.
ABAP Program
Class Pool
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 21
Encapsulation – Example ABAP Objects (1) A class unites functionality with data
CLASS account DEFINITION. PUBLIC SECTION. METHODS: constructor IMPORTING id TYPE accounts-id, deposit IMPORTING amount TYPE accounts-amount, withdraw IMPORTING amount TYPE accounts-amount RAISING cx_negative_amount, transfer IMPORTING amount TYPE accounts-amount target TYPE REF TO account RAISING cx_negative_amount. PRIVATE SECTION. DATA amount TYPE accounts-amount. Only data ENDCLASS. relevant for one
object is needed
Class Pool SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 22
Encapsulation – Example ABAP Objects (2) CLASS account IMPLEMENTATION. METHOD constructor. SELECT SINGLE amount FROM accounts INTO (amount) WHERE id = id. ENDMETHOD. METHOD deposit. me->amount = me->amount + amount. ENDMETHOD. METHOD withdraw. IF me->amount > amount. me->amount = me->amount - amount. ELSE. RAISE EXCEPTION TYPE cx_negative_amount. ENDIF. ENDMETHOD. METHOD transfer. me->withdraw( amount ). target->deposit( amount ). ENDMETHOD. ENDCLASS.
Class Pool SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 23
Next Topic Background Principal Features of ABAP Objects
Encapsulation
Instantiation
Reuse via Inheritance
Interfacing
Events
Benefits of ABAP Objects
Simplicity
Explicitness
Maintainability
Purified ABAP
Future Orientation
Conclusion
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 24
Each object is initialized individually
An object works on its own data, no business key needed
An object can address other objects
Instantiation – ABAP/4
External Procedure Calls
Instances of programs with global data
... ...
Only one instance of each program
Function group 2
Function group 1
CALL FUNCTION ...
ABAP program
No explicite instantiation, no control of lifetime
Internal session of an ABAP program External session
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 25
Instantiation – Example ABAP/4
DATA: id1(8) TYPE n, id2(8) TYPE n, amnt TYPE p DECIMALS 2, exc_ref TYPE REF TO cx_negative_amount, text TYPE string.
First call instantiates function group
TRY. id1 = ... id2 = ... amnt = ... CALL FUNCTION 'TRANSFER' EXPORTING id_from = id1 id_to = id2 amount = amnt. CATCH cx_negative_amount INTO exc_ref. text = exc_ref->get_text( ). MESSAGE text TYPE 'I'. ENDTRY.
ABAP Program SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 26
Business keys identify data No static type check
Instantiation – ABAP Objects
Creation of Objects
Many instances of one class
Instances of classes with attributes and an identity
Explicit instantiation, control of lifetime
CREATE OBJECT oref TYPE class.
ABAP program
oref2
oref1
Internal session of an ABAP program External session
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 27
Instantiation – Example ABAP Objects
DATA: account1 account2 amnt exc_ref text
TYPE TYPE TYPE TYPE TYPE
REF TO account, REF TO account, p DECIMALS 2, REF TO cx_negative_amount, string.
CREATE OBJECT: account1 EXPORTING id = ..., account2 EXPORTING id = ... TRY. amnt = ... account1->transfer( EXPORTING amount = amnt target = account2 ). CATCH cx_negative_amount INTO exc_ref. text = exc_ref->get_text( ). MESSAGE text TYPE 'I'. ENDTRY.
ABAP Program SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 28
Object reference variables as explicit handles Independent objects based on the same class
Static type check
Natural working with instances
Next Topic Background Principal Features of ABAP Objects
Encapsulation
Instantiation
Reuse via Inheritance
Interfacing
Events
Benefits of ABAP Objects
Simplicity
Explicitness
Maintainability
Purified ABAP
Future Orientation
Conclusion
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 29
Reuse – ABAP/4
Reuse of generalized procedures
No support for specialization
Typical results:
Large function groups
Extensive parameter interfaces
Subdividing tasks via CASE
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 30
Reuse – Example ABAP/4 (1)
FUNCTION withdraw. *"-------------------------------------------------------------*" IMPORTING *" REFERENCE(ID) TYPE ACCOUNTS-ID Steering *" REFERENCE(KIND) TYPE C DEFAULT 'C‚ parameters *" REFERENCE(AMOUNT) TYPE ACCOUNTS-AMOUNT *" RAISING *" CX_NEGATIVE_AMOUNT Additional *" CX_UNKNOWN_ACCOUNT_TYPE *"-------------------------------------------------------------- exception CASE kind. WHEN 'C'. „Checking account PERFORM withdraw_from_checking_account USING id amount. WHEN 'S'. "Savings account PERFORM withdraw_from_saving_account USING id amount. WHEN OTHERS. RAISE EXCEPTION TYPE cx_unknown_account_type. ENDCASE. ENDFUNCTION.
Procedure calls with parameter passing
Function Group SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 31
Reuse – Example ABAP/4 (2) FORM withdraw_from_checking_account USING l_id TYPE accounts-id l_amount TYPE accounts-amount. DATA account_wa TYPE accounts. READ TABLE account_tab INTO account_wa WITH TABLE KEY id = l_id. account_wa-amount = account_wa-amount - l_amount. MODIFY TABLE account_tab FROM account_wa. IF account_wa-amount < 0. ... " handle debit balance ENDIF. ENDFORM. Specialized FORM withdraw_from_savings_account procedures, USING l_id TYPE accounts-id no code l_amount TYPE accounts-amount reuse RAISING cx_negative_amount. DATA account_wa TYPE accounts. READ TABLE account_tab INTO account_wa WITH TABLE KEY id = l_id. IF account_wa-amount > l_amount. account_wa-amount = account_wa-amount - l_amount. MODIFY TABLE account_tab FROM account_wa. ELSE. RAISE EXCEPTION TYPE cx_negative_amount. ENDIF. ENDFORM.
Function Group SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 32
Reuse – ABAP Objects
Reuse of general classes
Specialization via inheritance
Benefits
Small classes
Minimal parameter interfaces
Polymorphism (CASE-less programming)
enhanced Modeling capabilities
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 33
Reuse – Example ABAP Objects (1)
General superclass CLASS account DEFINITION. PUBLIC SECTION. ... PROTECTED SECTION. DATA amount TYPE accounts-amount. ENDCLASS.
CLASS account IMPLEMENTATION. ... METHOD withdraw. me->amount = me->amount - amount. ENDMETHOD. ... ENDCLASS.
Class Pool SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 34
Data sharing with subclasses
General method implementation
Reuse – Example ABAP Objects (2)
Special subclass CLASS checking_account DEFINITION INHERITING FROM account. PUBLIC SECTION. METHODS withdraw REDEFINITION. ENDCLASS.
CLASS checking_account IMPLEMENTATION. METHOD withdraw. super->withdraw( amount ). IF me->amount < 0. ... " handle debit balance ENDIF. ENDMETHOD. ENDCLASS.
Method redefinition
Call of general implementation
Special method implementation
Class Pool SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 35
Reuse – Example ABAP Objects (3)
Special subclass CLASS savings_account DEFINITION INHERITING FROM account. PUBLIC SECTION. METHODS withdraw REDEFINITION. ENDCLASS.
CLASS savings_account IMPLEMENTATION. METHOD withdraw. IF me->amount > amount. super->withdraw( amount ). ELSE. RAISE EXCEPTION TYPE cx_negative_amount. ENDIF. ENDMETHOD. ENDCLASS.
Class Pool SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 36
Method redefinition
Special method implementation Call of general implementation
Reuse – Example ABAP Objects (4)
DATA: account1 account2 amnt exc_ref text
TYPE TYPE TYPE TYPE TYPE
REF TO account, REF TO account, p DECIMALS 2, REF TO cx_negative_amount, string.
CREATE OBJECT: account1 TYPE checking_account EXPORTING id = ..., account2 TYPE savings_account EXPORTING id = ... TRY. amnt = ... account1->transfer( EXPORTING amount = amnt target = account2 ). CATCH cx_negative_amount INTO exc_ref. text = exc_ref->get_text( ). MESSAGE text TYPE 'I'. ENDTRY.
Polymorphism
ABAP Program SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 37
Next Topic Background Principal Features of ABAP Objects
Encapsulation
Instantiation
Reuse via Inheritance
Interfacing
Events
Benefits of ABAP Objects
Simplicity
Explicitness
Maintainability
Purified ABAP
Future Orientation
Conclusion
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 38
Same object reference variables as before General handles for specialized objects
Same usage of objects as before!
Interfaces – ABAP/4
Limited support for interfaces:
Global data as interfaces between programs
Selection screens as interfaces for executable programs
Parameter interfaces for procedures
No standalone interfaces
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 39
Interfaces – ABAP Objects (1)
The interface of a class to the outside is defined by its public section
The interface of a class allows access to data and functionality
The interface of a class is inherited from its superclasses
Interfaces can be defined standalone
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 40
Interfaces – ABAP Objects (2)
Standalone Interfaces INTERFACE if1. METHODS: m1 ... ... ENDINTERFACE.
CLASS cl_.... INTERFACES: if1, if2. ... ENDINTERFACE.
oref1
oref2
INTERFACE if2. METHODS: m2 ... ... ENDINTERFACE.
METHOD if1~ m1. ... METHOD if2~ m2. ...
DATA: oref1 TYPE REF TO if1, oref2 TYPE REF TO if2. SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 41
Interfaces – ABAP Objects (3)
Benefits of standalone interfaces:
Can be defined by client
Independency from implementation
Polymorphism
Separation of independent characteristics
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 42
Interfaces – Example (1)
CLASS account DEFINITION. PUBLIC SECTION. INTERFACES if_serializable_object. ... ENDCLASS.
Class Pool One interface in different classes CLASS customer DEFINITION. PUBLIC SECTION. INTERFACES if_serializable_object. ... ENDCLASS.
Class Pool
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 43
Interfaces – Example (2)
DATA: account TYPE REF TO account, customer TYPE REF TO customer, serializable_objects TYPE TABLE OF REF TO if_serializable_object, serializable_object TYPE REF TO if_serializable_object, xmlstr TYPE string. CREATE OBJECT: account EXPORTING id = ..., customer. APPEND: account TO serializable_objects, customer TO serializable_objects. ... LOOP AT serializable_objects INTO serializable_object. CALL TRANSFORMATION id SOURCE obj = serializable_object RESULT XML xmlstr. ENDLOOP.
Interface reference variables
Instantiation of classes Collection of references to objects of different classes Polymorphism
ABAP Program SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 44
Next Topic Background Principal Features of ABAP Objects
Encapsulation
Instantiation
Reuse via Inheritance
Interfacing
Events
Benefits of ABAP Objects
Simplicity
Explicitness
Maintainability
Purified ABAP
Future Orientation
Conclusion
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 45
Events – ABAP/4
Classical Runtime Events:
PAI, PBO
Selection Screen Events
Reporting Events
List Events
No program defined events
No triggering of events in program
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 46
Events – ABAP Objects (1)
Events are components of classes
METHOD... ... RAISE EVENT evt ... ENDMETHOD.
METHODS handler FOR EVENT evt ... METHOD handler ... ENDMETHOD.
oref2 oref1 METHOD... ... SET HANDLER oref1->handler FOR oref2. ... ENDMETHOD. SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 47
Events – ABAP Objects (2)
Benefits of events in ABAP Objects:
indirect method call
decoupling of caller and handler
two stages of publish and subscribe
explicit exporting parameters
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 48
Events – Example (1)
CLASS checking_account DEFINITION INHERITING FROM account. PUBLIC SECTION. Allows the class to METHODS deposit REDEFINITION. publish an event ... EVENTS consulting_required EXPORTING value(amount) TYPE accounts-amount. PRIVATE SECTION. DATA limit TYPE accounts-amount VALUE '5000.00'. ENDCLASS.
CLASS checking_account IMPLEMENTATION. ... METHOD deposit. super->deposit( amount ). IF me->amount > limit. RAISE EVENT consulting_required EXPORTING amount = me->amount. ENDIF. ENDMETHOD. ENDCLASS.
Announces a state change that requires an action
Class Pool SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 49
Events – Example (2)
CLASS consultant DEFINITION. PUBLIC SECTION. Static subscription, METHODS constructor. ... allows the class to PRIVATE SECTION. handle an event ... METHODS receive_notification FOR EVENT consulting_required OF checking_account. ENDCLASS.
CLASS consultant IMPLEMENTATION. Dynamic METHOD constructor. ... subscription IF ... SET HANDLER me->receive_notification FOR ALL INSTANCES. ENDIF ... ENDMETHOD. ... METHOD receive_notification. " do something Event handling ENDMETHOD. ENDCLASS.
Class Pool SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 50
Next Topic Background Principal Features of ABAP Objects
Encapsulation
Instantiation
Reuse via Inheritance
Interfacing
Events
Benefits of ABAP Objects
Simplicity
Explicitness
Maintainability
Purified ABAP
Future Orientation
Conclusion
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 51
Simplicity – ABAP/4 (1)
ABAP/4 coding might be simple, but ... REPORT simple_report. NODES spfli. GET spfli. WRITE: / spfli-carrid, spfli-connid ...
ABAP Program
... who knows really what’s happening here?
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 52
Simplicity – ABAP/4 (2)
This is what happens after SUBMIT: Program is loaded, start values are set LOAD-OF-PROGRAM
Selection screen values are set INITIALIZATION
Call of selection screen
In parallel, subroutines of a logical database are implicitly called!
AT SELECTION-SCREEN ... START-OF-SELECTION GET spfli END-OF-SELECTION
Still simplified scheme! The exact program flow depends on different settings
Call of list processing for basic list AT LINE-SELECTION, ...
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 53
Simplicity – ABAP Objects
ABAP Objects is simple because:
There are only a few orthogonal basic concepts
Classes contain attributes and methods
Objects are instances of classes
Objects are adressed via references
Accessibility is clearly defined by the object’s interface
Programming with objects is more natural than handling data via procedures
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 54
Next Topic Background Principal Features of ABAP Objects
Encapsulation
Instantiation
Reuse via Inheritance
Interfacing
Events
Benefits of ABAP Objects
Simplicity
Explicitness
Maintainability
Purified ABAP
Future Orientation
Conclusion
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 55
Explicitness
ABAP/4 has many implicit features, e.g.:
Implicit interfaces via global data
Dynpros
Logical Databases
Programs are controlled by runtime environment
Programs are driven from Dynpros
Program execution via SUBMIT
ABAP Objects is explicit
no hidden concepts
no “black magic”
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 56
Explicitness – Example (1)
Classes instead of logical data bases
Simple example for a class that works like a logical database with a single node
CLASS ldb DEFINITION. PUBLIC SECTION. METHODS read_spfli. EVENTS spfli_ready EXPORTING value(values) TYPE spfli. PRIVATE SECTION. DATA spfli_wa TYPE spfli. ENDCLASS. Explicit
Interface
CLASS ldb IMPLEMENTATION. METHOD read_spfli. SELECT * FROM spfli INTO spfli_wa. RAISE EVENT spfli_ready EXPORTING values = spfli_wa. ENDSELECT. ENDMETHOD. ENDCLASS.
Events with parameters
Class Pool SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 57
Explicitness – Example (2)
Class instead of report program Simple example for a class that works like a report
Start via an OO Transaction
CLASS report DEFINITION. PUBLIC SECTION. METHODS start. PRIVATE SECTION. DATA spfli_tab TYPE TABLE OF spfli. METHODS: get_spfli FOR EVENT spfli_ready OF ldb IMPORTING values, display_spfli. ENDCLASS.
ABAP Program
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 58
Explicit event handling
Decoupling of data display from data handling
Explicitness – Example (3) CLASS report IMPLEMENTATION. METHOD start. DATA ldb TYPE REF TO ldb. CREATE OBJECT ldb. SET HANDLER me->get_spfli FOR ldb. ldb->read_spfli( ). display_spfli( ). ENDMETHOD. METHOD get_spfli. APPEND values TO spfli_tab. ENDMETHOD.
Simplicity does not necessarily mean less coding
METHOD display_spfli. DATA alv_list TYPE REF TO cl_gui_alv_grid. CREATE OBJECT alv_list EXPORTING i_parent = cl_gui_container=>screen0. alv_list->set_table_for_first_display( EXPORTING i_structure_name = 'SPFLI‚ CHANGING it_outtab = spfli_tab ). CALL SCREEN 100. ENDMETHOD. ENDCLASS.
ABAP Program SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 59
Next Topic Background Principal Features of ABAP Objects
Encapsulation
Instantiation
Reuse via Inheritance
Interfacing
Events
Benefits of ABAP Objects
Simplicity
Explicitness
Maintainability
Purified ABAP
Future Orientation
Conclusion
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 60
Maintainability
Programs using ABAP Objects are
easier to maintain
program structure better readable
better navigation
more reliable
encapsulation
secure typing
surprise free
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 61
Maintainability – Program Structure
No declaration of procedures FUNCTION f1. PERFORM s1 ... ENDFUNCTION. FORM s1 ... ... PERFORM f2 ... ... ENDFORM. FORM s2 ... ... PERFORM s3 ... ENDFORM.
Mixture of different procedures
FORM s3 ... ... ENDFORM.
Function Group SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 62
CLASS ... DEFINITION. PUBLIC SECTION. METHODS: m1 ... PRIVATE SECTION. METHODS: m2 ... m3 ... ENDCLASS.
Declaration mandatory
CLASS ... IMPLEMENTATION. METHOD m1. ... Navigation m2( ... ). ... ENDMETHOD. METHOD m2. ... m3( ... ). ... ENDMETHOD. METHOD m3. ... ENDMETHOD. ENDCLASS.
Class Pool
Only methods
Next Topic Background Principal Features of ABAP Objects
Encapsulation
Instantiation
Reuse via Inheritance
Interfacing
Events
Benefits of ABAP Objects
Simplicity
Explicitness
Maintainability
Purified ABAP
Future Orientation
Conclusion
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 63
Purified ABAP - Syntax (1)
ABAP/4 has developed over a long period of time
large number of obsolete statements
overlapping concepts
highly specialized concepts
surprising implicit behavior
difficult to learn
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 64
Purified ABAP - Syntax (2)
ABAP Objects reduces the complexity of ABAP
many obsolete statements and additions are forbidden
many implicit syntax completions must be done explicit
wrong handling of data is restricted
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 65
Purified ABAP – Syntax Examples (1)
Clean-Up in ABAP Objects
No implicit work areas
No internal tables with header lines specification of work areas
explicit
No TABLES work areas no data sharing between programs, explicit specification of work areas in Open SQL , ... Obsolete declaration!
Two data objects with one name!
DATA BEGIN OF itab OCCURS 10. ... CLEAR itab.
DATA itab TYPE TABLE OF ... ... CLEAR itab.
TABLES dbtab. SELECT * FROM dbtab.
DATA wa TYPE dbtab. SELECT * FROM dbtab INTO wa.
ABAP/4
Inappropriate short forms
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 66
ABAP Objects
Purified ABAP – Syntax Examples (2)
Clean-Up in ABAP Objects
No implicit typing of field-symbols and formal parameters Unexpected behavior!
FIELD-SYMBOLS
.
FIELD-SYMBOLS TYPE ANY.
IF IS ASSIGNED. ... ENDIF.
IF IS ASSIGNED. ... ENDIF.
ABAP/4
ABAP Objects Expected behavior!
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 67
Purified ABAP – Syntax Examples (3)
Clean-Up in ABAP Objects
No wrong data handling
DATA number TYPE i VALUE ... TRANSLATE number TO UPPER CASE.
ABAP/4
TRANSLATE number TO UPPER CASE.
ABAP Objects Undefined behavior!
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 68
DATA number TYPE i VALUE ...
Syntax error
Purified ABAP - Semantics
Unicode-enabled ABAP
Static type checks specified more precisely
Byte and character strings processed separately
Structures handled appropriately for their type using structural equivalence rules
Uncontrolled memory manipulation no longer permitted
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 69
Purified Semantics – Example (1)
Unicode-enabled ABAP
Structures are assigned fragment by fragment DATA: BEGIN OF struc1, col1(2) TYPE c VALUE 'AB', col2(2) TYPE c VALUE 'CD', END OF struc1.
Code page dependent representation of blanks
DATA: BEGIN OF struc2, col1(4) TYPE c, col2 TYPE i, END OF struc2. struc2 = struc1.
struc2 in non-Unicode-enabled ABAP:
ABCD
####
struc2 in Unicode-enabled ABAP:
ABCD
0
Type specific
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 70
Purified Semantics – Example (2)
Unicode-enabled ABAP
No access to memory outside a data object DATA: text1(10) TYPE c, text2(10) TYPE c VALUE 'ABCDEFGHIJ', off TYPE i. FIELD-SYMBOLS TYPE c. DO 15 TIMES. off = sy-index - 1. ASSIGN text1+off(1) TO . IF IS ASSIGNED. = 'X'. ENDIF. ENDDO.
Memory overwriting, even for references!
text2 in non-Unicode-enabled ABAP:
XXXXXFGHIJ
text2 in Unicode-enabled ABAP:
ABCDEFGHIJ
Memory protection SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 71
Purified Syntax + Purified Semantics
Unicode-enabled ABAP Objects!
Unicode-enabled ABAP Objects is the best ABAP available up to now!
If you use Unicode-enabled ABAP Objects, you program automatically according to the rules of purified syntax and semantics
Even if you do not exploit the real “object oriented” features, programs written in Unicode-enabled ABAP Objects are
more robust
less error prone
better to maintain
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 72
Next Topic Background Principal Features of ABAP Objects
Encapsulation
Instantiation
Reuse via Inheritance
Interfacing
Events
Benefits of ABAP Objects
Simplicity
Explicitness
Maintainability
Purified ABAP
Future Orientation
Conclusion
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 73
Future Orientation
(Unicode enabled) ABAP Objects is part of an ongoing evolution
Don‘t get lost!
Migration
R/2 ABAP
ABAP/4
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 74
ABAP Objects
Unicode enabled
Possible future developments
Next Topic Background Principal Features of ABAP Objects
Encapsulation
Instantiation
Reuse via Inheritance
Interfacing
Events
Benefits of ABAP Objects
Simplicity
Explicitness
Maintainability
Purified ABAP
Future Orientation
Conclusion
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 75
Conclusion - Summary
ABAP Objects offers
better encapsulation
better interfaces
better techniques for reuse
more static type security
support for multiple instantiation
better support for dynamic programming
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 76
Conclusion - Recommendation
Everybody programming in ABAP should use ABAP Objects for new and ongoing projects take
advantage of object oriented features
use
methods as far as possible, even if you stay within the procedural programming model
do
not use subroutines any more
use
function modules only when technically necessary (RFC, encapsulation of screens etc.)
disentangle
classical ABAP from ABAP Objects
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 77
Appendix
Stricter syntax in ABAP Objects Stricter syntax in Unicode programs Example for encapsulation of screens
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 78
Appendix A – Stricter Syntax in ABAP Objects (1/2) Notation: No special characters in names, no length specifications <= zero, no multi-line literals Declarations: LIKE references to data objects only; no implicit lengths or decimal places in TYPES; no length specifications for data types i, f, d , or t; no operational statements in structure definitions; FIELDS , RANGES, INFOTYPES , TABLES , NODES, COMMON PART , OCCURS , NONLOCAL not permitted Forbidden operations: CLEAR … WITH NULL , PACK, MOVE ... PERCENTAGE , ADD-CORRESPONDING, DIVIDE-CORRESPONDING , SUBTRACTCORRESPONDING , MULTIPLY-CORRESPONDING , ADD THEN ... UNTIL ... , ADD FROM ... TO ... , CONVERT {DATE|INVERTED DATE}
String processing: Not permitted on numeric data objects Field symbols: No implicit types; FIELD-SYMBOLS … STRUCTURE , ASSIGN ... TYPE, ASSIGN LOCAL COPY OF , ASSIGN TABLE FIELD not permitted Logic expressions: ><, =<, => not permitted, table used with IN must be a selection table Control structures: No operational statements between CASE and WHEN, ON-ENDON not permitted
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 79
Appendix A – Stricter Syntax in ABAP Objects (2/2)
Internal tables: No headers, no implicit work areas, no redundant key specifications, compatible work areas where necessary, obsolete READ variants and COLLECT ... SORTED BY , WRITE TO itab , PROVIDE (short form) not permitted
Procedures (methods): No implicit type assignment, compatible initial values only, passing sy-subrc not permitted, raising undefined exceptions not permitted
Program calls: No joint use of USING and SKIP FIRST SCREEN when calling transactions, passing formal parameters implicitly in CALL DIALOG not permitted Database accesses: No implicit work areas, no *-work areas, READ, LOOP, REFRESH FROM on database tables not permitted, VERSION addition to DELETE and MODIFY not permitted, no PERFORMING addition in Native SQL Data cluster : No implicit identifiers, passing parameters explicitly not permitted, no implicit work areas, MAJOR-ID and MINOR-ID not permitted Lists: DETAIL, SUMMARY , INPUT , MAXIMUM , MINIMUM , SUMMING , MARK , NEW SECTION and obsolete print parameters not permitted
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 80
Appendix B – Stricter Syntax in Unicode Programs
Offset/length accesses: Only performed on character-type or byte-type ranges, and only on flat character-type initial parts of structures
Memory accesses: No access to memory outside a data object
Separation of byte string and character string processing: Explicit specification with IN BYTE MODE or IN CHARACTER MODE ; appropriate types expected – for character strings this means only c, d , n, t, string, and flat structures with purely character-type components
Structures: When assigning and comparing you must take the Unicode fragment view into consideration
File interface: Implicitly opening files not permitted; access, storage, and coding type must be specified explicitly; no write access to read-only files
Conversions: TRANSLATE ... CODE PAGE ... , TRANSLATE ... NUMBER FORMAT ... not permitted
OPEN SQL: Stricter conditions for work areas
Type assignment using STRUCTURE: Stricter checks on assignments to field symbols and formal parameters typed using STRUCTURE
Function module calls: A specified formal parameter of a function module must be available
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 81
Appendix C – Encapsulation of Screens (1)
Encapsulation of screens in function groups FUNCTION-POOL flight_screens. SELECTION-SCREEN BEGIN OF SCREEN 100 AS WINDOW. PARAMETERS: p_carrid TYPE sflight-carrid, p_connid TYPE sflight-connid, p_fldate TYPE sflight-fldate. SELECTION-SCREEN END OF SCREEN 100. TABLES sflight. FUNCTION get_flight_parameters. CALL SELECTION-SCREEN 100 STARTING AT 10 10. ... " checks carrid = p_carrid. connid = p_connid. fldate = p_fldate. ENDFUNCTION. FUNCTION get_plane_type. sflight-planetype = plane_type. ... " preparation CALL SCREEN 200 STARTING AT 10 10. ... " checks plane_type = sflight-planetype. ENDFUNCTION.
Function Group SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 82
Selection Screen Interface data for dynpro
Screen handling in function modules
Further step: Local classes in function group as screen handlers
Appendix C – Encapsulation of Screens (2)
Object driven usage of screens CLASS flights DEFINITION. PUBLIC SECTION. METHODS: constructor, change_plane_type. PRIVATE SECTION. DATA flight TYPE sflight. ENDCLASS.
Call of functions with parameters
Class Pool
CLASS flights IMPLEMENTATION. METHOD constructor. DATA: carrid TYPE sflight-carrid, connid TYPE sflight-connid, fldate TYPE sflight-fldate. CALL FUNCTION 'GET_FLIGHT_PARAMETERS' IMPORTING carrid = carrid connid = connid fldate = fldate. SELECT SINGLE * FROM sflight INTO flight WHERE carrid = carrid AND connid = connid AND fldate = fldate. ENDMETHOD. METHOD change_plane_type. CALL FUNCTION 'GET_PLANE_TYPE' CHANGING plane_type = flight-planetype. ENDMETHOD. ENDCLASS.
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 83
Always the first source of information
http://www.intelligenterp.com/feature/archive/heymann.shtml http://www.intelligenterp.com/feature/archive/keller.shtml http://www.sappublications.com/insider/article.htm?key=20248
ABAP Objects, Introduction: ISBN 0-201-75080-5 (English) ISBN 3-89842-147-3 (German) ABAP Objects, Reference: ISBN 1-59229-011-6 (English) ISBN 3-934358-61-6 (German) SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 84
Questions?
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 85
Feedback
Please complete your session evaluation and drop it in the box on your way out.
The SAP TechEd ’03 Basel Team
SAP AG2003, TechED_Basel / ABAP256, Horst Keller, Stefan Bresch / 86