home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog3 / chap21.txt < prev    next >
Encoding:
Text File  |  1991-07-01  |  14.9 KB  |  351 lines

  1.  
  2.  
  3.  
  4.                                                        Chapter 21
  5.                                 ADVANCED PACKAGES & PRIVATE TYPES
  6.  
  7.  
  8. THIS IS FOR LARGE PROJECTS
  9. _________________________________________________________________
  10.  
  11. The material in this chapter is used for large projects when there
  12. are several programmers working together as a team.  In such a
  13. case, there is a demand for good communication between the various
  14. programmers, regardless of what programming language is used.  If
  15. Ada is the language chosen for the project, and if the principles
  16. set down in this chapter are carefully followed, the amount of
  17. communication required can be minimized.
  18.  
  19.  
  20.  
  21. TWO GOOD PROGRAMMERS
  22. _________________________________________________________________
  23.  
  24. In this chapter, we will assume we have a project being developed
  25. by two programmers, and that they are both sharp and well versed
  26. in how to develop quality software.  We will follow along with the
  27. example programs as they develop a simple system to add or subtract
  28. groups of three numbers.  One has been assigned the responsibility
  29. to write a package that the other programmer can use to add or
  30. subtract groups of three numbers of arbitrary structure.
  31.  
  32. This is a trivial problem, of course, but will illustrate the
  33. concept of information hiding, which is so necessary on a large
  34. project.
  35.  
  36.  
  37.  
  38. THE FIRST SOLUTION
  39. _________________________________________________________________
  40.  
  41. The example file named NOPRIVAT.ADA illustrates  ================
  42. the first attempt to solve the problem, however    NOPRIVAT.ADA
  43. it does not practice any information hiding.     ================
  44. Lines 1 through 69 represent the work of the
  45. package writer, and lines 74 through 101 are
  46. written by the user of the package.  The package writer used a
  47. record to store the three numbers and because he declared the
  48. record in the specification package, he gave the user full access
  49. to the record for his own use.
  50. The package writer, by providing three functions and a procedure,
  51. gave the user the ability to add the corresponding elements,
  52. subtract the corresponding elements, and two additional
  53. capabilities.  The function named Build_Structure allows the user
  54. to build a new structure given the individual components, and the
  55. procedure named Decompose, allows the user to separate the elements
  56. into their respective values.  We might add that this package is
  57.  
  58.                                                         Page 21-1
  59.  
  60.                    Chapter 21 - Advanced Packages & Private Types
  61.  
  62. a result of a meeting between the two programmers where they agreed
  63. on what the system must do and what the interface between them must
  64. be.  The package specification is intended to be the only
  65. information needed to define the interface between the programmers.
  66.  
  67. The user wished to change one value of the variable My_Data, and
  68. since he had the entire structure available, he cheated in line 99
  69. by circumventing the available structure handling method provided
  70. by the package writer.  He feels he saved a few instructions and
  71. a little execution time, so the little bit of cheating is
  72. justified.  Line 99 is perfectly legal, but could cause a problem
  73. in this instance due to the immense size of the project.  Recall
  74. that he had previously agreed to use only the functionality
  75. provided by the package writer because the package writer is
  76. supplying the same package to another large project and must
  77. maintain conformity with both users.
  78.  
  79.  
  80.  
  81. A PROBLEM DEVELOPS
  82. _________________________________________________________________
  83.  
  84. The package writer, for reasons of efficiency and conformity to the
  85. other project, decides to change the way he stores the data from
  86. a three element record, to a three element array.  With this
  87. modification in place and tested, the user suddenly finds that his
  88. program will no longer compile, even though nothing has been
  89. changed in his program.  The only problem is with line 99, which
  90. has been working just fine for months now, but as we stated
  91. earlier, is actually cheating on the interface agreed upon by the
  92. user and the package writer.  Information hiding would have
  93. prevented this problem, which in a real world system could cause
  94. hundreds of compiler errors following a seemingly minor change in
  95. one of the packages.
  96.  
  97. In this case, the user cheated and resulted ultimately in a rather
  98. large problem.  We need to prevent him from cheating.  Of course
  99. we could make a project rule that says "no cheating", but we will
  100. shortly define a way to let the compiler enforce the rule for us.
  101. It should be obvious to you that in a real-life large system, the
  102. called package and the calling package would not be in the same
  103. file, but would be separately compiled.  They were combined to make
  104. it easier for you to compile and execute, which you should do at
  105. this point.
  106.  
  107.  
  108. A BETTER WAY, THE PRIVATE TYPE
  109. _________________________________________________________________
  110.  
  111. Examine the program named PRIVAT1.ADA for an      ===============
  112. example of a private type which forces the user     PRIVAT1.ADA
  113. to follow the rules.  In the package              ===============
  114. specification, the type DATA_STRUCTURE is
  115. defined to be of type private, a reserved word
  116.  
  117.                                                         Page 21-2
  118.  
  119.                    Chapter 21 - Advanced Packages & Private Types
  120.  
  121. in Ada, which means that the type name is available for the user's
  122. use in declaring variables of this type, but the structure of the
  123. type is not available to him.  The actual type is defined at the
  124. end of the package specification following another use of the
  125. reserved word private.
  126.  
  127. Because the type is private, the user can make no use of the
  128. structure, even though he may be able to see the structure, as you
  129. can at this moment.  If the package writer wished to do so, he
  130. could actually remove the type definition from the listing and give
  131. the resulting listing to the user, but the fact that it is private
  132. is as good as physically hiding it from the user.
  133.  
  134. Because the type is private, the user has the ability to compare
  135. two variables of that type for either equality or inequality, and
  136. he can assign another variable or constant of that type to a
  137. variable of the same type.  No other operations on a variable of
  138. this private type are available to him, except for those
  139. specifically defined in the package specification itself.
  140.  
  141.  
  142.  
  143. NO TRICKS THIS TIME
  144. _________________________________________________________________
  145.  
  146. The trick, in line 99 of the last program is therefore illegal, and
  147. will result in a compile error.  If the user needs the ability to
  148. do some operation on this type variable, he must ask the package
  149. writer to provide it for him.  The package writer has the ability
  150. to do anything with the components of the structure within the
  151. package itself, and define any new procedures or functions
  152. necessary for the intelligent use of the type.  In order for the
  153. user to add 13 to one member, it is now necessary to declare a
  154. temporary record variable, load it with all fields, and add the
  155. entire temporary variable of the type DATA_STRUCTURE to the
  156. variable.  This is illustrated in lines 104 and 105.  It is rather
  157. obvious that these two statements could be combined and the
  158. variable named Temp not needed, but this was done for clarity.
  159.  
  160.  
  161.  
  162. WHERE IS THIS TYPE AVAILABLE?
  163. _________________________________________________________________
  164.  
  165. Following the partial declaration of the record type in line 10,
  166. it is available for use in the remainder of the specification
  167. package, and throughout the entire package body.  It is important
  168. to remember that the private type is available in these areas just
  169. as if it were not private.  It is only treated in a different
  170. manner with respect to the using program.
  171.  
  172. The remaining details of the program should be simple for you to
  173. understand, then to compile and execute.  Note that the output is
  174. identical to that from the previous example program.
  175.  
  176.                                                         Page 21-3
  177.  
  178.                    Chapter 21 - Advanced Packages & Private Types
  179.  
  180.  
  181. A NEW PACKAGE
  182. _________________________________________________________________
  183.  
  184. The package writer, for his own mysterious        ===============
  185. reasons, decides to change the package to use a     PRIVAT2.ADA
  186. different structure.  The example program named   ===============
  187. PRIVAT2.ADA does exactly that.  The only change
  188. from PRIVAT1.ADA is the method of defining the
  189. structure, in this case an array being used.  Since the user was
  190. forced to follow the rules, his program will run with no
  191. modifications.  See if you can find anything you can do in the main
  192. program using the structure defined in either program that cannot
  193. be done in the other.  According to the design of Ada, it should
  194. be impossible for you to do so.
  195.  
  196. Notice that the internal workings of the package body are
  197. significantly different to reflect the difference in the two data
  198. structures, but externally, you cannot tell the difference.  Even
  199. though the package was changed significantly, none of the users
  200. will see a difference and there will be no incompatibility
  201. problems.  Notice that the main program is identical to the main
  202. program in the last example program.  Be sure to compile and
  203. execute this program.
  204.  
  205.  
  206.  
  207. THE LIMITED PRIVATE TYPE
  208. _________________________________________________________________
  209.  
  210. Examine the program named LIMPRIV.ADA for an      ===============
  211. example of a limited private type.  The biggest     LIMPRIV.ADA
  212. difference between this program and the previous  ===============
  213. one is the addition of the reserved word limited
  214. in line 10 which declares the type
  215. DATA_STRUCTURE to be of type limited private.  This gives the
  216. package writer even more control over what the user can do with a
  217. variable of this type.  There are no operations the user can
  218. perform on data of this type except for those explicitly spelled
  219. out by the package writer in the form of procedures and functions.
  220.  
  221. If the user were to declare two variables of this type, the
  222. compiler would not allow him to compare them for equality or
  223. inequality, nor could he assign one variable to the other.  In
  224. fact, he could not even declare a variable of this type and
  225. initialize it to some value, because that would require an implied
  226. assignment statement.  He cannot declare a constant of this type
  227. either since that would also require an assignment statement.  The
  228. package writer provided an additional procedure named
  229. Assign_Struct, and an additional function named Compare so the user
  230. could perform these operations.  The program itself, beginning in
  231. line 93, is a bit messy because of the procedure calls required to
  232. do the assignments.  You are surely asking, "why should we go to
  233. all of the trouble to use the limited private type?"
  234.  
  235.                                                         Page 21-4
  236.  
  237.                    Chapter 21 - Advanced Packages & Private Types
  238.  
  239.  
  240. WHY USE LIMITED PRIVATE TYPES?
  241. _________________________________________________________________
  242.  
  243. It is conceivable that you, as the package writer, do not wish to
  244. give the user the ability to compare or assign variables of the
  245. type in question because of the nature of the project.  You may be
  246. working on some form of a data encryption system, or a limited
  247. access file management system, and you wish to be sure that all
  248. users are positively locked out of access to certain files.  The
  249. lack of compares and assignments would make it impossible for a
  250. user to access the lower levels of your system.  This is a possible
  251. use for the limited private type, but there is a much more
  252. important need for it.
  253.  
  254.  
  255.  
  256. OVERLOADING THE "=" OPERATOR
  257. _________________________________________________________________
  258.  
  259. The equals operator is defined for the private type, so it cannot
  260. be overloaded in the same manner that we have been overloading
  261. operators in this tutorial.  The limited private type does not have
  262. the equality operator available for use, so it can be overloaded
  263. in any manner the package writer decrees.  Suppose for example that
  264. you were using the dynamic string package, named DYNSTRNG.ADA, from
  265. chapter 16 of this tutorial, and you wished to compare two strings
  266. for equality.  If the strings were, for example, 80 characters
  267. long, but they only had 31 characters in use at this time, an
  268. overloading of the equality operator could provide a function that
  269. would compare only the 31 "active" characters of the string,
  270. stopping short of the additional characters in the static string
  271. declaration.  This would allow the comparison to be done using an
  272. infix operator rather than a function call for the comparison.
  273. Overloading the "=" operator is not permitted for any type but the
  274. limited private type in all of Ada.
  275.  
  276. You may think that it is very inefficient to write a program that
  277. requires a procedure or function call to do anything at all, but
  278. that is not necessarily the case.  If the compiler writer has done
  279. a good job, a subprogram call can be very efficient, and a special
  280. purpose comparison could be much more efficient than a general
  281. purpose comparison routine that is designed to handle all cases.
  282. Because a general purpose routine is designed for flexibility, it
  283. may not do any particular comparison very efficiently.
  284.  
  285. Be sure you compile and execute this program so you can verify that
  286. it does the same thing as the last two example programs.
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.                                                         Page 21-5
  295.  
  296.                    Chapter 21 - Advanced Packages & Private Types
  297.  
  298. A PACKAGE WITHOUT A BODY
  299. _________________________________________________________________
  300.  
  301. It is possible to declare a package specification without a
  302. corresponding body.  This would be done if you wished to declare
  303. types, variables, and constants that could be used by other
  304. packages and subprograms, but included no executable code with the
  305. declarations.  Programming exercise 3 will illustrate this
  306. technique.  It is never possible to declare a package body without
  307. a corresponding package specification.
  308.  
  309.  
  310.  
  311. PROGRAMMING EXERCISES
  312. _________________________________________________________________
  313.  
  314. 1.   Add a function to PRIVATE1.ADA named Compare_Sum, which
  315.      compares the sum of the three elements of one structure to the
  316.      sum of the three elements of the second structure, returning
  317.      a TRUE if the sums are equal, and a FALSE otherwise.  Add a
  318.      few statements to the main program to exercise this new
  319.      function.
  320.  
  321. 2.   Add the same function to PRIVATE2.ADA, and add a few
  322.      statements to the main program to test it.
  323.  
  324. 3.   Write a package named Stuff that contains only a package
  325.      specification.  The package specification should make the
  326.      value of Pi (3.14159) and Two_Pi available as well as a DATE
  327.      record type.  Include a short program to use the package.
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.                                                         Page 21-6