Omkar Tamhane Myriad Soft Corporation

ABAP Objects as a strategic SAP technology
`

Programming with objects, classes, and interfaces in ABAP Interoperability with other object systems

`

` `

Benefits of object orientation Current situation
External interoperability ABAP programming

` `

What are ABAP Objects ? Benefits of ABAP Objects

` ` ` ` ` `

Encapsulation - outside vs. inside Explicit interfaces Control of complexity and dependencies Reuse - of components and by inheritance Maintainability Interoperability across languages (Java, VB, ...) and object systems (DCOM/CORBA) Foundation for patterns and frameworks

`

`

BOR (Business Object Repository)
Foundation for DCOM/CORBA connection Fully featured standard object model Medium level object wrappers for business functionality written in standard ABAP

`

BAPIs (Business APIs)
Defined within the BOR Function-oriented, stable interfaces to R/3 applications Support for Internet applications

`

Complexity reduction by powerful high-level programming constructs
Procedural abstraction (function library) Data abstraction (type pools, complex data types) Logical databases for hierarchical data access Event-oriented programming with logical databases and interactive reporting Fully integrated SQL interface In-memory tables: fast key access, sorted and/or nested, group control, ... ...

`

Complete integration of a fully featured object model into the ABAP programming language 100% upward-compatible extension of ABAP/4
Bottom up: use objects in existing ABAP programs (reports, module- and function-pools) Top down: call forms and functions from within objects All ABAP language constructs are available within objects Fully integrated into the ABAP Workbench

`

`

Class library for global classes (will absorb BOR)

` ` `

Identical object model for external access and internal usage Seamless object model from analysis through design to implementation Kernel-embedded foundation for objects
Make OO benefits available for the implementation of the world¶s largest business application True two-way interoperability: ABAP <=> Java, ABAP <=> VB, ... Speed, speed, speed !

`

Enabling technology for GUI programming with frontend controls (ActiveX, JavaBeans)

`

ABAP Objects as a strategic SAP technology Programming with objects, classes, and interfaces in ABAP

`

Interoperability with other object systems

`

Objects occur 'naturally' in the world. We want to model our software accordingly
E.g.: Transportation company: trucks (various kinds), loads (various), etc.

`

Functions and data
µBig common data structure¶ and some common functions Lots of CASE statements, sparsely filled data structures

`

Objects: car, truck, load, «
Various kinds of everything, objects for truck, load, « Object: data and functions that belong together to model / implement a specific concept Fewer CASE statements, densely filled data, cohesion

`

Objects have«
...state, described by its attributes ...behavior, described by its methods ...identity to distinguish them from other objects with same state and behavior

`

Objects can interact with each other...
...by accessing (public) attributes ...by calling methods ...by raising or handling events

`

Objects are instances of classes

`

Classes«
...specify behavior of µsame kind of¶ objects ...define how objects can be accessed from outside (public vs. protected vs. private) ...hide implementation details ...may be specialized in subclasses
CLASS class DEFINITION [ INHERITNG FROM superclass ]. [ PUBLIC SECTION. ...<definition of public components> ] [ PROTECTED SECTION. ...<definition of protected components> ] [ PRIVATE SECTION. ...<definition of private components> ] ENDCLASS. CLASS class IMPLEMENTATION. [...<method implementations> ] ENDCLASS.

CLASS CTruck DEFINITION. PUBLIC SECTION. DATA: VehicleId TYPE I READ-ONLY. METHODS: LoadParcel IMPORTING Parcel TYPE REF TO CParcel, UnloadParcel « PRIVATE SECTION. DATA: ParcelTab TYPE REF TO CParcel OCCURS 0. ENDCLASS. CLASS CTruck IMPLEMENTATION. METHOD LoadParcel. APPEND Parcel TO ParcelTab. ³-- do more stuff « ENDMETHOD. ENDCLASS. PROGRAM xy. DATA: Parcel TYPE REF TO CParcel, Truck1 TYPE REF TO CTruck, Truck2 TYPE REF TO CTruck. « ³-- get input data for parcel from somewhere « CREATE OBJECT Parcel. CALL METHOD Parcel->SetPars EXPORTING Weight = In_weight. ³--- deal with multiple instances CALL METHOD Truck1->UnloadParcel IMPORTING Parcel = Parcel. CALL METHOD Truck2->LoadParcel( Parcel ).

`

Objects are created dynamically
Storage management, garbage collection

`

Access to objects via object reference only!!!
Distinguish instances by object reference Only and explicit means of dependency Sharing always and only via (object) references (similar to field-symbols; all other ABAP types are value-based!)

`

Internal data hidden from users
Private data accessible only by the object¶s methods

`

Attributes«
...store the internal state of an object (data) ...can be references to other objects «can be: read-only, virtual, class attributes «can be constants Virtual attributes: µAttribute¶ from the outside, inside the object Setand Get-methods. Dynamic control of Set-/Get-methods.

