Spec in plsql package - why its required

2020-04-16 06:21发布

Can someone tell me, why we package Spec is required in oracle PL/SQL. anyway the package body has all the information as spec.

标签: oracle plsql
2条回答
贼婆χ
2楼-- · 2020-04-16 06:46

The package specification contains the definition or specification of all the publicly available elements in the package that may be referenced outside of the package. The specification is like one big declaration section; it does not contain any PL/SQL blocks or executable code. If a specification is well designed, a developer can learn from it everything necessary to use the package. There should never be any need to go “behind” the interface of the specification and look at the implementation,which is in the body.

The distinction drawn between public and private elements in a package gives PL/SQL developers unprecedented control over their data structures and programs. As Booch diagram below displays enter image description here Notice the two labels Inside and Outside. Outside consists of all the programs you write that are not a part of the package at hand (the external programs). Inside consists of the package body (the internals or implementation of the package).

Here are the conclusions we can draw from the Booch diagram:

  • External programs cannot cross the boundary from outside to inside. That is, an external program may not reference or call any elements defined inside the package body. They are private and invisible outside of the package.
  • Those elements defined in the package specification (labeled Public in the figure) straddle the boundary between inside and outside. These programs can be called by an external program (from the outside), can be called or referenced by a private program, and can, in turn, call or reference any other element in the package.
  • Public elements of the package therefore offer the only path to the inside of the package. In this way, the package specification acts as a control mechanism for the package as a whole.
  • If you find that a formerly private object (such as a module or a cursor) should instead be made public, simply add that object to the package specification and recompile. It will then be visible outside of the package.

This explanation is inspired by oracle pl/sql programming 5th edition by Steven Feuerstein

查看更多
▲ chillily
3楼-- · 2020-04-16 06:48

The separation of package Specification from the package Body is a fundamentally important part of the design of languages such as PL/SQL. It's the way that PL/SQL allows you to use the principle of Encapsulation.

The Specification is the way you specify the Public portion of the package, that is, the functions, procedures, types and constants that should be accessible by other program units or external callers.

The Body not only encapsulates the implementation details, but also allows you to create functions and procedures that are Private; that is, they are only allowed to be used by other functions and procedures in the same package, and cannot be called by external callers.

Encapsulation brings a number of benefits - including self-documentation: if a method is Private, you are guaranteed that there will be no calls to it except within the same package - so you are free to change it, secure in the knowledge that your change will at least not break any other system that uses the package.

Packages are useful as APIs; the specification then becomes the documentation of which functions and procedures are designed and intended to be called by other code in the system.

查看更多
登录 后发表回答