PowerBuilder techniques

PowerBuilder provides full support for inheritance, encapsulation, and polymorphism in both visual and nonvisual objects.

NoteCreating reusable objects In most cases, the person developing reusable objects is not the same person using the objects in applications. This discussion describes defining and creating reusable objects. It does not address usage.

Implementing inheritance

PowerBuilder makes it easy to create descendent objects. You implement inheritance in PowerBuilder by using a painter to inherit from a specified ancestor object.

For examples of inheritance in visual objects, see the w_employee window and u_employee_object in the Code Examples sample application.

Example of ancestor service object One example of using inheritance in custom class user objects is creating an ancestor service object that performs basic services and several descendent service objects. These descendent objects perform specialized services, as well as having access to the ancestor’s services:

Figure 2-1: Ancestor service object

The ancestor service object u _ base _ service includes the u _ modify and u _ describe services and has two descendent objects. The descendent u _ selection _ service includes two services, and the descendent u _ print _ service also has two services.

Example of virtual function in ancestor object Another example of using inheritance in custom class user objects is creating an ancestor object containing functions for all platforms and then creating descendent objects that perform platform-specific functions. In this case, the ancestor object contains a virtual function (uf_change_dir in this example) so that developers can create descendent objects using the ancestor’s datatype.

Figure 2-2: Virtual function in ancestor object

The ancestor object u  platform has three functions, uf file read, uf file write, and uf change dir. Uf change dir is a virtual function that appears in the two descendent objects, u platform win and u platform unix, where it is defined to perform platform specific functions.

For more on virtual functions, see “Other techniques”.

Implementing encapsulation

Encapsulation allows you to insulate your object’s data, restricting access by declaring instance variables as private or protected. You then write object functions to provide selective access to the instance variables.

One approach One approach to encapsulating processing and data is as follows:

Another approach Another approach to encapsulating processing and data is to provide a single entry point, in which the developer specifies the action to be performed:

For an example, see the uo_sales_order user object in the Code Examples sample application.

NoteDistributed components When you generate an application server component, public functions are available in the interface of the generated component and you can choose to make public instance variables available. Private and protected functions and variables are never exposed in the interface of the generated component.

For more information, see Part 6, "Distributed Application Techniques."

Implementing polymorphism

Polymorphism refers to a programming language's ability to process objects differently depending on their datatype or class. Polymorphism means that functions with the same name behave differently depending on the referenced object. Although there is some discussion over an exact definition for polymorphism, many people find it helpful to think of it as follows:

Operational polymorphism Separate, unrelated objects define functions with the same name. Each function performs the appropriate processing for its object type:

Figure 2-4: Operational polymorphism

Two user objects, u _ em and u _ mle, each include a function called of _ Get Parent Window.

For an example, see the u_external_functions user object and its descendants in the Code Examples sample application.

Inclusional polymorphism Various objects in an inheritance chain define functions with the same name.

With inclusional polymorphism PowerBuilder determines which version of a function to execute, based on where the current object fits in the inheritance hierarchy. When the object is a descendant, PowerBuilder executes the descendent version of the function, overriding the ancestor version:

Figure 2-5: Inclusional polymorphism

At the top of the chain, the object u _ sort includes a function uf _ sort ( ). Farther down the chain, an object u _ sort _ dw also includes a function uf _ sort ( ) that overrides u _ sort.uf _ sort.

For an example, see the u_employee_object user object in the Code Examples sample application.