OpenEdge 10.1a OO4GL

Download Report

Transcript OpenEdge 10.1a OO4GL

OpenEdge®
Object-oriented ABL
April 2014
Shelley Chase, Senior Architect Progress
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
2
© 2014 Progress Software Corporation. All rights reserved.
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)
3
© 2014 Progress Software Corporation. All rights reserved.
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
4
© 2014 Progress Software Corporation. All rights reserved.
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)
5
© 2014 Progress Software Corporation. All rights reserved.
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
6
© 2014 Progress Software Corporation. All rights reserved.
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.
7
© 2014 Progress Software Corporation. All rights reserved.
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.
8
© 2014 Progress Software Corporation. All rights reserved.
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.
9
© 2014 Progress Software Corporation. All rights reserved.
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( ).
10
© 2014 Progress Software Corporation. All rights reserved.
OO Definition: Method
 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.
/* Call Add method of the class
*/
/* total = iNum1 + iNum2.
*/
/* message total view-as alert-box */
theCalculator = new Calculator( ).
theCalculator:Add(3, 4).
11
© 2014 Progress Software Corporation. All rights reserved.
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.
12
© 2014 Progress Software Corporation. All rights reserved.
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.
13
© 2014 Progress Software Corporation. All rights reserved.
Questions
 Type
 Class
 Object
 Class members
• Constructor
• Destructor
• Method
• Variable
• Property
14
© 2014 Progress Software Corporation. All rights reserved.
OO Programming Using Types
 Functionality centered around relationships
15
• 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.
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.
16
© 2014 Progress Software Corporation. All rights reserved.
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.
17
© 2014 Progress Software Corporation. All rights reserved.
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.
18
© 2014 Progress Software Corporation. All rights reserved.
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
19
© 2014 Progress Software Corporation. All rights reserved.
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.
20
© 2014 Progress Software Corporation. All rights reserved.
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.
21
© 2014 Progress Software Corporation. All rights reserved.
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
22
© 2014 Progress Software Corporation. All rights reserved.
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() */
23
© 2014 Progress Software Corporation. All rights reserved.
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
24
© 2014 Progress Software Corporation. All rights reserved.
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
25
© 2014 Progress Software Corporation. All rights reserved.
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().
26
© 2014 Progress Software Corporation. All rights reserved.
Questions
 Inheritance
 Interfaces
 Override
 Polymorphism
 Abstract class
 Package
27
© 2014 Progress Software Corporation. All rights reserved.
OOABL Specializations
 Classes and Procedures
 Progress.Lang.Object
 Progress.Lang.Class
 Compiler changes
28
© 2014 Progress Software Corporation. All rights reserved.
Classes and Procedures
Classes
29
Procedures
1.
Class files (.cls)
1.
Procedure file (.p)
2.
Data members
2.
Define variables
3.
Void methods
3.
Internal procedures
4.
Non-void methods
4.
User-defined functions
5.
Constructor
5.
Code in main block
6.
Inheritance
6.
Super procedures
© 2014 Progress Software Corporation. All rights reserved.
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
30
© 2014 Progress Software Corporation. All rights reserved.
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)
31
© 2014 Progress Software Corporation. All rights reserved.
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
32
© 2014 Progress Software Corporation. All rights reserved.
Progress.Lang.Class
 Used for reflection
 One per user-defined type
• Methods
– IsInterface ()
– IsFinal ()
• Properties
– TypeName
– Package
– SuperClass
33
© 2014 Progress Software Corporation. All rights reserved.
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
34
© 2014 Progress Software Corporation. All rights reserved.
Questions
 Classes and Procedures
 Progress.Lang.Object
 Progress.Lang.Class
 Compiler changes
35
© 2014 Progress Software Corporation. All rights reserved.
Part 2: Even More Advanced Features
 Static Class members
 Events
 Chaining method calls
 Garbage collection
 Class browser in PDSOE
36
© 2014 Progress Software Corporation. All rights reserved.
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" 
37
© 2014 Progress Software Corporation. All rights reserved.
Questions
38
© 2014 Progress Software Corporation. All rights reserved.