Name

PKG-01: Group related data structures and functionality together in a single package.

Synopsis

Even if you don’t take advantage of features unique to packages, such as persistent data, you can still use packages to organize—and store together—related code and data structures.

Without packages, you might end up with several hundred standalone procedures and functions and many repeated cursor, TYPE, and variable declarations.

A package gives a name to a set of program elements: procedures, functions, user-defined types, variable and constant declarations, cursors, and so on. By creating a package for each distinct area of functionality, you create intuitive containers for that functionality.

Example

As I go about building a library management system, I quickly identify the following functional areas:

Borrower information

Maintain underlying table, establish rules about who is a valid borrower.

Book information

Maintain underlying table, define validation for ISBN, and so on.

Borrowing history

Maintain underlying table that tracks who took out what and when.

Overdue fines

Collection of all formulas and business rules for processing overdue charges.

Special reservations

Maintain underlying table and collect all rules governing how a borrower can reserve a book.

Publisher information

Maintain underlying table.

I can now create separate packages for each bunch of data or functional specification. For example, the overdue package would contain all programs related to calculating and displaying overdue fine information. If I need any TYPEs (collections and records, for example) to declare or manipulate overdue data, I would also define those in the overdue package.

Benefits

It’s much easier to build, find, and maintain programs when they are organized by logical area into separate packages.

Once you have segregated programs and data into their own packages, you can more easily leverage special features of packages (persistent data, initialization section, overloading) to improve performance and functionality.

Resources

  1. te_employee.pks and te_employee.pkb : Table encapsulation packages feature “high cohesion” (grouping together of related programs). Such packages offer a set of procedures and functions that allow a developer to manipulate the underlying data structure (table or view) without writing any explicit SQL.

  2. xfile.pkg : The xfile class (built on top of the JFile Java class) offers “one stop shopping” for all file-related processing in a PL/SQL environment.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset