OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.
-
Upload
flora-robertson -
Category
Documents
-
view
215 -
download
3
Transcript of OpenEdge® Object-oriented ABL April 2014 Shelley Chase, Senior Architect Progress.
OpenEdge®Object-oriented ABL
April 2014Shelley 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
© 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)
© 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
© 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)
© 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
© 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.
© 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.
© 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.
© 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( ).
© 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.
© 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.
© 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.
© 2014 Progress Software Corporation. All rights reserved.14
Questions
Type
Class
Object
Class members
• Constructor
• Destructor
• Method
• Variable
• Property
© 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
© 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.
© 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.
© 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.
© 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
© 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.
© 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.
© 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
© 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() */
© 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
© 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
© 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().
© 2014 Progress Software Corporation. All rights reserved.27
Questions
Inheritance
Interfaces
Override
Polymorphism
Abstract class
Package
© 2014 Progress Software Corporation. All rights reserved.28
OOABL Specializations
Classes and Procedures
Progress.Lang.Object
Progress.Lang.Class
Compiler changes
© 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
© 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
© 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)
© 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
© 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
© 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
© 2014 Progress Software Corporation. All rights reserved.35
Questions
Classes and Procedures
Progress.Lang.Object
Progress.Lang.Class
Compiler changes
© 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
© 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"
© 2014 Progress Software Corporation. All rights reserved.38
Questions