|
| 1 | +============================================ |
| 2 | +Implementing Abstract Data Types Via Views |
| 3 | +============================================ |
| 4 | + |
| 5 | +---------------------------------- |
| 6 | +Implementing Abstract Data Types |
| 7 | +---------------------------------- |
| 8 | + |
| 9 | +* A combination of constructs in Ada |
| 10 | +* Not based on single "class" construct, for example |
| 11 | +* Constituent parts |
| 12 | + |
| 13 | + - Packages, with "private part" of package spec |
| 14 | + - "Private types" declared in packages |
| 15 | + - Subprograms declared within those packages |
| 16 | + |
| 17 | +--------------------------------------------- |
| 18 | +Package Visible and Private Parts for Views |
| 19 | +--------------------------------------------- |
| 20 | + |
| 21 | +* Declarations in visible part are exported to users |
| 22 | +* Declarations in private part are hidden from users |
| 23 | + |
| 24 | + - No compilable references to type's actual representation |
| 25 | + |
| 26 | +.. code:: Ada |
| 27 | +
|
| 28 | + package name is |
| 29 | + ... exported declarations of types, variables, subprograms ... |
| 30 | + private |
| 31 | + ... hidden declarations of types, variables, subprograms ... |
| 32 | + end name; |
| 33 | +
|
| 34 | +----------------------------------- |
| 35 | +Declaring Private Types for Views |
| 36 | +----------------------------------- |
| 37 | + |
| 38 | +* Partial syntax |
| 39 | + |
| 40 | + .. code:: Ada |
| 41 | +
|
| 42 | + type defining_identifier is private; |
| 43 | +
|
| 44 | +* Private type declaration must occur in visible part |
| 45 | + |
| 46 | + - :dfn:`Partial view` |
| 47 | + - Only partial information on the type |
| 48 | + - Users can reference the type name |
| 49 | + |
| 50 | + - But cannot create an object of that type until after the full type declaration |
| 51 | + |
| 52 | +* Full type declaration must appear in private part |
| 53 | + |
| 54 | + - Completion is the :dfn:`Full view` |
| 55 | + - **Never** visible to users |
| 56 | + - **Not** visible to designer until reached |
| 57 | + |
| 58 | +.. code:: Ada |
| 59 | +
|
| 60 | + package Bounded_Stacks is |
| 61 | + type Stack is private; |
| 62 | + procedure Push (Item : in Integer; Onto : in out Stack); |
| 63 | + ... |
| 64 | + private |
| 65 | + ... |
| 66 | + type Stack is record |
| 67 | + Top : Positive; |
| 68 | + ... |
| 69 | + end Bounded_Stacks; |
| 70 | +
|
| 71 | +--------------------------------- |
| 72 | +Partial and Full Views of Types |
| 73 | +--------------------------------- |
| 74 | + |
| 75 | +* Private type declaration defines a :dfn:`partial view` |
| 76 | + |
| 77 | + - The type name is visible |
| 78 | + - Only designer's operations and some predefined operations |
| 79 | + - No references to full type representation |
| 80 | + |
| 81 | +* Full type declaration defines the :dfn:`full view` |
| 82 | + |
| 83 | + - Fully defined as a record type, scalar, imported type, etc... |
| 84 | + - Just an ordinary type within the package |
| 85 | + |
| 86 | +* Operations available depend upon one's view |
| 87 | + |
| 88 | +--------------------------------- |
| 89 | +Software Engineering Principles |
| 90 | +--------------------------------- |
| 91 | + |
| 92 | +* Encapsulation and abstraction enforced by views |
| 93 | + |
| 94 | + - Compiler enforces view effects |
| 95 | + |
| 96 | +* Same protection as hiding in a package body |
| 97 | + |
| 98 | + - Recall "Abstract Data Machines" idiom |
| 99 | + |
| 100 | +* Additional flexibility of types |
| 101 | + |
| 102 | + - Unlimited number of objects possible |
| 103 | + - Passed as parameters |
| 104 | + - Components of array and record types |
| 105 | + - Dynamically allocated |
| 106 | + - et cetera |
| 107 | + |
| 108 | +----------------------------------- |
| 109 | +Users Declare Objects of the Type |
| 110 | +----------------------------------- |
| 111 | + |
| 112 | +* Unlike "abstract data machine" approach |
| 113 | +* Hence must specify which stack to manipulate |
| 114 | + |
| 115 | + - Via parameter |
| 116 | + |
| 117 | +.. code:: Ada |
| 118 | +
|
| 119 | + X, Y, Z : Bounded_Stacks.Stack; |
| 120 | + ... |
| 121 | + Push (42, X); |
| 122 | + ... |
| 123 | + if Empty (Y) then |
| 124 | + ... |
| 125 | + Pop (Counter, Z); |
| 126 | +
|
| 127 | +------------------------------------ |
| 128 | +Compile-Time Visibility Protection |
| 129 | +------------------------------------ |
| 130 | + |
| 131 | +* No type representation details available outside the package |
| 132 | +* Therefore users cannot compile code referencing representation |
| 133 | +* This does not compile |
| 134 | + |
| 135 | + .. code:: Ada |
| 136 | +
|
| 137 | + with Bounded_Stacks; |
| 138 | + procedure User is |
| 139 | + S : Bounded_Stacks.Stack; |
| 140 | + begin |
| 141 | + S.Top := 1; -- Top is not visible |
| 142 | + end User; |
| 143 | +
|
| 144 | +------------------- |
| 145 | +Benefits of Views |
| 146 | +------------------- |
| 147 | + |
| 148 | +* Users depend only on visible part of specification |
| 149 | + |
| 150 | + - Impossible for users to compile references to private part |
| 151 | + - Physically seeing private part in source code is irrelevant |
| 152 | + |
| 153 | +* Changes to implementation don't affect users |
| 154 | + |
| 155 | + - No editing changes necessary for user code |
| 156 | + |
| 157 | +* Implementers can create bullet-proof abstractions |
| 158 | + |
| 159 | + - If a facility isn't working, you know where to look |
| 160 | + |
| 161 | +* Fixing bugs is less likely to introduce new ones |
| 162 | + |
| 163 | +------ |
| 164 | +Quiz |
| 165 | +------ |
| 166 | + |
| 167 | +.. code:: Ada |
| 168 | +
|
| 169 | + package P is |
| 170 | + type Private_T is private; |
| 171 | +
|
| 172 | + type Record_T is record |
| 173 | +
|
| 174 | +Which component(s) is (are) legal? |
| 175 | + |
| 176 | + A. ``Field_A : Integer := Private_T'Pos (Private_T'First);`` |
| 177 | + B. ``Field_B : Private_T := null;`` |
| 178 | + C. ``Field_C : Private_T := 0;`` |
| 179 | + D. :answermono:`Field_D : Integer := Private_T'Size;` |
| 180 | + |
| 181 | + .. code:: Ada |
| 182 | +
|
| 183 | + end record; |
| 184 | +
|
| 185 | +.. container:: animate |
| 186 | + |
| 187 | + Explanations |
| 188 | + |
| 189 | + A. Visible part does not know :ada:`Private_T` is discrete |
| 190 | + B. Visible part does not know possible values for :ada:`Private_T` |
| 191 | + C. Visible part does not know possible values for :ada:`Private_T` |
| 192 | + D. Correct - type will have a known size at run-time |
| 193 | + |
0 commit comments