Friday, November 23, 2007

ABAP Real time object for To Use BADI - Business Add In you need to Understand ABAP OO Interface Concept

Report Z_CTRY.

* A Shape Interface "Like a shape" "Behaves like a Shape"
Adverb/Adjective

Interface IShape.
Methods: getArea Returning Value(area) Type F,
getCircumference Returning Value(circumference) Type F.
Endinterface.

* A Circle Class that behaves like a Shape
Class CCircle Definition.

Public Section.

Interfaces IShape.

Aliases: getArea For IShape~getArea,
getCircumference For IShape~getCircumference.

Methods: Constructor Importing pRadius Type F,
setRadius Importing pRadius Type F,
getRadius Returning Value(pRadius) Type F.

Private Section.
Data radius Type F.
Constants PI Type F Value '3.141592365359'.
EndClass.

Class CCircle Implementation.

Method Constructor.
radius = pRadius.
EndMethod.

Method setRadius.
radius = pRadius.
EndMethod.

Method getRadius.
pRadius = radius.
EndMethod.

Method IShape~getArea.
Compute area = 2 * PI * radius.
EndMethod.

Method IShape~getCircumference.
Compute circumference = PI * radius * radius.
EndMethod.

EndClass.

* A Square Class
Class CSquare Definition.

Public Section.

Interfaces IShape.

Aliases: getArea For IShape~getArea,
getCircumference For IShape~getCircumference.

Methods: Constructor Importing pSide Type F.

Private Section.
Data side Type F.
EndClass.

Class CSquare Implementation.

Method Constructor.
side = pSide.
EndMethod.

Method IShape~getArea.
Compute area = side * side.
EndMethod.

Method IShape~getCircumference.
Compute circumference = 4 * side.
EndMethod.

EndClass.

* A Rectangle Class
Class CRectangle Definition.

Public Section.

Interfaces IShape.

Aliases: getArea For IShape~getArea,
getCircumference For IShape~getCircumference.

Methods: Constructor Importing pHeight Type F
pLength Type F.

Private Section.
Data: height Type F,
length Type F.
EndClass.

Class CRectangle Implementation.

Method Constructor.
height = pHeight.
length = pLength.
EndMethod.

Method IShape~getArea.
Compute area = height * length.
EndMethod.

Method IShape~getCircumference.
Compute circumference = 2 * ( height + length ).
EndMethod.

EndClass.

* START of PROGRAM

* Array of Shapes
Data: OneShape Type Ref To IShape, " One Object with
Shape Behaviour
ShapeTable Type Table Of Ref To IShape. " Array of Objects
with Shape Behaviour

* Concrete Objects with IShape Behaviour!
Data: C1 Type Ref To CCircle,
S1 Type Ref To CSquare,
R1 Type Ref To CRectangle,
C2 Type Ref To CCircle,
S2 Type Ref To CSquare,
R2 Type Ref To CRectangle.


Data: descr_ref TYPE ref to CL_ABAP_TYPEDESCR,
ClassName Type String,
Serial Type I.

Data: myArea Type F,
myCircumference Type F.

START-OF-SELECTION.
Create Object C1 Exporting pRadius = '2.5'.
Create Object C2 Exporting pRadius = '5.0'.

Create Object S1 Exporting pSide = '3.5'.
Create Object S2 Exporting pSide = '6.0'.

Create Object R1 Exporting pHeight = '2.8' pLength = '3.4'.
Create Object R2 Exporting pHeight = '1.7' pLength = '6.3'.

* Append in any order!
Append S1 to ShapeTable.
Append R2 to ShapeTable.
Append R1 to ShapeTable.
Append C2 to ShapeTable.
Append C1 to ShapeTable.
Append S2 to ShapeTable.

Serial = 0.

Loop At ShapeTable into OneShape.
Call Method OneShape->getArea
Receiving area = myArea.
Call Method OneShape->getCircumference
Receiving circumference = myCircumference.

descr_ref = CL_ABAP_TYPEDESCR=>Describe_By_Object_Ref( OneShape
).
Call Method descr_ref->get_relative_name
Receiving P_RELATIVE_NAME = ClassName.

Add 1 to Serial.

Write: / Serial, ClassName.
Write: / 'Area ', myArea Decimals 4 Exponent
0.
Write: / 'Circumference ', myCircumference Decimals 4 Exponent
0.
Write: /.

EndLoop.

** Results
* 1 CSQUARE
* Area 12.2500
* Circumference 14.0000
*
* 2 CRECTANGLE
* Area 10.7100
* Circumference 16.0000
*
* 3 CRECTANGLE
* Area 9.5200
* Circumference 12.4000
*
* 4 CCIRCLE
* Area 31.4159
* Circumference 78.5398
*
* 5 CCIRCLE
* Area 15.7080
* Circumference 19.6350
*
* 6 CSQUARE
* Area 36.0000
* Circumference 24.0000

No comments:

Blog Archive