Previous | Table of Contents | Next

Page 294

NEW TERM
Many objects can be built from a class, just as one set of blueprints can be used to build numerous houses. If you were writing code to process employee records, you would use the employee class to instantiate, in other words to construct, an employee object for each employee record.

NEW TERM
Objects consist of attributes and methods. An attribute can be anything you need to know about an object. Name, phone number, Social Security number, pay rate, and pay type are all good examples of attributes for an employee object. Attributes are implemented as variable declarations made within the object class definition.

NEW TERM
Methods are the functions and procedures used to perform functions related to the object. Like attributes, methods are implemented as functions and procedures in the object class definition. Anything you want to do to an object should be implemented as a method. If you want to compare two objects, you should implement a compare method. If you want to copy an object, you should implement a copy method. An employee object class, for example, might contain a method to calculate an employee's yearly bonus based on pay type, longevity with the firm, and so on.

Advantages over Traditional Methods

Objects offer the opportunity for increased reliability because of their well-defined interfaces. Reuse is made easier because all necessary code and data are part of the object definition; thus object classes can easily be added to programs as new functionality is required. Because you can model real-world, business objects, as well as encapsulate and hide the details behind an object's functionality, you can program at a higher level of abstraction, minimizing the amount of detail you need to remember, which makes your job as a developer much easier.

How Oracle Implements Objects

Oracle8 implements several new constructs in support of object-oriented programming:

Oracle also implements an object-relational database. The underpinnings are still relational, and that underlying relational model has simply been extended to include support for new datatypes, which in this case happen to be object types. By doing this, Oracle has maintained compatibility with existing relational databases and provided a path for gradual migration to objects.

Page 295

Object Types

NEW TERM
To use an object, first you need to define it. For this purpose Oracle8 introduces the object type. An object type is a database-level definition and is equivalent to the term class as used in object-oriented languages such as Java and C++. It contains both the code and data definitions for an object. Object types are also treated as datatypes and can be used in PL/SQL programs for declaring variables that will contain objects.

Object Tables

NEW TERM
Object tables are a special kind of table that you can create. These are based on an object definition and essentially map each attribute of an object to a column in the table.

Object Views

Object views are the object analog of a view on a table. A full discussion of object views is beyond the scope of this book, but your database administrator can use them to define pseudo-objects based on existing relational data. Like a relational view, object views are based on a SQL statement that retrieves the data for the object.

Defining an Object Type

You should now have a good overview of what object-oriented programming is and how Oracle handles objects. It's time to get down to some practical examples. To begin, define an object type for employee addresses. Listing 13.1 shows one possible implementation.

INPUT/
OUTPUT
Listing 13.1. Creating the address object type.

 1: CREATE OR REPLACE TYPE address AS OBJECT (
 2:   street_1      VARCHAR2(40),
 3:   street_2      VARCHAR2(40),
 4:   city          VARCHAR2(40),
 5:   state_abbr    VARCHAR2(2),
 6:   zip_code      VARCHAR2(5),
 7:   phone_number  VARCHAR2(10),
 8:   MEMBER PROCEDURE ChangeAddress (
 9:     st_1 IN VARCHAR2, st_2 IN VARCHAR2, cty IN VARCHAR2,
10:     state IN VARCHAR2, zip IN VARCHAR2),
11:   MEMBER FUNCTION getStreet (line_no IN number) RETURN VARCHAR2,
12:   MEMBER FUNCTION getCity RETURN VARCHAR2,
13:   MEMBER FUNCTION getStateAbbr RETURN VARCHAR2,
14:   MEMBER FUNCTION getPostalCode RETURN VARCHAR2,
15:   MEMBER FUNCTION getPhone RETURN VARCHAR2,
16:   MEMBER PROCEDURE setPhone (newPhone IN VARCHAR2)
17: );

Type created.
                                                            continues

Page 296

Listing 13.1. continued

 1: CREATE OR REPLACE TYPE BODY address AS
 2:   MEMBER PROCEDURE ChangeAddress (
 3:     st_1 IN VARCHAR2, st_2 IN VARCHAR2, cty IN VARCHAR2,
 4:     state IN VARCHAR2, zip IN VARCHAR2) IS
 5:   BEGIN
 6:     IF (st_1 IS NULL) OR (cty IS NULL) OR
                 Â(state IS NULL) OR (zip IS NULL)
 7:        OR (upper(state) NOT IN (`AK','AL','AR','AZ','CA','CO',
 8:                                 `CT','DC','DE','FL','GA','HI',
 9:                                 `IA','ID','IL','IN','KS','KY',
10:                                 `LA','MA','MD','ME','MI','MN',
11:                                 `MO','MS','MT','NC','ND','NE',
12:                                 `NH','NJ','NM','NV','NY','OH',
13:                                 `OK','OR','PA','RI','SC','SD',
14:                                 `TN','TX','UT','VA','VT','WA'
15:                                 `WI','WV','WY'))
16: OR (zip <> ltrim(to_char(to_number(zip),'09999'))) THEN
17:      RAISE_application_error(-20001,'The new Address is invalid.');
18:     ELSE
19:       street_1 := st_1;
20:       street_2 := st_2;
21:       city := cty;
22:       state_abbr := upper(state);
23:       zip_code := zip;
24:     END IF;
25:   END;
26:
27:   MEMBER FUNCTION getStreet (line_no IN number)
28:     RETURN VARCHAR2 IS
29:   BEGIN
30:     IF line_no = 1 THEN
31:       RETURN street_1;
32:     ELSIF line_no = 2 THEN
33:       RETURN street_2;
34:     ELSE
35:       RETURN ` `;    --send back a blank.
36:     END IF;
37:   END;
38:
39:   MEMBER FUNCTION getCity RETURN VARCHAR2 IS
40:   BEGIN
41:     RETURN city;
42:   END;
43:
44:   MEMBER FUNCTION getStateAbbr RETURN VARCHAR2 IS
45:   BEGIN
46:     RETURN state_abbr;
47:   END;
48:
49:   MEMBER FUNCTION getPostalCode RETURN VARCHAR2 IS
50:   BEGIN
51:     RETURN zip_code;
52:   END;
53:

Page 297

54:   MEMBER FUNCTION getPhone RETURN VARCHAR2 IS
55:   BEGIN
56:     RETURN phone_number;
57:   END;
58:
59:   MEMBER PROCEDURE setPhone (newPhone IN VARCHAR2) IS
60:   BEGIN
61:     phone_number := newPhone;
62:   END;
63: END;
64: /

Type body created.

ANALYSIS
Notice that the form of an object type declaration closely resembles that of a package definition. Like packages, object types have both a specification and a body. The specification, shown in lines 1 through 17 of the first segment, lists the object's attributes and member functions. The object body, lines 1 through 64 of the second segment, contains the actual code for the methods.

The statements in Listing 13.1 show how to define an object type, or class if you prefer that term. The complete syntax for an object type definition is shown following.

The Syntax for Defining an Object Type

CREATE TYPE type_name [IS | AS] OBJECT (
  attribute_name    datatype,
  attribute_name    datatype,
  ...
  MEMBER [function_specification | procedure_specification],
  MEMBER [function_specification | procedure_specification],
 ...
  [MAP | ORDER] MEMBER function_specification,
  pragma,
  pragma,
  ...
  );
CREATE TYPE BODY type_name [IS | AS]
  MEMBER [function_definition | procedure_definition];
  MEMBER [function_definition | procedure_definition];
 ...
  [MAP | ORDER] MEMBER function_definition;
END;

In this syntax, the parameters are as follows:

Previous | Table of Contents | Next

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