OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

39
OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress

Transcript of OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

Page 1: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

OpenEdge®Object-oriented ABL

April 2014Shelley Chase, Senior Architect Progress

Page 2: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.2

Object-oriented programming in ABL (OOABL)

Introduced in 2007 with OE Release 10

• Interoperates with procedures

– Procedures can call classes; classes can call procedures

– Same ABL logic in both

New features implemented in OOABL

• When possible – already have enough keywords

• Easy to consume – usage is very similar to persistent procedures

GUI for .NET uses OOABL

• Use .NET classes to build rich desktop applications

• Use .NET classes for functionality not in ABL

Page 3: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.3

Why should I Use Object-oriented (OO) programming?

Clean-er, “bug-free” code

• Compile-time validation for programming errors beyond syntax

• Tooling: Content-assist based on object definition

Encapsulation and easier code reuse

• Protection levels identify external interface

• Common code in “super” class – seen at compilation

• Customize behavior without changing original code

Most modern programming languages use OO concepts

• You can use words like “factory”, “instantiation”, “inheritance” and polymorphism (and know what they mean)

Page 4: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.4

OO Programming Basics

Functionality centered around “objects”

• Type: definition of API and relationships with other Types

• Class: code represents data and behavior; implements a Type

• Object: runtime instance of a Class

Other basic features

• Class members – constructors, destructors, methods, data

Page 5: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.5

OO Definition: Type

Type: Meta-data for a class

• Data members – variable and properties (getter and setter)

• Method signatures – API for the class

– Logic is not part of the type

• Identifies relationships with other Types

– Inheritance from other Types

– Implement an Interface (special Type)

Page 6: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.6

OO Definition: Class

Class: ABL file with a .cls extension

• Constructor called when class instantiated – place to do initialization

• Destructor called when class is deleted – place to do cleanup

• Methods are just like procedures and user-defined functions

• Variables represent the state of the object

• Properties wrap variables with a getter and setter

Implemented in a class file (.cls)

• Compiles to .r just like procedures; can be put into PL files

• Similar to a persistent procedure

Page 7: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.7

Sample OOABL Class – Starts with Type Definition

class Calculator:

define variable total AS decimal NO-UNDO.

constructor public Calculator( ):total = 0.

end constructor.

method void Add(iNum1 as integer, iNum2 as integer):

total = iNum1 + iNum2.

message total view-as alert-box.

end method.

end class.

Page 8: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.8

Sample OOABL Class – Constructor

class Calculator:

define variable total AS decimal NO-UNDO.

constructor public Calculator( ):total = 0.

end constructor.

method void Add(iNum1 as integer, iNum2 as integer):

total = iNum1 + iNum2.

message total view-as alert-box.

end method.

end class.

Page 9: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.9

Sample OOABL Class – Method

class Calculator:

define variable total AS decimal NO-UNDO.

constructor public Calculator( ):total = 0.

end constructor.

method void Add(iNum1 as integer, iNum2 as integer):

total = iNum1 + iNum2.

message total view-as alert-box.

end method.

end class.

Page 10: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.10

OO Definition: Object

Object: A running instance of a class• Object reference used to access instance – “strongly-typed” handle

• Creating the object runs the constructor – like block 0 of a persistent proc

– Each instance keeps context in data members

• Deleting the object runs the destructor

• Similar to a persistent procedure

Run foo.p persistent set fooHandle.

/* Create instance of a class */

define variable theCalculator as Calculator( ).theCalculator = new Calculator( ).

Page 11: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.11

OO Definition: Method

/* Call Add method of the class *//* total = iNum1 + iNum2. *//* message total view-as alert-box */

theCalculator = new Calculator( ).

theCalculator:Add(3, 4).

Method: business logic

• Run by calling method on object reference

• Signature is strongly-typed

– Parameters checked at compile-time

• Similar to calling an internal proc or user-defined function Run myProc in fooHandle.

Page 12: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.12

OO Definition: Variable

Variable: defines the state of the object instance

• Defined outside of methods (not global)

• Use DEFINE syntax with new access levels