`

Methods«
«are operations on objects (the µfunctionality¶) «are the only way to change the state of an object (other than public attributes) ...have parameters and can raise exceptions (similar to function modules) ...can pass back a return value No method-name overloading!

CLASS c1 DEFINITION. PUBLIC SECTION. DATA: v1 TYPE I, o1 TYPE REF TO c1. METHODS: m1 IMPORTING a1 TYPE REF TO c1, m2 IMPORTING a1 TYPE REF TO c1 RETURNING result TYPE I. PRIVATE SECTION. DATA: v2 TYPE I. ENDCLASS. PROGRAM xy. DATA o1 TYPE REF TO c1. « ³--- attribute can occur anywhere a µnormal variable¶ can occur CREATE OBJECT o1. x = o1->v1 + sin( o1-> v1 ). CALL FUNCTION 'abc' EXPORTING p1 = o1->v1 « . ³--- some method calls « CALL METHOD o1->m1 EXPORTING a1 = o1. CALL METHOD o1->m1( o1 ). ³-- short form for 1 exporting arg « y = obj1->m2( x ). ³-- result can be used in expressions «

`

Events...
...occur at a particular point in time, e.g. µchange in state of an object¶ ...can be raised to inform other interested objects ...can pass parameters

`

Events are handled by classes
General publish-subscribe model Syntax similar to µVisual Basic¶ event handling

`

Event handlers...
...are methods for handling events from other objects ...are declared with reference to the event to be handled (signature from there) «must be µregistered¶ explicitly

Event Handling Example
*---- proxy class for GUI control CLASS CButton DEFINITION. PUBLIC SECTION. METHODS: SetLabel IMPORTING Txt TYPE « . EVENTS: Clicked EXPORTING DoubleClick TYPE I. ENDCLASS. CLASS CWindow1 DEFINITION. PUBLIC SECTION. "--- handle events by implementing "--- event handler methods METHODS: OKClicked FOR EVENT Clicked OF CButton IMPORTING DoubleClick, CanClicked FOR EVENT Clicked OF CButton. DATA: OKBtn TYPE REF TO CButton. « ENDCLASS. CLASS CWindow1 IMPLEMENTATION. METHOD Init. CREATE OBJECT: OKBtn, CanBtn. SET HANDLER: OKClicked FOR OKBtn, CanClicked FOR CanBtn. ENDMETHOD. METHOD OKClicked. IF DoubleClick = 1. ENDMETHOD.

CLASS CButton IMPLEMENTATION. « METHOD AnyMethod. « RAISE EVENT Clicked EXPORTING DoubleClick = 0. « ENDMETHOD. ENDCLASS.

«

ENDIF.

METHOD CancelClicked. « "--- DoubleClick not visible ENDMETHOD. ENDCLASS.

`

Class attributes...
...are data on class level, independent of object / instance ...are µalways there¶ like global variables / functions ...have global lifetime, with scope tied to class

`

Class methods...
...can only access class attributes ...can be called like µglobal functions¶, but are tied to class
*--- class attribute definition CLASS-DATA: var TYPE t « . *--- class method definition CLASS-METHODS: cm « <parameter syntax like methods>.

*---- Transaction controller for nested transactions ---CLASS TACtrl DEFINITION. PUBLIC SECTION. ³--- class method to create new controller instance CLASS-METHODS: CreateNew RETURNING TaObj TYPE REF TO TACtrl. CLASS-DATA: Current TYPE REF TO TACtrl READ-ONLY. METHODS: Commit, Abort. ³-- instance methods PRIVATE SECTION. CLASS-DATA: TAStack TYPE REF TO TACtrl OCCURS 0. ENDCLASS. CLASS TACtrl IMPLEMENTATION. METHOD CreateNew. DATA NewTA TYPE REF TO TACtrl. CREATE OBJECT NewTA. APPEND NewTA TO TAStack. Current = NewTA. ENDMETHOD. ENDCLASS. PROGRAM xy. ³--- start nested transaction CALL METHOD TACtrl=>CreateNew. « CALL METHOD TACtrl=>Current->Commit.

`

A class can be derived from another
Only specify what is different / added Add attributes and methods Redefine / override existing methods (in any section) = change implementation, µslight change¶ of interface possible µSingle inheritance¶ on class
CLASS class DEFINITION INHERITING FROM superclass. « SECTION. ³--- added attributes and methods DATA: « METHODS: « ³--- override / redefine existing method METHODS m REDEFINITION « ENDCLASS.

`

Polymorphism on object references
DrawableObject

CLASS DrawableObject DEFINITION PUBLIC SECTION. METHODS: Draw. ENDCLASS. CLASS Polygon DEFINITION INHERITING FROM DrawableObject. PUBLIC SECTION. METHODS: AddPoint IMPORTING P TYPE T_Point, Draw REDEFINITION. PRIVATE SECTION. DATA: PointTab TYPE T_Point OCCURS 0. ENDCLASS. CLASS Polygon IMPLEMENTATION. METHOD Draw. DATA: Point TYPE T_Point. LOOP AT PointTab INTO Point. CALL METHOD DrawableObject=>Draw( Point ). ENDLOOP. ENDMETHOD. ENDCLASS.

