720 likes | 1.16k Views
IA353 Using Custom Class Objects in PowerBuilder. Fred Grau Systems Architect - VIPS fgrau@chesint.net. Skill level: Beginning to intermediate Agenda: -OOP Basics -NVO Basics -NVO Usage -Service Based Architecture (SBA). Using Custom Class Objects in PowerBuilder.
E N D
IA353Using Custom Class Objects in PowerBuilder Fred Grau Systems Architect - VIPS fgrau@chesint.net
Skill level: Beginning to intermediate Agenda:-OOP Basics-NVO Basics-NVO Usage-Service Based Architecture (SBA) Using Custom Class Objects in PowerBuilder
Certified PowerBuilder Developer Former Consultant - Woodfield Technical Services, Inc. Current Position: Systems Architect - VIPS E-mail: fgrau@chesint.net Shameless Pitch
If you want to leverage your PowerBuilder code for the Internet, you must use Custom Class Objects (Also called Non-Visual Objects or NVOs). If you want to use a good framework (including the PFC), you must learn how to use NVOs. IMPORTANT!!!
Purpose: To maintain code in one place. Avoid code duplication OOP Basics
Object - A combination of data and behaviors. Object consists of:-Attributes: What do I know about?-Functions: What things can I do?-Events: What can happen to me? Objects
Pre-defined attributes:-window.title-window.height-sle_1.text User-defined attributes-customer.name-n_cst_appmanager.is_userid Attributes
Declare methods or functions to access and manipulate attributes. Examples:-w_employee.hide()-w_employee.wf_calculate_balance()-dw_1.reset() Functions
Predefined mapped - Directly mapped to the operating system. Example:Window::Open Predefined not mapped - Predefined in PB, but not mapped to an object. Example:sle_1::ue_changed (mapped to pbm_enchange) User defined mapped to pbm_customXX - PB 4.0 and before. Example: ue_save (mapped to pbm_custom01) User defined not mapped to any MessageID - Programmer has control over the parameters and return types. Example: ue_save (unmapped) Events
An abstraction of an object. The definition of an object. In PowerBuilder, classes are stored in PBLs. All windows created are in the same class of Window. Classes
An object never exists until runtime. At runtime, a class is instantiated into an object. Example: A window is opened. Note: The same object can be instatiated more than once. Instantiation
One class gets its definition from another class. A real-time template - When the parent (ancestor) object changes, so does the descendant. Eliminates the copy-paste method of code reuse. Inheritance
Any Root of all objects and data typesPowerObject Remenber Message.PowerObjectParm? NonVisualObject Transaction, Message GraphicObject Menu Window WindowObject DragObject Datawindow, Commandbutton, ListBox, etc. DrawObject Rectangle, line, etc. PowerBuilder Uses Inheritance
When you click ‘New’, you are inheriting from a PowerBuilder system class. Why not inherit from your own customized class instead? Inheriting From System Classes
Attributes Events Functions What Gets Inherited
Events: Extending scripts-This is the default behavior of an event script.-When an event is triggered (i.e. a window’s open event), the ancestor’s script is executed before the descendant’s script. How Does Inheritance Work
w_ancestor::openABC w_desc1::openDEF w_desc2::openGHI Statements executed: ABC DEF GHI Inherited Event Flow - Open(w_desc2)
w_ancestor::openABC w_desc1::openCALL w_ancestor::openDEF w_desc2::openCALL w_desc1::openGHI How PowerBuilder Extends a Script An implicit Call to the ancestor is automatically coded in each extended descendant.
Enabled on an event-by-event basis by setting the Design/Override Ancestor Script menu option in the descendant’s event script. When an event occurs, only the descendant’s script is executed. If necessary, you can still call an ancestor script by using the CALL <ancestor_name or SUPER>::Event <event_name> syntax. Overriding Scripts
w_ancestor::openABC w_desc1::openCALL w_ancestor::openDEF w_desc2::open [OVERRIDDEN]GHI Overriding Events Statements executed: GHI
w_ancestor::openABC w_desc1::open [OVERRIDDEN] DEF w_desc2::open CALL w_desc1::openGHI Overriding Events - Continued Statements executed: DEF GHI
The ancestor and descendant object each have a function with the same name and same argument list. If a descendant script calls the function name, the descendant’s function gets executed and the ancestor’s function is ignored. How does Inheritance Work for Functions?
w_ancestor.wf_calc()ABC w_desc1.wf_calc()DEF w_desc2.wf_calc()GHI Overriding Functions - Call wf_calc() in w_desc2 Statements executed: GHI
w_ancestor.wf_calc()ABC w_desc1.wf_calc()DEF w_desc2.wf_calc() does not exist Overriding Functions - Call wf_calc() in w_desc2 Statements executed: DEF
Ancestor and descendant object each have a function with the same name but a different argument list. If a descendant script calls a function, the function with the same argument list gets executed and any other functions with the same name are ignored. You can overload a function without using inheritance. Overloading Functions
MessageBox-MessageBox("Greeting", "Hello User")-MessageBox (“Delete”, “Delete Error”, Exclamation!)-MessageBox (“Delete”, “Are you deleting?”, Exclamation!, YesNoCancel!, 2) PowerBuilder Example of Function Overloading
Provides the ability to hide different implementations of a method behind a common interface. The same function call performed on different objects produces different results. Shifts responsibility of correct functionality from the sender (client) to the receiver (server). Polymorphism
Example #1inv_sybase.of_get_return_code()inv_oracle.of_get_return_code()inv_informix.of_get_return_code() Example #2basketball.bounce()football.bounce()bowlingball.bounce() Polymorphism Examples
Objects communicate to each other by passing messages (via a public interface).-You any pass the information the object needs. This allows you to develop single-purpose methods. In PowerBuilder you can call functions or trigger events. How Objects Talk To Each Other
By Value – You pass a copy of the value to the method. By Reference – You pass a reference (a pointer or memory location) to the method. By ReadOnly – You pass a reference, but it cannot change. Ways to Pass Arguments to a Method
When passing a simple data type (i.e. String, Int, Boolean) by value, you are passing a copy of the value. When passing a complex data type (i.e. DataWindow, Window, User Object) it is automatically passed by reference. Remember
PowerBuilder calls them Custom Class User Objects. Non-Visual Objects
Once defined, you can add:1. Functions2. Events3. Attributes (e.g. Instance Variables) They exist only in memory. Non-Visual Objects
Window: w_logon lw_logon NVO: n_cst_count lnv_count Declaring an object merely creates a reference variable (pointer). This pointer is initialized to null (invalid). It hasn’t been created in memory (instantiated). The compiler uses this declaration to validate the existence of its methods and attributes. Instantiating Objects: DECLARE
Window: Open (lw_logon) NVO: lnv_count = CREATE n_cst_count Instantiating an object tells PowerBuilder to create an object in memory and make the reference variable point to the object. NOTE: You can create the declared object or a descendant of the declared object. Examples: lnv_count = CREATE n_cst_count_win16 lnv_count = CREATE n_cst_count_win32 Instantiating Objects: Instantiate
OPEN is for visual objects-If the reference variable points to an instantiated object (IsValid) then a new object is NOT created.-OPEN returns a -1 if the object cannot be instantiated. CREATE is for Non-visual objects.-CREATE always creates a new object.-If the reference variable points to an instantiated object, then the old object is orphaned. Differences Between OPEN and CREATE
Can now CREATE objects dynamically without hard coding the object names. Example: n_cst_deptxxx are descendants of n_cst_dept.n_cst_dept lnv_deptString ls_dept_nvoInteger li_dept_num…ls_dept_nvo = ‘n_cst_dept’ + String(li_dept_num)lnv_dept = CREATE USING ls_dept_nvo If the classname referenced in CREATE USING is not found, a runtime error is generated. CREATE USING
Window: lw_logon.Hide() NVO: lnv_count.of_get_count() Can use an object’s: Attributes Events Functions Instantiating Objects: USE
Window: Close (lw_logon) NVO: Destroy lnv_count Destroying an object removes it from memory and frees up resources. The variable will still exist and is set to null (invalid). Not destroying NVOs can cause memory leaks. PowerBuilder 6.0+ has automatic garbage collection which will reduce memory leaks. Instantiating Objects: DESTROY
Allows you to change the implementation of an object without breaking other parts of the system. One object should not directly access the attributes of another object. Only public methods (functions or events) can access these attributes. To prevent direct manipulation, declare your instance variables as private or protected. Create “Get” and “Set” functions to access the attributes. Examples:gnv_app.of_SetUserID (ls_userid) ls_userid = gnv_app.of_GetUserID() Encapsulate Your NVO
Public (Default): Open to all other objects. Protected: Open to the current object and its descendants. Private: Open to the current object only. ProtectedRead: Only the current object and its descendants can read this attribute. ProtectedWrite: Only the current object and its descendants can write to this attribute. PrivateRead: Only the current object can read this attribute. PrivateWrite: Only the current object can write to this attribute. Access Modifiers
NVOs can now be set to AutoInstantiate when the object is declared. Object is automatically destroyed when it goes out of scope. To AutoInstantiate:-Right-click on the user object when it is defined.-Select the AutoInstantiate menu item CREATE causes a compile error.CREATE USING causes a runtime error. AutoInstantiation of User Objects
Assume n_cst_employee is AutoInstantiatedn_cst_employee inv_emp // Instance or Local VariableInteger li_rcString ls_ssn…li_rc = inv_emp.of_edit_ssn(ls_ssn) Object is destroyed when it goes out of scope. AutoInstantiated NVO Example
Even with AutoInstantiation and PowerBuilder’s garbage collection, still use the CREATE and DESTROY. AutoInstantiation is very useful when using NVOs as structures. Use AutoInstantiation when an object does not reference any other objects. Using AutoInstantiated NVOs
Hold “Global” variables. Storage for constants. Business rules. EAServer and the Web. Add GUI functionality. Database independence. Placeholder for external DLL functions. Some Uses for NVOs
Lets you easily transport “globals” to new applications.No need to duplicate globals in each application. Allows for the easy add or change of new attributes. Lets you protect against invalid changes of attributes through encapsulation. NVO Uses: Hold “Global” variables
Return values should be constants. Remember: Constants do not use storage during runtime. Any changes to a constant’s value only has to be made in one place. After making the change, just simply regenerate the necessary objects. To use:-Create an NVO that holds nothing but constants (e.g. n_cst_constants)-Create a global variable: n_cst_constants gc-Reference constants using dot notation: IF SQLCA.SQLCode = gc.NOT_FOUND Then … NVO Uses: Storage for Constants
Place as many edits and updates as possible in NVOs. Business rules NVOs then become reusable in your application. They also become reusable in future applications. NVO Uses: Business Rules
NVOs can be moved to an application server (EAServer). Web pages can then be incorporated to have HTML interface with NVOs running on an application server. NVO Uses: EAServer and the Web