Object Oriented PowerBuilder - PowerBuilder User Group
Download
Report
Transcript Object Oriented PowerBuilder - PowerBuilder User Group
Intoduction to Custom Classes in
PowerBuilder
(Non-Visual User Objects)
William B. Heys
Senior Consultant
7/21/2015
Copyright 2000, marchFIRST, Inc. All
rights reserved.
2
Introduction to Custom Classes
Explain how PowerBuilder is very Object-Oriented
Help you understand Object-Oriented Terminology
Demonstrate why OO is important
Page: 3
Copyright 2000, marchFIRST, Inc. All rights reserved.
Introduction to Custom Classes
Show how to separate your business rules from your
interface
Introduce you to PowerBuilder Custom Classes (NVOs)
Page: 4
Copyright 2000, marchFIRST, Inc. All rights reserved.
Stop me at any time for questions
Page: 5
Copyright 2000, marchFIRST, Inc. All rights reserved.
Object
A software package containing ...
Data (called attributes) and
Processing (called operations or services) done by or to the object
Page: 6
Copyright 2000, marchFIRST, Inc. All rights reserved.
Objects
Building blocks for Object-Oriented applications
Page: 7
Copyright 2000, marchFIRST, Inc. All rights reserved.
Class
A description of a group of identical objects
Template or blueprint
Page: 8
Copyright 2000, marchFIRST, Inc. All rights reserved.
Class
Defines the data contained in the object - implemented as
instance variables
Defines processing (methods) done by or to an object implemented as events or object functions
Page: 9
Copyright 2000, marchFIRST, Inc. All rights reserved.
Class
During PowerBuilder development, you are really defining
classes, not objects
Classes are stored in PowerBuilder Libraries (PBLs)
Page: 10
Copyright 2000, marchFIRST, Inc. All rights reserved.
Object
Objects are created from classes at runtime
An object is a specific instance of a class
Page: 11
Copyright 2000, marchFIRST, Inc. All rights reserved.
Object
There may be many objects (instances) of a particular type
(class)
All objects of a given class share the processing defined in
the class, but contain different values for their attributes
Page: 12
Copyright 2000, marchFIRST, Inc. All rights reserved.
Class and Objects
Page: 13
Copyright 2000, marchFIRST, Inc. All rights reserved.
Class and Objects
Round
Yellow
Fuzzy
Rubber
Hollow
Bounces
Page: 14
Copyright 2000, marchFIRST, Inc. All rights reserved.
Attributes
Data elements (variables) contained in an object
Defined once - in the class
name, data type, initial value
scope and access
Each object (class instance) has its own values for its
attributes
Page: 15
Copyright 2000, marchFIRST, Inc. All rights reserved.
Implementing Attributes
Use instance variables to define attributes in PowerBuilder
classes
Page: 16
Copyright 2000, marchFIRST, Inc. All rights reserved.
Methods
Implement the processing performed by or to an object
Defined in the class
Provide access to attributes (Get and Set)
Provide state transitions
Implement business rules
Do other processing
Page: 17
Copyright 2000, marchFIRST, Inc. All rights reserved.
Inheritance
Ability to create new classes from existing classes
Specialization, sub-classing, or sub-typing
For Example, Fruits and Types of Fruit
Page: 18
Copyright 2000, marchFIRST, Inc. All rights reserved.
Inheritance
The new class can utilize the methods and variables defined
in all classes above it.
Page: 19
Copyright 2000, marchFIRST, Inc. All rights reserved.
Encapsulation
Packaging data and related processing together
Page: 20
Copyright 2000, marchFIRST, Inc. All rights reserved.
Encapsulation
Information Hiding
Hiding the implementation details of an object
Keeping attributes private or protected
Page: 21
Copyright 2000, marchFIRST, Inc. All rights reserved.
Encapsulation
Provide public interface (methods) to access or change
attribute values
Page: 22
Copyright 2000, marchFIRST, Inc. All rights reserved.
Object Communication
Objects communicate with each other by sending messages
A client object can send a message calling a function in the
server object
Or by triggering an event in the server object
Page: 23
Copyright 2000, marchFIRST, Inc. All rights reserved.
Polymorphism
Ability to hide different implementations of a method behind
a common interface.
Different objects respond differently to the same message
Shifts responsibility of selecting correct method from
sender (client) to receiver (server) of message
Page: 24
Copyright 2000, marchFIRST, Inc. All rights reserved.
Polymorphism
Bounce!
Page: 25
Copyright 2000, marchFIRST, Inc. All rights reserved.
Polymorphism
Bounce!
Page: 26
Copyright 2000, marchFIRST, Inc. All rights reserved.
Polymorphism
Bounce!
Page: 27
Copyright 2000, marchFIRST, Inc. All rights reserved.
Polymorphism
Bounce!
Applesauce!
Page: 28
Copyright 2000, marchFIRST, Inc. All rights reserved.
Polymorphism
Bounce!
Applesauce!
Page: 29
Copyright 2000, marchFIRST, Inc. All rights reserved.
Polymorphism
Bounce!
Applesauce!
Page: 30
Copyright 2000, marchFIRST, Inc. All rights reserved.
Scope
The scope of a variable determines when the variable is
created and when it is destroyed
PowerBuilder has four variable scopes
Global
Local
Instance
Shared
Page: 31
Copyright 2000, marchFIRST, Inc. All rights reserved.
Scope - Global Variables
Exist for the entire life of the application.
Created when the application initially starts up and
destroyed when the application shuts down.
Page: 32
Copyright 2000, marchFIRST, Inc. All rights reserved.
Scope - Global Variables
Can be defined from the script painter within:
Application, Menu, Window, User Object, and Function painters
Regardless of where you define global variables, they are
stored in the PowerBuilder application object.
Page: 33
Copyright 2000, marchFIRST, Inc. All rights reserved.
Scope - Global Variables
Public Access -The disadvantage to global variables is that
they can be accessed by all scripts in all objects in the
application
Impact Analysis - To understand whether you can reference
or change the value of a global variable, you need to
examine all of the scripts contained in the entire application
Page: 34
Copyright 2000, marchFIRST, Inc. All rights reserved.
Scope - Global Variables
Avoid at all cost
Page: 35
Copyright 2000, marchFIRST, Inc. All rights reserved.
Scope - Local Variables
Defined inside a script.
Only exist while the script is executing.
Created just before the script begins executing, and are
destroyed as soon as the script finishes.
Really only temporary variables
Page: 36
Copyright 2000, marchFIRST, Inc. All rights reserved.
Scope - Instance Variables
Object-level variables
Exist only for the life of an object (class instance)
Created when the object is created, and destroyed when the
object is destroyed
Allow values to be shared between scripts in the same
object
Page: 37
Copyright 2000, marchFIRST, Inc. All rights reserved.
Scope - Shared Variables
Defined for a class
Shared variables exist for the duration of the application
Shared variables are created when the first instance of a
class (object type) is created, and are destroyed when the
application finishes
Page: 38
Copyright 2000, marchFIRST, Inc. All rights reserved.
Scope - Shared Variables
Shared variables allow values to be shared between
instances of the same type of object
Page: 39
Copyright 2000, marchFIRST, Inc. All rights reserved.
Access
PowerBuilder defines three levels of access
Public
Private
Protected
Access can be defined for instance variables and object
functions but not events
Page: 40
Copyright 2000, marchFIRST, Inc. All rights reserved.
Access - Public
Any script anywhere in the application can access a public
variable or function as long as it is in scope.
Global variables and global functions in PowerBuilder are
always Public
Events are always Public.
Page: 41
Copyright 2000, marchFIRST, Inc. All rights reserved.
Access - Private
Private variables and functions are accessible only by
scripts defined in the same object
Shared variables are always Private
Page: 42
Copyright 2000, marchFIRST, Inc. All rights reserved.
Access - Protected
Protected variables and functions are accessible only by
scripts defined in the same object or its descendants.
Page: 43
Copyright 2000, marchFIRST, Inc. All rights reserved.
Functions vs. Events
Everything changed in PowerBuilder 5
Page: 44
Copyright 2000, marchFIRST, Inc. All rights reserved.
Events and Functions
Can be triggered synchronously
Can be posted asynchronously
Can now pass named arguments
Can now define return value
Can be type checked (does it exist?) at compile time (static)
or run-time (dynamic)
Page: 45
Copyright 2000, marchFIRST, Inc. All rights reserved.
Events
Limited ability to pass arguments prior to
PowerBuilder 5 (TriggerEvent, PostEvent)
Message Object LongParm and WordParm
Could not return explicit values prior to
PowerBuilder 5
Always associated with an object
Are always public
Page: 46
Copyright 2000, marchFIRST, Inc. All rights reserved.
Events
Are easier to discover in the PowerBuilder script painter
Ancestor events can be easily extended or overriden
(compile option)
Cannot overload
Page: 47
Copyright 2000, marchFIRST, Inc. All rights reserved.
Functions
Could only be called synchronously prior to PowerBuilder 5
May be independent of any object (global function)
Can be private, public, or protected (encapsulation)
Page: 48
Copyright 2000, marchFIRST, Inc. All rights reserved.
Functions
Are more difficult to discover inherited functions
A descendant object does not list its ancestor’s functions
Can be overloaded (prior to
PowerBuilder 5 this required inheritance)
Can be polymorphic (prior to PowerBuilder 5 this required
inheritance)
Page: 49
Copyright 2000, marchFIRST, Inc. All rights reserved.
Building Good Objects - Goals:
Minimize the dependencies between objects (coupling)
How much one object knows about another object
How much one object can affect the behavior of another object
Page: 50
Copyright 2000, marchFIRST, Inc. All rights reserved.
Objects talk to each other
Objects communicate by passing messages (Public
Interface)
Clearly-defined, narrow interface
Explicitly pass only needed information between objects
Define simple single-purpose functions (high level of
cohesion)
Page: 51
Copyright 2000, marchFIRST, Inc. All rights reserved.
Visual Classes in PowerBuilder
Windows
w_frame (MDI frame)
w_base_sheet (ancestor sheet)
w_customer_list (application specific sheet)
Menus
m_main_menu
m_sheet_menu
Page: 52
Copyright 2000, marchFIRST, Inc. All rights reserved.
Visual Classes in PowerBuilder
Standard Visual User Objects
u_datawindow (reusable datawindow control)
u_cb_retrieve (reusable retrieve commandbutton)
Custom Visual User Objects
External Visual User Objects
Visual Basic Controls (VBXs)
Page: 53
Copyright 2000, marchFIRST, Inc. All rights reserved.
Standard Classes (Nonvisual)
Can now be inherited
Error
Message
Transaction (SQLCA)
Dynamic SQL Staging Area (SQLSA)
Dynamic SQL Description Area (SQLDA)
Page: 54
Copyright 2000, marchFIRST, Inc. All rights reserved.
Custom Classes (Nonvisual)
Page: 55
Copyright 2000, marchFIRST, Inc. All rights reserved.
Custom Classes (NVOs)
Separate processing logic from interface objects
Implement business rules (Business Objects)
Service Delegation
DataWindow controllers
Multiple Inheritance work-around
Page: 56
Copyright 2000, marchFIRST, Inc. All rights reserved.
Custom Class Example
A Customer Business Object
Page: 57
Copyright 2000, marchFIRST, Inc. All rights reserved.
Business Object Inheritance
Customer
Residential
Customer
Page: 58
Copyright 2000, marchFIRST, Inc. All rights reserved.
Commercial
Customer
Application Custom Class
Service Delegator
Overcomes limitation on inheriting application object
Replacement for Global variables
Application-wide scope vs. Global scope
Page: 59
Copyright 2000, marchFIRST, Inc. All rights reserved.
Object Manager
Knows about all objects created from custom classes
Does not create any objects, but may destroy them
Each object automatically registers itself to Object Manager
when constructed
Clean-up at application end
Avoiding orphans and memory leaks
Page: 60
Copyright 2000, marchFIRST, Inc. All rights reserved.
Object Manager
Each object has a primary key (optional)
And can return value of its primary key
Object Manager can find an object given class name and
primary key
Returns a reference to the object if found
Page: 61
Copyright 2000, marchFIRST, Inc. All rights reserved.
Datawindow Row Selection
Overcomes limitation on multiple inheritance
Different custom classes provide different behavior
Single Row Selection
Multiple Row Toggle
Range Selection
No highlighting
Page: 62
Copyright 2000, marchFIRST, Inc. All rights reserved.
Datawindow Row Selection
Related to a common ancestor selection object
Reduces size of standard DataWindow User Object
Create the one you need
Only load optional code if you need it
Page: 63
Copyright 2000, marchFIRST, Inc. All rights reserved.
Why is OO so important?
Productivity
Increase code reuse
Decrease unnecessary maintenance
Getting ready for the future
Distributed Objects
Improved performance and scalability
Page: 64
Copyright 2000, marchFIRST, Inc. All rights reserved.
To Learn More
Take the Powersoft course: Building Object-Oriented
PowerBuilder Applications
Download library file NVOHOW.ZIP from the NVO section of
PBForum on Compuserve
Try it yourself
Page: 65
Copyright 2000, marchFIRST, Inc. All rights reserved.
Special Edition Using PowerBuilder 6
Page: 66
Copyright 2000, marchFIRST, Inc. All rights reserved.
Special Edition Using PowerBuilder 6
ISBN: 0-7897-1437-x
Macmillan (Que) website (www.mcp.com)
Go to Site Search, Book Information and enter ISBN
Amazon.Com (www.amazon.com)
Search on author: Heys, William
SoftPro Bookstore (www.SoftPro.com)
Page: 67
Copyright 2000, marchFIRST, Inc. All rights reserved.
Books and Articles by Bill Heys
Special Edition Using PowerBuilder 5.0.
Co-author, published by Que
June 1996
“NVO’s How and Why”
Posted to Compuserve (Go PBForum)
NVO Library Section
Filename is NVOHOW.Zip
August 1995
Page: 68
Copyright 2000, marchFIRST, Inc. All rights reserved.
PowerBuilder Advisor Columns
“How the PowerBuilder 5.0 Foundation Class Libraries Implement a
Service-Based Architecture.”
November-December 1996
“New Language Features in PowerBuilder 5.0.”
September-October 1996
“Managing your PowerBuilder Objects.”
May-June 1996
“Encapsulating Your Objects”
March-April 1996
“Introduction to Custom Classes”
January-February 1996 (Premier Issue)
Page: 69
Copyright 2000, marchFIRST, Inc. All rights reserved.
About the Author
CPD Professional
Certified PowerBuilder Developer
Certified for PB 5.0
Since 1994
Advanced CPI
Certified Powersoft Instructor
Since 1992
Founded Boston PowerBuilder User
Group in 1992
Page: 70
Copyright 2000, marchFIRST, Inc. All rights reserved.
You can reach me...
William B. Heys
Senior Consultant
+1 (781) 203-3171 direct
+1 (617) 513-0296 cell
bill.heys @ marchFIRST.com
bheys @ attglobal.net
[email protected]
Page: 71
marchFIRST, Inc.
128 Corporate Center
70 Blanchard Rd., 2nd Flooor
Burlington, MA 01803
+1 (781) 203-3000 tel
+1 (781) 203-3050 fax
www.marchFIRST.com
Copyright 2000, marchFIRST, Inc. All rights reserved.
Thank You!
Page: 72
Copyright 2000, marchFIRST, Inc. All rights reserved.