Point
PROGRAM xy.

Polygon

Bitmap

DATA: DObj TYPE REF TO DrawableObject. DATA: DObjTab TYPE REF TO DrawableObject OCCURS 0. ³--- create drawable objects « ³--- draw all of them LOOP AT DObjTab INTO DObj. CALL METHOD DObj->Draw. ENDLOOP.

Interfaces
`

`

` `

Interfaces define the interaction between different objects Polymorphism independent of class / inheritance Classes can implement multiple interfaces Uniform access through interface reference
Archi e

IArchi e

Customer

¡

Plan

¡   ¢

gr

aterial

Interface Definition
`

Interfaces...
«can define same components as class - without implementation ...may µenclose¶ multiple other interfaces (hierarchy) «have separate name spaces for their components Components of enclosed interfaces are not visible in the top-level interface (µblack boxes¶); there is a mapping/aliasing feature

INTERFACE interface. [ INTERFACES ...<list of comprised interfaces> .] [ ...<definition of interface components> ] ENDINTERFACE.

Interfaces
`

Implementation of interfaces
A class can implement many interfaces Interfaces are implemented µside-by-side¶ in a class (like COM) No name conflicts on the class level No semantic conflicts at class level and interface composition

`

Using interfaces
Access by interface reference like object reference An interface reference only exposes the components of that interface Assignment / µcast¶ to another interface possible

CLASS CArchiveMgr. INTERFACE IArchive DEFINITION. DATA: ObjID TYPE T_OID VIRTUAL. ³-- fast EVENTS: Saved, « . METHODS: SaveYourself IMPORTING « . ENDINTERFACE. CLASS Customer DEFINITION. INTERFACES: IArchive, IWorkflow, « . ENDCLASS. CLASS Customer IMPLEMENTATION. « METHOD IArchive~GET_ObjID. CALL FUNCTION µArchive_Get_OID¶ « IMPORTING IArchive~objid. ³-- no more recompute SET DIRECT READ ACCESS FOR IArchive~ObjID. ENDMETHOD. « METHOD IArchive~SaveYourself. ³--- save all own data into « « RAISE EVENT IArchive~Saved « . ENDMETHOD. « ENDCLASS. DATA: IAObj TYPE REF TO IArchive. DATA: IATab TYPE REF TO IArchive OCCURS 0. « METHOD AddToArchive IMPORTING IAObj « APPEND IAObj TO IATab. ENDMETHOD. METHOD DoArchive. ³--- archive all objects in table LOOP AT IATab INTO IAObj. WRITE: / ³Wrote:´, IAObj->ObjID. CALL METHOD IAObj->SaveYourself « . ENDLOOP. ENDMETHOD.
CArchiveMgr

Plan1

iaTab

IArchive

Plan2

Material

Customer

Object reference

class 1 specific

Class 1 Part

Interface 1

Interface 2
Interface reference

Comprising Interface 3 Interface 4 Implementing Class 1 Inheriting from Interface 5

if 3 specific

Interface 3 Interface reference
interface 1

Interface reference

interface 2 if 4 specific

Interface 4

Class 2

Interface reference

Object reference

class 2 specific

Class 2 Part

Interface reference

interface 5

`

Class components...
«share a common name space within the class ...may be x public = visible to all x protected = visible to subclasses and implementation x private = visible to the class implementation only ...depend on instance data or not

`

Interface components
Separate name space for interface components Interfaces are visible as a whole (like µview¶)

Avoid naming conflicts, selectively make components visible
{CLASS « DEFINITION | INTERFACE « }. INTERFACES i. ... ALIASES a FOR i~a. {ENDCLASS|ENDINTERFACE}.

Constructor (Destructor)
CLASS class DEFINITION. ... METHODS CONSTRUCTOR IMPORTING p TYPE t « . « ENDCLASS.

"--- name / syntax TBD

Friends
CLASS c1 DEFINITION EXPOSING PRIVATE COMPONENTS TO c2. ... PRIVATE SECTION. ENDCLASS. CLASS c2 DEFINITION ACCESSING PRIVATE COMPONENTS OF c1. ... PRIVATE SECTION. ENDCLASS.

`

Summary
Classes and interfaces Attributes, methods, and events Classes can implement interfaces Interface composition Single inheritance for classes, multiple composition + aliasing for interfaces Event handling

` `

ABAP Objects as a strategic SAP technology Programming with objects, classes, and interfaces in ABAP Interoperability with other object systems

Interoperability: DCOM and CORBA
Client / Server Client / Server

Visual Basic

Component Connector DCOM ABAP Objects CORBA

*Script, ...

Java CORBA Bridge

`

Transparent two-way mapping between ABAP Objects and external object models Automatic generation of proxies and stubs Location transparency:
CREATE OBJECT obj DESTINATION dest

` `

`

Delta management for mass data For details see presentation on Distributed Objects

Sign up to vote on this title
UsefulNot useful