– PRIVATE

– PROTECTED

– PUBLIC

• Accessed through object reference

/* Access total variable of Calculator */

theCalculator = new Calculator( ).

message theCalculator:total view-as alert-box.

Page 13: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.13

OO Definition: Properties

Variable: defines the state of the object instance

• Defined outside of methods (not global)

• Includes variable definition plus GET and SET methods

– Can validate values before setting variable

– Read-only = GET and private SET

class Calculator:

define property total AS decimal NO-UNDO GET private SET.

end class.

Page 14: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.14

Questions

Type

Class

Object

Class members

• Constructor

• Destructor

• Method

• Variable

• Property

Page 15: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.15

OO Programming Using Types

Functionality centered around relationships

• Interface Type definition without implementation

• Inheritance Relationships between Types

• Override Customize behavior

• Polymorphism “Cool trick”

• Abstract class Part Class, part Interface

• Package Namespace for grouping

Page 16: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.16

Definition: Interface

Special type of Class that has no code

• Basically a Class with only Type information: Data and Methods

Defines a contract (API)

• Any class that implements the interface must code all methods

• Used when behavior must be specialized

• Compiler validates implementation of interface

Interface iBaseCalculator:

method void Subtract(iNum1 as integer, iNum2 as integer).method void Add(iNum1 as integer, iNum2 as integer).

end interface.

Page 17: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.17

Sample OOABL Class – Using an Interface

class Calculator implements iBaseCalculator:

define variable total AS decimal initial 0 NO-UNDO.

method void Add(iNum1 as integer, iNum2 as integer):

total = iNum1 + iNum2.

message total view-as alert-box.

end method.

method void Subtract(iNum1 as integer, iNum2 as integer):

total = iNum1 - iNum2.

message total view-as alert-box.

end method.

end class.

Page 18: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.18

Sample OOABL Class – Compile time Validation

class Calculator implements iBaseCalculator:

define variable total AS decimal initial 0 NO-UNDO.

method void Add(iNum1 as integer, iNum2 as integer):

total = iNum1 + iNum2.

message total view-as alert-box.

end method.

method void Subtract(iNum1 as integer, iNum2 as integer):

total = iNum1 - iNum2.

message total view-as alert-box.

end method.

end class.

Page 19: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.19

Definition: Inheritance

Inheritance: defines relationships among classes• Super class – common data and functionality that can be shared by

classes

• Subclass – specialized class that inherits from a Super class

– Inherits public & protected data members and methods

– Can add additional data and methods

– Override can augment OR override super class behavior

OOABL super class• All Types implicitly inherit from this super class

Progress.Lang.Object

Page 20: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.20

Sample OOABL Class – Inheritance

class AdvancedCalculator inherits Calculator: /* Inherits data members and methods from Calculator */

/* Extend the class with a new method */

method void Multiply(iNum1 as integer, iNum2 as integer):

total = iNum1 * iNum2.

message total view-as alert-box.

end method.

end class.

Page 21: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.21

Definition: Override

Override: Change the behavior of a super class method

• Overridden method must have same signature

• Can be used to define totally new behavior

• Or do pre-processing or post-processing

– To access super class method: SUPER:<method-name>

Class SubCalculator implements iBaseCalculator:

method override Add(iNum1 as integer, iNum2 as integer):

/* new behavior */

end method.

end class.

Page 22: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.22

Definition: Polymorphism

Polymorphism: Ability to write generic code but call custom methods

• Code written using super class / interface (parent) type

– Tightly coupled to inheritance, interface and override behavior

– invoke an overridden method in a class

– Parent variable used at compile time

– Subclass created and assigned to parent variable at runtime

• Cool trick: Method called on parent object reference actually calls subclass method

– Method call on parent dispatched to subclass’ method at runtime

Page 23: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.23

Invoke a Polymorphic Method

class BackwardCalculator implements iBaseCalculator:

method void Subtract(iNum1 as integer, iNum2 as integer):

total = iNum2 – iNum1 /* swap order */.

end method.end class.===============================================================define variable cal1 as iBaseCalculator.define variable cal2 as iBaseCalculator.

cal1 = new Calculator().cal1:Subtract(1-9). /* Calls Calculator:Subtract() */

cal2 = new BackwardCalculator().cal2:Subtract(1-9). /* Calls BackwardCalculator:Subtract() */

Page 24: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.24

Benefits of Polymorphism

Supports generic programming using super class or interface

• Type used at compile time is super class or interface

• New subclasses can be defined and code doesn’t need to change

Specialized behavior is called at runtime automatically

Page 25: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.25

Definition: Abstract Class

Abstract class: part of class implementation is missing

• Combines an interface and a class in a single definition

– Used when some behavior must be specialized and some can be shared

• All implemented properties and methods are available to subclass

A subclass needs to inherit from an abstract class

• Class cannot be directly instantiated

Page 26: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.26

Package

Package: Uniquely identify the Type from other Types

• Type name = Package name and Class name

• Defines the directory where code lives

foo.bar.MyClass is foo/bar/MyClass.cls

• Must identify Type using fully qualified Type name

– Or add USING statement for the class or package with wildcard

/* Class file must be located in math/Calculator.r */Class Math.Calculator: ...============================================define variable cal as Math.Calculator.cal = new Math.Calculator().

Page 27: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.27

Questions

Inheritance

Interfaces

Override

Polymorphism

Abstract class

Package

Page 28: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.28

OOABL Specializations

Classes and Procedures

Progress.Lang.Object

Progress.Lang.Class

Compiler changes

Page 29: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.29

Classes and Procedures

Classes Procedures

1. Procedure file (.p)

2. Define variables

3. Internal procedures

4. User-defined functions

5. Code in main block

6. Super procedures

1. Procedure file (.p)

2. Define variables

3. Internal procedures

4. User-defined functions

5. Code in main block

6. Super procedures

1. Class files (.cls)

2. Data members

3. Void methods

4. Non-void methods

5. Constructor

6. Inheritance

1. Class files (.cls)

2. Data members

3. Void methods

4. Non-void methods

5. Constructor

6. Inheritance

Page 30: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.30

Classes and Procedures: Interoperability

Procedures

• Can use an Object

– Create and delete an Object

– Invoke methods using object reference

– Pass objects as a parameter

Classes

• Can RUN a procedure or persistent procedure

– Can RUN internal procedures and user-defined functions on a handle

Page 31: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.31

Classes and Procedures: Behavior Differences

Procedures and Classes

• Modular programming

• Supports reuse of common behavior (super)

Classes only

• Programming errors caught early by compiler

• Natural integration with modeling tools and other Object-oriented platforms like .NET™

• Modern programming model (used at most universities)

Page 32: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.32

Progress.Lang.Object

Implicit super class for all user-defined classes. Facilitates generic code

• Methods

– ToString ( )

– GetClass ( )

– Equals ( ) – Must be overridden

– Clone ( ) – Must be overridden

• Properties

– Next-sibling

– Prev-sibling

Page 33: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.33

Progress.Lang.Class

Used for reflection

One per user-defined type

• Methods

– IsInterface ()

– IsFinal ()

• Properties

– TypeName

– Package

– SuperClass

Page 34: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.34

Compiler changes

Two pass compiler

• First pass gathers Type information

• Second pass does validation and compilation

– Compile time validation of object reference

– Validates

o Methods

o Parameters

Compiles all files in class hierarchy

• Does not compile subclasses

• Might need to compile Types referenced in code

Page 35: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.35

Questions

Classes and Procedures

Progress.Lang.Object

Progress.Lang.Class

Compiler changes

Page 36: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.36

Part 2: Even More Advanced Features

Static Class members

Events

Chaining method calls

Garbage collection

Class browser in PDSOE

Page 37: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.37

In Summary

Standard OO concepts available in the 4GL

Built on top of existing 4GL constructs

Interoperability between Classes and Procedure

"Be part of the cool crowd"

Page 38: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.

© 2014 Progress Software Corporation. All rights reserved.38

Questions

Page 39: OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.