dev-6: advanced object-oriented programming in the abl evan bleicher [email protected] senior...
TRANSCRIPT
DEV-6: Advanced Object-Oriented Programming in the ABL
Evan [email protected] Development Manager
Shelley [email protected]
Architect
© 2006 Progress Software Corporation2DEV-6 Advanced Object-oriented Programming in the ABL
D I S C L A I M E R
Under Development
This talk includes information about potential future products and/or product enhancements.
What we are going to say reflects our current thinking, but the information contained herein is preliminary and subject to change. Any future products we ultimately deliver may be materially different from what is described here.
D I S C L A I M E R
© 2006 Progress Software Corporation3DEV-6 Advanced Object-oriented Programming in the ABL
Agenda
Order Entry System Object Model Abstract Programming with Classes Working with Datasets / Temp-tables in Classes Miscellaneous Topics:
• Debugging with Classes• Using SUPER in Classes • Accessing Classes on the AppServer• Error Handling policy for Classes• Proper Encapsulation in Classes• Compiler Tips and Tricks• Coding Standards
© 2006 Progress Software Corporation4DEV-6 Advanced Object-oriented Programming in the ABL
Order Entry System Object Model
“Business Entity”Interface between
DA layer and Presentation /
Integration
“Data Access Object”
Interface to Data Store
Use OpenEdge® Reference Architecture
PresentationPresentation
Business ServicesBusiness Services
Data AccessData Access
Data SourcesData Sources
Co
mm
on
Infrastru
cture
Co
mm
on
Infrastru
cture
Enterprise ServicesEnterprise Services
© 2006 Progress Software Corporation5DEV-6 Advanced Object-oriented Programming in the ABL
Order Entry Object Model - Handout
Inherits
CLASS Acme.BusinessEntity
DEF VAR rDataObject AS CLASS IDataAccess
METHOD VOID fetchWhere ( )
METHOD VOID saveChanges ( )
CLASS Acme.BEOrder INHERITS Acme.BusinessEntity
METHOD VOID processOrder ( )
…
Inherits
CLASS Acme.BEInternalOrder INHERITS Acme.BEOrder
DEF VAR invoiceNum AS INT
METHOD VOID OVERRIDE processOrder ( )…
CLASS Acme.BEExternalOrder INHERITS Acme.BEOrder
DEF VAR crossDept AS INT
METHOD VOID OVERRIDE processOrder ( )…
Inherits
CLASS Acme.DAOrderSuper
PROTECTED TEMP-TABLE ttOlinePROTECTED TEMP-TABLE ttOrderPROTECTED DATASET dsOrder…
INTERFACE Acme.IDataAccess
METHOD LONGCHAR selectRecords (…)
ImplementsCLASS Acme.DAItem IMPLEMENTS Acme.IDataAccess, Acme.IList
…
CLASS Acme.DAOrderINHERITS Acme.DAOrderSuper IMPLEMENTS Acme.IDataAccess, Acme.IList
METHOD LONGCHAR selectRecords (…)METHOD NextRecord ( )METHOD PrevRecord ( )
METHOD VOID postOlineFill (…)
METHOD VOID postDSFill (…)
Implements
INTERFACE Acme.IList
METHOD nextRecord ( )
METHOD prevRecord ( )…
© 2006 Progress Software Corporation6DEV-6 Advanced Object-oriented Programming in the ABL
Order Entry Object Model – Data Access Layer
CLASS CLASS Acme.DAOrderSuperAcme.DAOrderSuper
CLASS CLASS Acme.DAItemAcme.DAItem
INTERFACE INTERFACE Acme.IListAcme.IList
ImplementsCLASS CLASS
Acme.DAOrderAcme.DAOrder
Inherits
INTERFACE INTERFACE Acme.IDataAccessAcme.IDataAccess
Implements
Data Access Interface
Navigation Interface
Super Order Class
Specific Order Class
Specific Item Class
PROTECTED TEMP-TABLE ttOlinePROTECTED TEMP-TABLE ttOrderPROTECTED DATASET dsOrder
METHOD LONGCHAR selectRecords (…)
METHOD LONGCHAR selectRecords (…)
METHOD LONGCHAR selectRecords (…)
Implements
© 2006 Progress Software Corporation7DEV-6 Advanced Object-oriented Programming in the ABL
Order Entry Object Model – Business Servicing Layer
CLASS CLASS Acme.BusinessEntityAcme.BusinessEntity
CLASS CLASS Acme.BEOrderAcme.BEOrder
CLASS CLASS Acme.BEInternalOrderAcme.BEInternalOrder
CLASS CLASS Acme.BEExternalOrderAcme.BEExternalOrder
Inherits
Inherits Inherits
Business Entity Class
Order Class
Specific Internal Order Class
Specific External Order Class
DEF VAR rDataObject AS CLASS Acme.IDataAccess
METHOD VOID fetchWhere ( )METHOD VOID saveChanges ( )
METHOD VOID processOrder( )
METHOD OVERRIDE VOID processOrder ( )
METHOD OVERRIDE VOID processOrder ( )
© 2006 Progress Software Corporation8DEV-6 Advanced Object-oriented Programming in the ABL
Physical Architecture of A Class Hierarchy
COMPILE Acme/BEExternalOrder.cls SAVE.
Inherits
Inherits
<propath> / Acme / <propath> / Acme / BusinessEntity.clsBusinessEntity.cls
<propath> / Acme /<propath> / Acme / BEOrder.clsBEOrder.cls
<propath> / Acme /<propath> / Acme / BEExternalOrder.clsBEExternalOrder.cls
© 2006 Progress Software Corporation9DEV-6 Advanced Object-oriented Programming in the ABL
COMPILE
COMPILE
COMPILE
Physical Architecture of A Class Hierarchy
COMPILE Acme/BEExternalOrder.cls SAVE.
Inherits
Inherits
<propath> / Acme / <propath> / Acme / BusinessEntity.clsBusinessEntity.cls
<propath> / Acme /<propath> / Acme / BEOrder.clsBEOrder.cls
<propath> / Acme /<propath> / Acme / BEExternalOrder.clsBEExternalOrder.cls
© 2006 Progress Software Corporation10DEV-6 Advanced Object-oriented Programming in the ABL
Physical Architecture of A Class Hierarchy
COMPILE Acme/BEExternalOrder.cls SAVE.
Inherits
Inherits
Digest: <val>
Inherits:<class-type, digest-val>
Implements:<class-type, digest-val><class-type, digest-val>…
Class r-code Data<propath> / Acme / <propath> / Acme / BusinessEntity.clsBusinessEntity.cls
<propath> / Acme /<propath> / Acme / BEOrder.clsBEOrder.cls
<propath> / Acme /<propath> / Acme / BEExternalOrder.clsBEExternalOrder.cls
<propath> / Acme / BEOrder.r
5678Acme/BusinessEntity, 1234None
<propath> / Acme / BusinessEntity.r
1234NoneNone
<propath> / Acme / BEExternalOrder.r
9000Acme/BEOrder, 5678None
© 2006 Progress Software Corporation11DEV-6 Advanced Object-oriented Programming in the ABL
Agenda
Order Entry System Object Model Abstract Programming with Classes Working with Datasets / Temp-tables in Classes Miscellaneous Topics:
• Debugging with Classes• Using SUPER in Classes • Accessing Classes on the AppServer• Error Handling policy for Classes• Proper Encapsulation in Classes• Compiler Tips and Tricks• Coding Standards
© 2006 Progress Software Corporation12DEV-6 Advanced Object-oriented Programming in the ABL
Abstract (Generic) Programming
Define super class / interface for abstraction• Define abstract methods
Use abstract object• Define object reference for super class / interface• Set object ref to NEW subclass object• Polymorphism causes method calls on object ref to
run method in subclass
Abstract code should be put in top most class• Supports abstraction and reuse
© 2006 Progress Software Corporation13DEV-6 Advanced Object-oriented Programming in the ABL
Choosing between Super Classes and Interfaces
“Both”• Define API (contract) for abstract programming• Support polymorphism
Super class• Contain common code for sharing / augmenting• Contain protected data members
Interfaces• Use when no common behavior exists• Use to emulate multiple inheritance
© 2006 Progress Software Corporation14DEV-6 Advanced Object-oriented Programming in the ABL
Abstract Programming Using Interfaces
Define an interface• Common methods put in interface
• Implementer must provide code for methods
INTERFACE INTERFACE Acme.IDataAccessAcme.IDataAccess
CLASS CLASS Acme.DAOrderAcme.DAOrder
CLASS CLASS Acme.DAItemAcme.DAItem
Implements
METHOD LONGCHAR selectRecords (…)
METHOD LONGCHAR selectRecords (…)
METHOD LONGCHAR selectRecords (…)
Interface
Implementer Implementer
© 2006 Progress Software Corporation15DEV-6 Advanced Object-oriented Programming in the ABL
Abstract Programming Using Super Classes
Define a super class• Common methods including code put in super class
• Optionally, subclass can override common code
CLASS CLASS Acme.BEOrderAcme.BEOrder
CLASS CLASS Acme.BEInternalOrderAcme.BEInternalOrder
CLASS CLASS Acme.BEExternalOrderAcme.BEExternalOrder
METHOD OVERRIDE VOID processOrder ( )
METHOD OVERRIDE VOID processOrder ( )
METHOD VOID processOrder ( )
Super Class
Subclass Subclass
© 2006 Progress Software Corporation16DEV-6 Advanced Object-oriented Programming in the ABL
Abstract Programming Sample
CLASS Acme.BusinessEntity: /* Define variable for interface type */ DEFINE PROTECTED VARIABLE rDataObject
AS CLASS Acme.IDataAccess NO-UNDO.
/* Generic method – works on any class that implements Acme.IDataAccess */ METHOD PUBLIC LONGCHAR fetchWhere (…): lcVar1 = rDataObject:selectRecords (…).…
CLASS Acme.BEOrder: CONSTRUCTOR BEOrder ( ): /* Create Order data object assign to super class reference*/ rDataObject = NEW Acme.DAOrder ( ). lcVar2 = rDataObject:fetchWhere (…).…
© 2006 Progress Software Corporation17DEV-6 Advanced Object-oriented Programming in the ABL
Demo of Abstract Programming
© 2006 Progress Software Corporation18DEV-6 Advanced Object-oriented Programming in the ABL
Multiple Inheritance Using Interfaces
Some older OO languages support multiple inheritance
Newer OO languages support multiple APIs using interfaces – class inheritance is single
CLASS CLASS Acme.DAOrderAcme.DAOrder
INTERFACE INTERFACE Acme.IDataAccessAcme.IDataAccess
INTERFACE INTERFACE Acme.IListAcme.IList
Implements
METHOD LONGCHAR selectRecords (…)
METHOD NextRecord ( )
METHOD PrevRecord ( )
METHOD LONGCHAR selectRecords (…)
METHOD NextRecord ( )
METHOD PrevRecord ( )
© 2006 Progress Software Corporation19DEV-6 Advanced Object-oriented Programming in the ABL
Agenda
Order Entry System Object Model Abstract Programming with Classes Working with Datasets / Temp-tables in Classes Miscellaneous Topics:
• Debugging with Classes• Using SUPER in Classes • Accessing Classes on the AppServer• Error Handling policy for Classes• Proper Encapsulation in Classes• Compiler Tips and Tricks• Coding Standards
© 2006 Progress Software Corporation20DEV-6 Advanced Object-oriented Programming in the ABL
Datasets and Temp-Tables in Classes
Defined in a class as either PROTECTED or PRIVATE – class data member
Passed as parameters to methods (like procs)
Cannot be returned from a method (like UDFs)
Temp-table columns can contain Progress.Lang.Object • Automatically upcast when put into column
• CAST column value to subclass before use
© 2006 Progress Software Corporation21DEV-6 Advanced Object-oriented Programming in the ABL
Using a Class from a Temp-Table Column
Use TYPE-OF function with CAST
DEFINE VAR rOrder AS Acme.BEOrder.
FOR EACH myTT:
/* See if column is an external order */ IF TYPE-OF (myTT.rObj, Acme.BEExternalOrder) THEN
rOrder = CAST (myTT.rObj, Acme.BEExternalOrder).
/* See if column is an internal order */
ELSE IF TYPE-OF (myTT.rObj, Acme.BEInternalOrder) THEN
rOrder = CAST (myTT.rObj, Acme.BEInternalOrder).
END.
© 2006 Progress Software Corporation22DEV-6 Advanced Object-oriented Programming in the ABL
Protected DataSets / Temp-Tables
PROTECTED access in subclass “automatic” – use normal syntax • Better than passing BY-REFERENCE or BIND,
defining as SHARED
• No need to include definition in subclass
CLASS CLASS Acme.DAOrderSuperAcme.DAOrderSuper
CLASS CLASS Acme.DAOrderAcme.DAOrder
Inherits
PROTECTED TEMP-TABLE ttOlinePROTECTED TEMP-TABLE ttOrderPROTECTED DATASET dsOrder
FOR EACH ttOrder:
DISPLAY ttOrder.CustomerName
© 2006 Progress Software Corporation23DEV-6 Advanced Object-oriented Programming in the ABL
ProDataSet Event Handlers
Callbacks can be methods as well as internal procedures
Protected datasets can have handlers in subclass
CLASS CLASS Acme. Acme.
DAOrderSuperDAOrderSuper
CLASS CLASS Acme.DAOrderAcme.DAOrder
Inherits
/* Set callback in subclass for dataset in super class */
hDSOrder = dsOrder:HANDLE.
hDSOrder:SET-CALLBACK (“AFTER-FILL”, “postDSFill”).…
METHOD VOID postDSFill (DATASET dsOrder).…
© 2006 Progress Software Corporation24DEV-6 Advanced Object-oriented Programming in the ABL
Demo of Datasets and Temp-Tables
© 2006 Progress Software Corporation25DEV-6 Advanced Object-oriented Programming in the ABL
Agenda
Order Entry System Object Model Abstract Programming with Classes Working with Datasets / Temp-tables in Classes Miscellaneous Topics:
• Debugging with Classes• Using SUPER in Classes • Accessing Classes on the AppServer• Error Handling policy for Classes• Proper Encapsulation in Classes• Compiler Tips and Tricks• Coding Standards
© 2006 Progress Software Corporation26DEV-6 Advanced Object-oriented Programming in the ABL
Debugging Classes
Debugger works with Classes Demos have been running classes in the
debugger• Steps into each constructor / destructor in
hierarchy• Shows polymorphism in action – subclass
method is stepped into Remember
• Open class file in debugger to see data members
© 2006 Progress Software Corporation27DEV-6 Advanced Object-oriented Programming in the ABL
Using SUPER in Classes
Use SUPER ( ) to call super class constructor• Only valid in constructor
• Must be first line if present
Use SUPER:method-name ( ) to call a method higher in the class hierarchy• Valid in constructor, destructor, methods
• Must specify method-name (unlike procedures)
© 2006 Progress Software Corporation28DEV-6 Advanced Object-oriented Programming in the ABL
AppServer
Running with Classes on an AppServer
Option 1: Classes on both client and server– Provide serialize and deserialize methods– Remote class access support coming
Option 2: Dispatch procedure on AppServer– Client interacts with dispatcher– Data passed in temp-table or dataset
Remote classes cannot be accessed from client
Client AppServerDataData
Client
Data
Dispatch.pRUN dispatch.p SET h.RUN getData (myTT) in h. Dataset /
Temp-tableData
DataDataData
Data
Data
Dataset / Temp-table
DataDataset /
Temp-table
© 2006 Progress Software Corporation29DEV-6 Advanced Object-oriented Programming in the ABL
Error Handling
RETURN ERROR not allowed in classes
Options:1. Use output parameter or return type
2. Use STOP mechanism
Improved error handling coming!
© 2006 Progress Software Corporation30DEV-6 Advanced Object-oriented Programming in the ABL
Options for Handling Errors in Constructor
1. Constructor handles error– Run DELETE THIS-OBJECT in constructor
Automatically runs destructors for any successful constructors in hierarchy
– Caller checks object reference for unknown
3. Use an output parameter to indicate error to caller– Caller must check for error and delete object
2. Compile/Deployment errors found during NEW– Mismatched parameters, super class not found– Automatically aborts new and cleans up– Caller checks object reference for unknown or
ERROR-STATUS:ERROR
© 2006 Progress Software Corporation31DEV-6 Advanced Object-oriented Programming in the ABL
Demo of Error Handling in Classes
© 2006 Progress Software Corporation32DEV-6 Advanced Object-oriented Programming in the ABL
Encapsulation
Good practice to keep data members protected or private
Provide “getter” and “setter” routines to access data members• Safe access
• Support read-only or write-only
PROPERTY class members coming!
© 2006 Progress Software Corporation33DEV-6 Advanced Object-oriented Programming in the ABL
Compiling Tips and Techniques
Strong-typing only works if you compile – running “compile-on-the-fly” catches all errors at runtime• Be safe – always rebuild/compile your code
Recompiling only super classes can result in runtime errors – always recompile subclasses that depend on super classes to be safe• Exceptions to this rule…
© 2006 Progress Software Corporation34DEV-6 Advanced Object-oriented Programming in the ABL
Call new method
Recompile
Add new method
Compile
Compiling Tips and Techniques
No recompile of subclasses required when:• Methods / data is added to
super class
• Only implementation changes, not signature
When subclass accesses new methods and data, must recompile
AA
BB EE
FF
KK
JJ
HH
GGDD
CC
II
© 2006 Progress Software Corporation35DEV-6 Advanced Object-oriented Programming in the ABL
Compiling Tips and Techniques
COMPILER: MULTI-COMPILE = TRUE• Uses cache to store classes
already compiled in session• “Cache” cleared when set to
FALSE• OE Architect uses this
option
Otherwise COMPILE builds full class hierarchy – no timestamp check
Total files compiled:
MULTI-COMPILE = FALSE: 25
MULTI-COMPILE = TRUE: 8
AA
BB
EE FF HHGG
DDCC
Build procedures (make.p)
© 2006 Progress Software Corporation36DEV-6 Advanced Object-oriented Programming in the ABL
Recommended Coding Standards
Always use packages in your type name: Acme.DAOrder• First node is your company name
Class and Interface names use CamelCase starting with uppercase letter• Interface names start with leading “I”: Acme.IList
Methods and data member use camelCase with lowercase first letter
Follow rules for encapsulation, abstraction, and delegation
© 2006 Progress Software Corporation37DEV-6 Advanced Object-oriented Programming in the ABL
In Summary
Object-oriented ABL supports standard OO design patterns – abstraction, encapsulation, inheritance, polymorphism
OO functionality still coming• Overloading
• Properties
• Error handling
• Remote Objects
• CLASS-GLOBAL (Static)
• Strongly-typed events
© 2006 Progress Software Corporation38DEV-6 Advanced Object-oriented Programming in the ABL
Related Sessions
DEV–1: Introduction to Object–Oriented Language Concepts and Programming in OpenEdge ABL
MOVE–11: Using Classes and Procedures in OpenEdge 10
© 2006 Progress Software Corporation39DEV-6 Advanced Object-oriented Programming in the ABL
Questions?
© 2006 Progress Software Corporation40DEV-6 Advanced Object-oriented Programming in the ABL
Thank you foryour time
© 2006 Progress Software Corporation41DEV-6 Advanced Object-oriented Programming in the ABL