home *** CD-ROM | disk | FTP | other *** search
/ Solo Programadores 22 / SOLO_22.iso / docs / lovelace / lesson15.les < prev    next >
Encoding:
Text File  |  1995-11-21  |  7.4 KB  |  178 lines

  1. <COMMENT This is a lesson file for the Lovelace Ada tutorial>
  2. <COMMENT A program called genlesson is used to transform this file into a set>
  3. <COMMENT of useful HTML files for use by Mosaic & other WWW browsers.>
  4.  
  5. <COMMENT  Edit the following lines. >
  6. <TUTOR NAME="Lovelace">
  7. <LESSON NUMBER=15>
  8. <AUTHOR NAME="David A. Wheeler" EMAIL="wheeler@ida.org">
  9. <AUTHOR ADDRESS="<A HREF="dwheeler.htm">David A. Wheeler (wheeler@ida.org)</A>">
  10. <COMMENT $Id$ >
  11.  
  12. <COMMENT  You'll probably want to uncomment and edit these lines: >
  13. <COMMENT  <PREVIOUS_LESSON LOCATION="URL_of_directory/" >
  14. <COMMENT  <NEXT_LESSON LOCATION="URL_of_directory/" >
  15.  
  16. <COMMENT A lesson is divided into 1 or more "sections".>
  17. <COMMENT Each section has a title; SECTION starts a new section.>
  18.  
  19. <SECTION NAME="Ada Program Structure">
  20. Because this is a tutorial I've used very short programs to
  21. demonstrate specific points.
  22. Real Ada programs are developed instead as a set of (many) Ada packages.
  23. Each package has a declaration that publicly declares the facilities
  24. it makes available, and each package has a body to implement that public
  25. declaration.
  26. <P>
  27. Packages are the principle structuring facility in Ada.
  28. Packages are intended to be used to directly support abstraction,
  29. information hiding, and modularization.
  30. For example, packages are useful for encapsulating machine dependencies.
  31. <COMMENT This is a quote from AQ&S chapter 4 >
  32. <P>
  33. It isn't easy to describe program structuring issues, because that's
  34. really a program design question, not just a language question.
  35. Some recommendations are given in
  36. <A HREF="ftp://sw-eng.falls-church.va.us/public/AdaIC/docs/style-guide/95style/ps/struct.ps">Chapter 4 ("Program Structure") of
  37. <EM>Ada Quality and Style: Guidelines for Professional Programmers.</EM></A>
  38. Here are some of those guidelines:
  39. <P>
  40. <UL>
  41. <LI>
  42. Packages should serve a single purpose.
  43. You should avoid creating packages that are
  44. simply collections of unrelated objects and subprograms.
  45. <LI>
  46. You should use a package to group together closely related types.
  47. For example, you might have a
  48. "vector" type (a one-dimensional list of numbers)
  49. and "matrix" type (a two-dimensional list of numbers).
  50. In this case, you could have a single package
  51. called <EM>Matrix_Manipulation</EM> with both types defined in it.
  52. <LI>
  53. Put only what is needed for the use of a package into its
  54. specification; hide the implementation details from the users.
  55. <LI>
  56. In general,
  57. avoid defining a variable in a package specification, since that
  58. creates a global variable visible to anyone who <EM>with</EM>s the package.
  59. <LI>
  60. Minimize the number of declarations in a package specification.
  61. <LI>
  62. Only use <EM>with</EM> clauses where they are needed.
  63. In particular, if you can move a <EM>with</EM> clause so that it's used by
  64. the package body instead of the package specification,
  65. you should do so.
  66. </UL>
  67. <P>
  68.  
  69. Note that Ada is different from some other languages.
  70. Ada separates the concept of <EM>type</EM> from
  71. the concept of <EM>module</EM> (package); some other languages
  72. merge the two concepts (particularly Eiffel, and to a lessor degree C++).
  73. Each approach has its advantages and disadvantages, which we
  74. won't delve into here.
  75. <P>
  76.  
  77.  
  78. <SECTION NAME="Child Packages">
  79. Ada 95 has added a new capability called a "Child Package."
  80. A child package is a package that is conceptually part of its parent
  81. package, but a child package can be compiled
  82. separately (after its parent has been compiled)
  83. without recompiling or modifying the parent.
  84. This can reduce recompilation time and it provides another mechanism for
  85. breaking a complicated system into components.
  86. <P>
  87. Child packages are especially useful if you define
  88. a private tagged type in a package.
  89. A child package can "see" the definition of a private tagged type,
  90. because conceptually the child package is part of the parent package.
  91. <P>
  92. We'll go over child packages briefly here, but feel free to skim this section
  93. and return later if you like.
  94. <P>
  95. To understand how child packages work, let's first repeat the fundamentals.
  96. Any package can have a declaration (specification) and a body, and the
  97. package declaration can have both a public part and a private part.
  98. Normally the private part of the package declaration can only be seen by
  99. the body of package X;
  100. anyone who "with"s package X may only see the public part.
  101. <P>
  102. A child package adds a few twists. First,
  103. a child package is considered to have "automatically with'ed" its parents.
  104. In the public part of a child package you can only see the public part of your
  105. ancestors, just as though the package had the statement
  106. "with Parent" at the top.
  107. The real difference, though, is what happens in the child package
  108. declaration (specification) private part and body - in those places,
  109. the <EM>private</EM> part of the child packages' ancestors
  110. are made visible.
  111. That's an important difference - normally packages do <EM>not</EM>
  112. have visibility to the private parts of other packages, because
  113. conceptually the child package is part of the parent's package.
  114. <P>
  115. Let's go through an example to make this clearer.
  116. Let's say you have a package named "X".
  117. Package X can a declaration (specification) and a body, and package X's
  118. declaration can have a public part and a private part.
  119. Normally only package X's body can see the private part of the package
  120. declaration; anyone who "with"s package X may only use the public part.
  121. <P>
  122. <IMG ALIGN=right SRC="childp.gif" ALT="[Child Package]">
  123. Now, let's define a child package of X, called "X.Y".
  124. Package X.Y's public part can see the public part of X automatically, and
  125. the private part of X.Y and the body of X.Y can automatically see the
  126. private part of package X.
  127. This isn't considered a violation of Ada's safety rules,
  128. because X.Y is considered to be part of X.
  129. <P>
  130. The syntax for declaring a child package is the same as ordinary packages,
  131. you just name the child package using dotted notation:
  132. <P>
  133. <PRE>
  134.   package X.Y is ....
  135.   end X.Y;
  136. </PRE>
  137. <P>
  138. <IMG ALIGN=right SRC="pchildp.gif" ALT="[Private Child Package]">
  139. You can also have <EM>private</EM> children (the normal kind of children
  140. are called <EM>public</EM> children).
  141. Private children are intended for "internal" packages that should only
  142. be "with'ed" by a restricted number of packages.
  143. Private children can only be "with'ed" by the body of its parent or by
  144. descendents of the private child's parent.
  145. In exchange for such a restrictive requirement, a
  146. private child gets a new authority:
  147. a private child's specification automatically sees both the public and
  148. private parts of all of its ancestors' specifications.
  149. To declare a private package, just append the word "private":
  150. <P>
  151. <PRE>
  152.   private package X.Z is ....
  153.   end X.Z;
  154. </PRE>
  155. <P>
  156. You can <EM>with</EM> a child package by specifying its parents in dotted
  157. notation.
  158. Whenever you name a child package in a "with" statement, you
  159. automatically "with" all of its ancestors.
  160. For example, if package "Ada" has a child package
  161. named "Strings", which then has a child
  162. package named "Unbounded", you would say:
  163. <PRE>
  164.   with Ada.Strings.Unbounded;
  165. </PRE>
  166. <P>
  167. When you "with" Ada.Strings.Unbounded, you automatically "with" the
  168. packages "Ada" and "Ada.Strings".
  169. That doesn't mean that you automatically "use" them; you can then "use"
  170. whichever packages you wish.
  171. <P>
  172. <P>
  173. There's a lot more about child packages that we won't cover now.
  174. <A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/Rationale/rat95html/rat95-p1-2.html#7">The
  175. Ada Rationale section II.7</A> describes child packages
  176. (also called hierarchical libraries) in more detail if you are curious.
  177.  
  178.