Previous | Table of Contents | Next

Page 298

Objects must contain at least one attribute, and can contain as many as a thousand. Member functions and procedures are entirely optional, as are compiler directives (pragmas). The definition of a MAP function or an ORDER function is also optional, but if present, only one type can be used. MAP and ORDER functions are discussed later in this chapter in the section titled "Comparing Objects."

As mentioned earlier, an object type is a database-level definition. After an object type is defined in the database, it can be used to create object tables, to define table columns that are themselves objects, or to declare object variables in PL/SQL blocks.

Constructor Methods

NEW TERM
Each Oracle object type has a built-in constructor method that is used to create an instance of that type. This method is responsible for initializing all the object's attributes and for doing whatever internal housekeeping is necessary. You do not have to declare or define this method, and in fact you cannot—Oracle does it for you.

NOTE
The inability to declare and define your own constructor methods represents a serious weakness in Oracle's current implementation of objects. Your flexibility is limited, and your control is limited as well because you have nowhere to write validation code, which can prevent an object from being created with an invalid combination of attribute values.

The constructor method always has the same name as the object type, and has as its arguments each of the object's attributes in the order in which you declared them. Thus the constructor method for the address object type would be

Page 299

FUNCTION address (street_1 in VARCHAR2, street_2 in VARCHAR2,
                       city in VARCHAR2, state_abbr, zip_code,
                            phone_number) returns address

The constructor function always returns an object of the same type. In your code, you would reference address as a function, passing values for each argument, in order to create an object of the address type, for example

address_variable := address(`101 Oak','','Detroit','MI',
     `48223','3135358886');

You will see more examples of this in the section titled "Instantiating and Using an Object." (See Listing 13.2.)

Accessor Methods

NEW TERM
The term accessor methods is used to describe methods that are used to return an object's attributes. The implementation of address shown in Listing 13.1 contains five accessor methods:

In most cases, these simply return the attribute in question. The getStreet method does a bit more in that it will return a blank if an invalid street address line is requested. By convention, accessor method names typically begin with get.

At first glance, it might seem silly to use a function like getStreet when you could just as easily reference the street_1 and street_2 attributes directly. However, accessor methods provide extra insulation between the underlying implementation of your objects and your programs. Consider the implications if, for whatever reason, you decided to remove the street_2 attribute from the address object. What impact would that have on existing programs? None if they are using getStreet. One small change to that function, and your programs wouldn't know the difference.

WARNING
Most object-oriented languages allow you to force the use of accessor functions by letting you define attributes as "private," meaning that they cannot be accessed directly. Oracle does not yet do this, so even though the accessor functions exist, there is no way to be 100 percent sure that they are always used.

Page 300

Mutator Methods

NEW TERM
A mutator method is the opposite of an accessor method. It lets you set attribute
values without referencing them directly. The advantages are the same as for accessor methods. Mutator methods simply provide an extra level of insulation between a program and an object's underlying implementation. By convention, mutator method names typically start with set.

The ChangeAddress method of your address object would be considered a mutator method. It could have been named setAddress to conform more closely to convention, but the name ChangeAddress was chosen because it is more descriptive of the real-world event for which this method exists.

Instantiating and Using an Object

After you have defined an object type, you probably want to do something with it. Using an object from within PL/SQL is not terribly difficult. You just have to follow these few simple steps:

  1. Declare one or more variables in which the datatype is the object type you want
    to use.
  2. Instantiate one or more of the objects.
  3. Use your object's member methods to manipulate the objects.
  4. Optionally, store your objects in the database.

This section discusses how to perform the first three of these four steps. There are two different approaches to storing objects, and those are discussed later in this chapter in the section titled "Storing and Retrieving Objects."

Listing 13.2 shows some fairly simple code that uses the address object defined earlier. Several variables of the address object type are declared, a few address objects are instantiated, their values are manipulated, and finally the objects' attributes are displayed for you to see.

INPUT/
OUTPUT
Listing 13.2. Using the address object.

 1: --A PL/SQL block demonstrating the use of the address object.
 2: SET SERVEROUTPUT ON
 3: DECLARE
 4:   address_1   address;
 5:   address_2   address;
 6:   address_3   address;
 7: BEGIN
 8:   --Instantiate a new address object named address_1,
 9:   --and assign a copy of it to address_2.
10:   address_1 := address (`2700 Peerless Road','Apt 1',

Previous | Table of Contents | Next

Используются технологии uCoz