Object-oriented programming in the Progress® 4GL 10.1A Beta Salvador Viñals Consultant Product Manager svi@progress.com OpenEdge 10.1A1B Beta Agenda Introduction and goals of OO in the 4GL Concepts and overview An example Tools support for OO in the 4GL Beta use cases 2 © 2005 Progress Software Corporation Object-oriented Application Development A way to design and build applications – Objects bundle together data (state) and methods (behavior) – Objects facilitate separating definition from implementation Much more than just syntax – You might have already done object-oriented programming in the 4GL 3 © 2005 Progress Software Corporation Object-orientation and the 4GL Goals Extend the 4GL to support the OO programming model – Retain existing strengths A natural basis for OpenEdge Reference Architecture applications Programming with classes More closely support SOA Multiple rollouts – 10.1A first one 4 © 2005 Progress Software Corporation OO in the 4GL Features CLASS definitions Single inheritance hierarchies Data members and methods Interfaces 5 © 2005 Progress Software Corporation Benefits of OO in the 4GL Programming Business Encapsulation Maintenance Inheritance Promotes reuse Code reuse Productivity Interfaces Contracts Strong typing Robustness Invocation model Ease of use Maps modeling tools Efficient dev cycle .cls - .p interoperability Not disruptive 6 © 2005 Progress Software Corporation Poll Are you familiar with Object-oriented Programming concepts? – Type – Class – Data members and methods – Interface – Inheritance – Polymorphism – Objects <Please “click” Yes or No, using the menu-bar> 7 © 2005 Progress Software Corporation Poll How many using SUPER-PROCEDURES? <Please “click” Yes or No, using the menu-bar> 8 © 2005 Progress Software Corporation Similarities between Classes and Procedures Procedures Classes Procedure files (.p) Class files (.cls) Define variables Data members Internal procedures VOID methods User defined functions Other methods Code in main block Constructor Super-procedures Inheritance 9 © 2005 Progress Software Corporation Interoperability Procedures and Classes Procedures – – – – Can NEW a CLASS Can DELETE an object Invoke methods using object reference Can pass object reference as a parameter Classes – Can RUN a procedure – Can invoke internal procedure / udf using procedure handle 10 © 2005 Progress Software Corporation Agenda Introduction and goals of OO in the 4GL Concepts and overview An example Tools support for OO in the 4GL Beta use cases 11 © 2005 Progress Software Corporation Object-Oriented Concepts What do I need to understand before I learn OO4GL? OO terminology for familiar concepts – Classes, types, data members, methods & objects Encapsulation – Grouping data & behavior together Inheritance – Re-using and extending code Delegation – Letting contained objects do their own work Polymorphism – Generic code for objects with common data & behavior Interfaces – Implementing a standard set of methods 12 © 2005 Progress Software Corporation Types A Type is a definition Each class represents a new data type – Variables, parameters, return types Allows for strong-typing – Early binding - types determined at compile time – Type-consistency enforced at compile time and runtime 13 © 2005 Progress Software Corporation Types – An Example Types: Order Order is a InternalOrder is a – Sub-Type of Order ExternalOrder A Sub-Type can appear anywhere a Super-Type is expected 14 © 2005 Progress Software Corporation InternalOrder ExternalOrder – Sub-Type of Order Benefits of Types Strong Typing Compile time checking for type consistency myObj = mySubObject. myObj:method(…). myObj:data = 3. (must be Sub-Type) (validates signature) (validates data type) Results in safer, bug-free code because all code paths checked at compile time Develop super-classes first 15 © 2005 Progress Software Corporation Class A Class implements a Type A Class is a template (blueprint) for an object: – Data – Methods – Relationships to other classes 16 © 2005 Progress Software Corporation Data A Class defines and implements a user-defined type orderNum AS INT custNum AS INT CalculateTotalPrice( ) Methods Class: Order PUBLIC: CreateOrder( ) UpdateOrder( ) GetOrderTotal( ) Next( ) The CLASS Construct CLASS [<package>.]<class-name> [INHERITS <super-type-name> ] [IMPLEMENTS <interface-type-name> [,<interface-type-name>]…] [ FINAL ]: [ [ [ [ <data member> …] <constructor> ] <method> … ] <destructor> ] END [ CLASS ]. 17 © 2005 Progress Software Corporation The CLASS Construct CLASS [<package>.]<class-name> [INHERITS <super-type-name> ] • Use packages to [IMPLEMENTS <interface-type-name> group classes [,<interface-type-name>]…] • Package maps to [ FINAL ]: physical file relative to PROPATH [ <data member> …] [ <constructor> ] at compile time [ <method> … ] AND run time [ <destructor> ] • Period “.” maps to slash “/”, “\” in END [ CLASS ]. directories CLASS Acme.Inventory.Order: … 18 © 2005 Progress Software Corporation The CLASS Construct • Use inheritance to abstract out CLASS [<package>.]<class-name> common data and [INHERITS <super-type-name> ] [IMPLEMENTS <interface-type-name> functionality [,<interface-type-name>]…] • Sub-classes inherit [ FINAL ]: all data members & methods from their [ <data member> …] super-classes [ <constructor> ] • Sub-classes may [ <method> … ] extend or change [ <destructor> ] behavior by overriding END [ CLASS ]. methods of superclass 19 © 2005 Progress Software Corporation The CLASS Construct Classes are always PUBLIC CLASS [<package>.]< <class-name> [INHERITS <super-type-name> ] [IMPLEMENTS <interface-type-name> [,<interface-type-name>]…] [ FINAL ]: Encapsulation through class members: [ <data member> …] • Data [ <constructor> ] • Methods [ <method> … ] [ <destructor> ] Access types: END [ CLASS ]. 20 © 2005 Progress Software Corporation • PUBLIC • PRIVATE • PROTECTED The Class File Filename consists of package and class name – One class per file .cls extension Compiles to r-code 21 © 2005 Progress Software Corporation Creating Class Instances The NEW statement (equivalent to RUN) <obj-ref> = NEW <type-name> ([<parameter>[,…]]) [ NO-ERROR ]. Example DEFINE VARIABLE myOrder AS Acme.Inventory.Order NO-UNDO. myOrder = NEW Acme.Inventory.Order ( ). 22 © 2005 Progress Software Corporation Agenda Introduction and goals of OO in the 4GL Concepts and overview An example Tools support for OO in the 4GL Beta use cases 23 © 2005 Progress Software Corporation Object-orientation – An Example Application needs to manage two sets of orders for the same items: – Customer orders Need to check valid customer and credit-limit Charge Shipping and logistics – Internal orders Need to check valid department and cost center No charge Internal delivery – Inventory is common 24 © 2005 Progress Software Corporation Access Levels Accessible by super-class only Class Order DEF PRIVATE VAR OrderNum AS INT DEF PRIVATE VAR CustNum AS INT DEF PRIVATE VAR ShipDate AS DATE Private DEF PRIVATE VAR OrderStatus AS CHAR METHOD PROTECTED … calculatePrice() Protected Accessible by superclass & subclasses METHOD PUBLIC VOID calculateTax() METHOD PUBLIC VOID initOrder() METHOD PUBLIC VOID updateOrder() METHOD PUBLIC CHAR getCustomer() METHOD PUBLIC CHAR getShipDate() METHOD PUBLIC CHAR getStatus() METHOD PUBLIC DEC getOrderTotal() 25 © 2005 Progress Software Corporation Public Accessible by super-class, subclasses & other classes Inheritance Hierarchy Class Order InternalOrder Class DEF … OrderNum AS INT INHERITS Order DEF … CustNum AS INT DEF … ShipDate AS DATE Inherit from DEF … CostCenter AS INT DEF … Department AS INT DEF … OrderStatus AS CHAR METHOD … getCostCenter() METHOD … calculatePrice() METHOD … getDepartment() METHOD … calculateTax() METHOD … initOrder() ExternalOrder Class METHOD … updateOrder() INHERITS Order METHOD … getCustomer() METHOD … getShipDate() METHOD … getStatus() METHOD … getOrderTotal() 26 © 2005 Progress Software Corporation DEF … CustNum AS INT DEF … CreditLimit AS DEC METHOD … verifyCredit() METHOD … getCustNum() Polymorphism These objects of class types derived from Order may each behave differently when sent the InitOrder() message: myInternalOrder myExternalOrder1 myExternalOrder2 CostCenter = 9145 CustNum = 10 CustNum = 11 Department = 5 CreditLimit = 50000 CreditLimit = 300000 InitOrder InitOrder if isValid(getCostCenter() and If (isValid(getCustNum()) and getDepartment()) verifyCredit()) then call super-class’ initOrder() then call super-class’ initOrder() else call super-class’ rejectOrder() else call super-class’ rejectOrder() 27 © 2005 Progress Software Corporation Delegation Class GuiHandler FirstButtonPressed() Class Navigator LastButtonPressed() GuiHandler myGui NextButtonPressed() QueryHandler myQuery PrevButtonPressed() OkButtonPressed() CancelButtonPressed() initNavigator() First() Last() Next() Prev() Cancel() OK() Contains Class QueryHandler OpenQuery() CloseQuery() GetFirstRec() GetLastRec() GetNextRec() GetPrevRec() 28 © 2005 Progress Software Corporation Agenda Introduction and goals of OO in the 4GL Concepts and overview An example Tools support for OO in the 4GL Beta use cases 29 © 2005 Progress Software Corporation Tools Support For OO in the 4GL OpenEdge Architect – Code editors – Tools for Business Logic Not in this beta Business Entity Designer OpenEdge Studio (AppBuilder) 4GL Development System (Procedure Editor) Application Compiler Debugger – Compile Debug requires stub 30 © 2005 Progress Software Corporation Rules For .cls Files All editing environments Must have .cls extension to compile – Added to file filters File and package name in class file must match physical location CLASS payroll.taxcalc: … END CLASS. Must save file to run 31 © 2005 Progress Software Corporation AppBuilder Support Procedure Window No AppBuilder changes to support class files Opened as unstructured procedures in Procedure Window AppBuilder Procedure Window – Same support as Procedure Editor Procedure Window supports remote development – Web-disp.p cannot instantiate class files 32 © 2005 Progress Software Corporation Procedure Editor Support File filters New “*.cls” filter in GUI – File open – Save – Save As Source file types – All Source (*.p, *.w, *.i, *.cls) – Classes (*.cls) TTY has no filters 33 © 2005 Progress Software Corporation Syntax Checking Checking syntax of named edit buffers – Create temp directory(s) – Contains class file of correct name Checking syntax of unnamed edit buffers – Saves to temp file of .cls extension – In current working directory An inherited super-class must be in PROPATH for sub-classes to syntax check 34 © 2005 Progress Software Corporation Application Compiler Includes ‘.cls’ extension May compile super-classes “Remove Old .r Files” – Super-classes .r is not removed when compiling sub-classes “Only Compile if No .r File” 36 © 2005 Progress Software Corporation Agenda Introduction and goals of OO in the 4GL Concepts and overview An example Tools support for OO in the 4GL Beta use cases 37 © 2005 Progress Software Corporation Current Beta Use Cases (1/4) Use Case ID: Use Case Name: Description Actors Assumptions Steps Expected results or end state 101A4GL500 OO4GL new design/development Apply Object-Oriented design principles to new 4GL application 4GL architect/designer/developer Knowledge of OO concepts 1. Identify a new (not yet designed/implemented) 4GL application or new component of an existing 4GL application 2. Create an object-oriented design for 4GL application: a. define architecture b. analyze components c. determine data model d. determine object model e. define class hierarchies 3. Use new 4GL programming constructs to create user-defined classes for the design Verify that the 4GL constructs provided are sufficient to implement your OO design. 38 © 2005 Progress Software Corporation Current Beta Use Cases (2/4) Use Case ID: Use Case Name: Description Actors Assumptions Steps Expected results or end state 101A4GL501 OO4GL existing OO model Transform existing ‘OO’ 4GL app to use new OO4GL constructs 4GL architect/designer/developer Knowledge of OO concepts 1. Identify an existing 4GL application (or component) that employs OO concepts, implemented with existing 4GL constructs, such as SUPER Procedures. 2. Use new 4GL programming constructs to implement the existing ‘OO’ design Verify that the new 4GL constructs are sufficient to provide the same functionality as the existing 4GL features. 39 © 2005 Progress Software Corporation Current Beta Use Cases (3/4) Use Case ID: Use Case Name: Description Actors Assumptions Steps Expected results or end state 101A4GL502 OO4GL implementation through user-defined classes Exercise the new OO4GL constructs 4GL architect/designer/developer Knowledge of OO concepts 1. Start with an OO-designed 4GL application based on the new 4GL features (see Use Case “101A4GL500 and 101A4GL501”) 2. Write 4GL code using the new language constructs that implement: a. inheritance b. overriding c. polymorphism d. public/private/protected methods e. public/private/protected data members i. temptables ii. buffers iii. queries iv. built-in datatypes f. methods with VOID and non-VOID return values g. constructors and destructors Verify that the new 4GL features work according to the specified behavior. 40 © 2005 Progress Software Corporation Current Beta Use Cases (4/4) Use Case ID: Use Case Name: Description Actors Assumptions Steps Expected results or end state 101A4GL503 OO4GL use of User Defined Classes Use new OO4GL User-defined Classes as data types 4GL architect/designer/developer Knowledge of OO concepts 1. Implement a new 4GL application (or component) using the new 4GL constructs (see Use Case “101A4GL502”) 2. Instantiate your user-defined classes, and use them as you would builtin datatypes: a. Pass as parameters b. Pass as return values c. Use as temptable fields d. Delete them Verify that the user-defined classes’ methods and data members are accessible and behave as expected throughout these steps. 41 © 2005 Progress Software Corporation In addition, you shall … Validate interoperability Gather information about increase in development / maintenance productivity "Benchmark" runtime performance through testing of your user-defined classes Share your findings – Multiple ways to design, organize any given functionality 42 © 2005 Progress Software Corporation In Summary Standard OO concepts available in the 4GL Built on top of existing 4GL constructs Interoperability with Procedure / Functions – Can be combined with procedural programming, not all or nothing Many benefits in OO Programming Major difference of OO: Enforcement 43 © 2005 Progress Software Corporation Questions? 44 © 2005 Progress Software Corporation Thank you for your time! 45 © 2005 Progress Software Corporation 46 © 2005 Progress Software Corporation