Franco Gasperoni
                             gasperon@act-europe.fr
                             http://libre.act-europe....
Copyright Notice

           • © ACT Europe under the GNU Free Documentation License

           • Permission is granted t...
3
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
What is the largest
                              software system
                              you have built ?

        ...
Small Software Systems

           • Understandable by 1 person

           • Can throw away & replace it to
             ...
Medium/Large Systems

           • Team of people
           • No 1 person knows all its aspects
           • Long life-sp...
• Programming in the Large
                             – specification & implementation
                             – pr...
Separate Compilation
       T
                               C
       H                       O
                          ...
Problem with this approach
                                 C              C
                                             ...
Idea
                              SPECIFY
                             WHAT EACH
                              MODULE
   ...
SPECIFICATION




                                      ?
                                BODY                            ...
• Programming in the Large
                             – specification & implementation
                               • ...
A Specification is a ...

                                                                                           Users...
• SPEC = list of services provided

       • BODY = implementation of the services (hidden)


                            ...
SPECIFICATION




                                             ?
                                         BODY            ...
Example

           • Create a Queue module that can
                  – Add an Integer to the Queue
                  – S...
queue.ads
          queue.ads
                    package Queue is
                    package Queue is
                  ...
queue.ads
                                                                            queue.ads
                          ...
Using package Queue
                         client.adb
                                with Queue;
                      ...
Specifications Reduce Complexity
                                      SPEC
                                     SPEC
    ...
To write Client only need to look at
                                                                             queue.ad...
Aside: use clause
                             with Queue; use Queue;
                             procedure Client is
   ...
• Programming in the Large
                             – specification & implementation
                               • ...
ONE possible
                             implementation
                               of package
                       ...
Circular Buffer


     Q

                   0         1
                                                                 ...
queue.adb
           package body Queue is
              Max_Size : constant := 100;

                  type Q_Index is mo...
package body Queue is          queue.adb
                                ...
                                function Firs...
ANOTHER possible
                              implementation
                                of package
                 ...
Linked List




              Q_First                                    Q_Last




                 Free                 ...
queue.adb
                  package body Queue is
                     type Queue_Element;
                     type Eleme...
queue.adb
             package body Queue is
                    ...
                    procedure Add (Element : Integer)...
queue.adb
                             package body Queue is
                                ...
                         ...
queue.adb
                             package body Queue is
                                ...
                         ...
A Spec can have several
                                     implementations
             queue.ads
           queue.ads
 ...
• Programming in the Large
                             – specification & implementation
                               • ...
In Ada

           • Spec always checked against implementation

           • Must with the specs that you are going to
  ...
Spec is checked against its body
                    package Queue is
                    package Queue is
               ...
Must with Specs used


                              with Queue;
                              procedure Client is        ...
Multiple Name Spaces
         package Queue is
        package Queue is                                        package Set...
Use Clause and Ambiguities
         package Queue is
        package Queue is                                    package S...
But … Ada has overloading
         package Queue is
        package Queue is
           procedure Add (E ::Integer);
     ...
• Programming in the Large
                             – specification & implementation
                             – pr...
Having Several Queues

                    package Queues is
                    package Queues is
                       ...
!!! WARNING !!!

                    package Queues is
                    package Queues is

                            ...
Using Several Queues
                                with Queues; use Queues;
                                procedure Cl...
One possible implementation ...
                                                             type Q_Element;
             ...
Client code allowed to depend
                on the implementation !
                             with Queues; use Queues...
Another implementation ...

                                                      Max_Size ::constant := 100;
            ...
… breaks client code !

                                 with Queues; use Queues;
                                 procedu...
Even without changing
                               the implementation
                              there is a PROBLEM
 ...
You need PRIVACY

           • Exposing your data structures is risky

                  – Client code may manipulate the ...
Think                  BIG
                               what if
                         the Queues package
            ...
• If there is a bug concerning a Queue, you
        may have to look at 1000s of packages to
        find the bug



     ...
• Programming in the Large

                             – privacy
                               • private types
        ...
Private types

                    package Queues is
                    package Queues is
                        type Qu...
In any implementation ...
                             package Queues is
                                type Queue is pri...
… private types are PRIVATE

                             with Queues; use Queues;
                             procedure ...
Advantages of private types

           • Enforces the contract of a specification

           • No client code can corrup...
Why is the private part in the spec ?
                             package Queues is
                                type ...
… because we still need to
                    compile the clients code
                             with Queues; use Queu...
… but you can make a private type
              quite private

                     package Queues is
                    ...
package body Queues is
                                type Queue_Element;
                                type Element_Pt...
• Programming in the Large

                             – privacy
                               • private types
        ...
package Queues is
                     type Queue (Max_Size : Natural) is private;

                             procedure...
with Queues; use Queues;
                         procedure Client is

                             Q1 : Queue (100);     ...
• Programming in the Large

                             – privacy
                               • private types
        ...
with Queues; use Queues;
                             procedure Client is
                                Q1 : Queue;
    ...
it depends on … the
                                implementation !

           • If a Queue is implemented with

       ...
with Queues; use Queues;                                         Pointer
   procedure Client is                           ...
limited private types



           • NO assignment                                :=

           • NO equality comparison...
package Queues is
                    package Queues is
                        type Queue is limited private;
           ...
with Queues; use Queues;
                             procedure Client is
                                Q1 : Queue;
    ...
• Programming in the Large
                             – specification & implementation
                             – pr...
Queue is an
                             Abstract Data Type
                                   (ADT)


                   ...
Queue is an ADT
                                                     values
                 package Queues is
           ...
Objects & Variables

                 • OBJECT = instance of an ADT
                        – piece of memory containing v...
an object



                   My_Q : Queue;
                                                                            ...
not all objects have a name


        type Queue_Ptr is access Queue;                                                  Que...
CLASS = ADT
                                   + inheritance




                                                         ...
operation                                                       invoke some
                                              ...
The object model




                                                                                        81
http://lib...
The C model


                             GLOBAL DATA




               C module      C module                          ...
• Programming in the Large
                             – specification & implementation
                             – pr...
To add functionality ...
      function Last (Q : Queue) return Integer;

                  package Queues is
            ...
But ...

           • Every time you change a spec you must
             recompile all its clients

           • Every tim...
Solution: use child units
                                                                                        queues.a...
Child Units Rules

           • The body or private part of a child unit can
             see the private part of all of i...
Using a child unit

                             with Queues; use Queues;
                             with Queues.Last;
 ...
queues.ads
                             package Queues is
                                type Queue is private;
         ...
with Queues; use Queues;
                         with Queues.New_Functionality;
                         procedure Client...
Upcoming SlideShare
Loading in...5
×

Ada 95 - Programming in the large

999
-1

Published on

Author: Franco Gasperoni. License: GFDL

Published in: Technology
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total Views
999
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
28
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Ada 95 - Programming in the large

  1. 1. Franco Gasperoni gasperon@act-europe.fr http://libre.act-europe.fr 1 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  2. 2. Copyright Notice • © ACT Europe under the GNU Free Documentation License • Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; provided its original author is mentioned and the link to http://libre.act-europe.fr/ is kept at the bottom of every non-title slide. A copy of the license is available at: • http://www.fsf.org/licenses/fdl.html 2 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  3. 3. 3 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  4. 4. What is the largest software system you have built ? (in SLOC) 4 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  5. 5. Small Software Systems • Understandable by 1 person • Can throw away & replace it to – repair / extend – port to new platform • Anything is OK for small systems (< 10 Ksloc) 5 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  6. 6. Medium/Large Systems • Team of people • No 1 person knows all its aspects • Long life-span (> 10 years) • CANNOT throw away & replace it to – repair / extend – port to new platform • Requires organization, discipline & right tools 6 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  7. 7. • Programming in the Large – specification & implementation – privacy – abstract data types – hierarchical packages 7 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  8. 8. Separate Compilation T C H O D Compiler object E E L C i P O D Compiler object n executable k R E e O r B L C O Compiler object E D E libraries M 8 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  9. 9. Problem with this approach C C O C O O D D E D C E E O D C E O D E • No structure • To write your own code – YOU MUST understand everybody else’s code 9 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  10. 10. Idea SPECIFY WHAT EACH MODULE SHOULD DO 10 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  11. 11. SPECIFICATION ? BODY Software module 11 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  12. 12. • Programming in the Large – specification & implementation • specification • implementation • specification rules in Ada 12 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  13. 13. A Specification is a ... Users/clients Implementor of the of the module module CONTRACT • On the SERVICES provided by the module 13 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  14. 14. • SPEC = list of services provided • BODY = implementation of the services (hidden) Service_1 Service_2 Service_3 Service_1 implementation Service_2 implementation Service_3 implementation Software module 14 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  15. 15. SPECIFICATION ? BODY 15 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  16. 16. Example • Create a Queue module that can – Add an Integer to the Queue – See the First integer in the Queue – Get the first integer in the Queue – Test whether the Queue is Empty 16 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  17. 17. queue.ads queue.ads package Queue is package Queue is procedure Add (Element ::Integer); procedure Add (Element Integer); function First return Integer; function First return Integer; function Get return Integer; function Get return Integer; function Empty return Boolean; function Empty return Boolean; end Queue; end Queue; 17 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  18. 18. queue.ads queue.ads package Queue is package Queue is procedure Add (Element : :Integer); procedure Add (Element Integer); function First return Integer; function First return Integer; function Get return Integer; function Get return Integer; function Empty return Boolean; function Empty return Boolean; end Queue; end Queue; ? BODY package Queue 18 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  19. 19. Using package Queue client.adb with Queue; procedure Client is Queue_Error : exception; X : Integer; begin Queue.Add (3); Queue.Add (4); if not Queue.Empty then X := Queue.Get; else raise Queue_Error; end if; end Client; 19 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  20. 20. Specifications Reduce Complexity SPEC SPEC SPEC SPEC SPEC SPEC SPEC SPEC • To write your own code – only need to understand specs for the services you need 20 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  21. 21. To write Client only need to look at queue.ads queue.ads package Queue is package Queue is procedure Add (Element : :Integer); procedure Add (Element Integer); function First return Integer; function First return Integer; function Get return Integer; function Get return Integer; function Empty return Boolean; function Empty return Boolean; end Queue; end Queue; ? package Queue 21 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  22. 22. Aside: use clause with Queue; use Queue; procedure Client is Queue_Error : exception; X : Integer; begin Queue. Add (3); Queue. Add (4); if not Queue. Empty then X := Queue. Get; else raise Queue_Error; end if; end Client; 22 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  23. 23. • Programming in the Large – specification & implementation • specification • implementation • specification rules in Ada 23 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  24. 24. ONE possible implementation of package Queue This implementation raises Constraint_Error if more than Max_Size elements are put in the Queue. 24 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  25. 25. Circular Buffer Q 0 1 Max_Size - 1 Q_Last Q_First 25 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  26. 26. queue.adb package body Queue is Max_Size : constant := 100; type Q_Index is mod Max_Size; Q : array (Q_Index range 0 .. Max_Size - 1) of Integer; Q_First : Q_Index := Q ’ First; Q_Last : Q_Index := Q_First; Size : Natural range 0 .. Max_Size; procedure Add (Element : Integer) is begin Q (Q_Last) := Element; Q_Last := Q_Last + 1; Size := Size + 1; end Add; ... 26 end Queue; http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  27. 27. package body Queue is queue.adb ... function First return Integer is begin return Q (Q_First); end First; function Get return Integer is begin Q_First := Q_First + 1; Size := Size - 1; return Q (Q_First - 1); end Get; function Empty return Boolean is begin return Size = 0; end Empty; 27 http://libre.act-europe.fr end Queue; under the GNU Free Documentation License © ACT Europe
  28. 28. ANOTHER possible implementation of package Queue 28 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  29. 29. Linked List Q_First Q_Last Free 29 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  30. 30. queue.adb package body Queue is type Queue_Element; type Element_Ptr is access Queue_Element; type Queue_Element is record Val : Integer; Next : Element_Ptr; end record; Q_First : Element_Ptr; Q_Last : Element_Ptr; Free : Element_Ptr := new Queue_Element; ... end Queue; 30 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  31. 31. queue.adb package body Queue is ... procedure Add (Element : Integer) is begin if Q_First = null then Q_First := Free; else Q_Last.Next := Free; end if; Q_Last := Free; Free := Free.Next; Q_Last.all := (Element, null); if Free = null then Free := new Queue_Element; end if; end Add; ... 31 end Queue; http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  32. 32. queue.adb package body Queue is ... function Get return Integer is Tmp : Element_Ptr := Q_First; begin Q_First := Q_First.Next; if Q_First = null then Q_Last := null; end if; Tmp.Next := Free; Free := Tmp; return Tmp.Val; end Get; ... end Queue; 32 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  33. 33. queue.adb package body Queue is ... function First return Integer is begin return Q_First; end First; function Empty return Boolean is begin return Q_First = null; end Empty; end Queue; 33 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  34. 34. A Spec can have several implementations queue.ads queue.ads package Queue is package Queue is (Element : Integer); procedure Add procedure Add (Element : Integer); function First return Integer; function First return Integer; function Get return Integer; function Get return Integer; function Empty return Boolean; function Empty return Boolean; end Queue; end Queue; • Can change implementation second second first first implement. implement. implement. implement. • WITHOUT having to change ANY of the client’s code 34 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  35. 35. • Programming in the Large – specification & implementation • specification • implementation • specification rules in Ada 35 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  36. 36. In Ada • Spec always checked against implementation • Must with the specs that you are going to use (not in C) • Packages provide multiple name spaces 36 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  37. 37. Spec is checked against its body package Queue is package Queue is procedure Add (Element ::Integer); procedure Add (Element Integer); ... ... end Queue; end Queue; Compilation error package body Queue is package body Queue is ... ... procedure Add (Element ::Integer; X ::Float) is procedure Add (Element Integer; X Float) is ... ... end Add; end Add; ... ... end Queue; end Queue; 37 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  38. 38. Must with Specs used with Queue; procedure Client is Compilation ... error begin Queue.Add (3); ... end Client; 38 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  39. 39. Multiple Name Spaces package Queue is package Queue is package Set is package Set is procedure Add (E ::Integer); procedure Add (E Integer); procedure Add (E ::Integer); procedure Add (E Integer); ... ... ... ... end Queue; end Queue; end Set; end Set; with Queue; with Set; procedure Client is begin Queue.Add (3); Set.Add (99); end Client; 39 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  40. 40. Use Clause and Ambiguities package Queue is package Queue is package Set is package Set is procedure Add (E ::Integer); procedure Add (E Integer); procedure Add (E ::Integer); procedure Add (E Integer); ... ... ... ... end Queue; end Queue; end Set; end Set; with Queue; use Queue; with Set; use Set; procedure Client is begin Compilation Add (123); error end Client; ambiguity 40 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  41. 41. But … Ada has overloading package Queue is package Queue is procedure Add (E ::Integer); procedure Add (E Integer); procedure Add (E ::Float); procedure Add (E Float); ... ... end Queue; end Queue; with Queue; use Queue; procedure Client is begin Add (123); Add (3.141); end Client; 41 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  42. 42. • Programming in the Large – specification & implementation – privacy – abstract data types – hierarchical packages 42 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  43. 43. Having Several Queues package Queues is package Queues is type Queue is …; type Queue is …; procedure Add (Q ::Queue; Element ::Integer); procedure Add (Q Queue; Element Integer); function First (Q ::Queue) return Integer; function First (Q Queue) return Integer; function Get (Q :: Queue) return Integer; function Get (Q Queue) return Integer; function Empty (Q ::Queue) return Boolean; function Empty (Q Queue) return Boolean; end Queues; end Queues; 43 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  44. 44. !!! WARNING !!! package Queues is package Queues is type Queue is …; Use Different names type Queue is …; end Queues; end Queues; 44 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  45. 45. Using Several Queues with Queues; use Queues; procedure Client is Q1 : Queue; Q2 : Queue; begin Add (Q1, 123); Add (Q2, 3); Add (Q2, Get (Q1)); end Client; 45 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  46. 46. One possible implementation ... type Q_Element; type Q_Element; type Element_Ptr is type Element_Ptr is access Queue_Element; access Queue_Element; type Queue_Element is record type Queue_Element is record Val ::Integer; Val Integer; Next ::Element_Ptr; Next Element_Ptr; end record; end record; package Queues is type Queue is …; type Queue is record type Queue is record First ::Element_Ptr; First Element_Ptr; procedure Add (Q : Queue; Element : Integer); Last : Element_Ptr; function First (Q : Queue) return Integer; Last : Element_Ptr; end record; function Get (Q : Queue) return Integer; end record; function Empty (Q : Queue) return Boolean; end Queues; 46 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  47. 47. Client code allowed to depend on the implementation ! with Queues; use Queues; procedure Client is Q1 : Queue; Q2 : Queue; begin Add (Q1, 123); Add (Q2, 3); Q2.Last := null; OK end Client; 47 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  48. 48. Another implementation ... Max_Size ::constant := 100; Max_Size constant := 100; type Q_array (Natural range <>) type Q_array (Natural range <>) of Integer; of Integer; type Queue is record type Queue is record Q ::Q_Array (0 .. Max_Size); Q Q_Array (0 .. Max_Size); package Queues is First ::Natural; First Natural; type Queue is …; Last ::Natural; Last Natural; procedure Add (Q : Queue; Element : Integer);Size ::Natural; Size Natural; end record; function First (Q : Queue) return Integer;end record; function Get (Q : Queue) return Integer; function Empty (Q : Queue) return Boolean; end Queues; 48 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  49. 49. … breaks client code ! with Queues; use Queues; procedure Client is Q1 : Queue; Q2 : Queue; begin Add (Q1, 123); Add (Q2, 3); Compilation error Q2.Last := null; end Client; 49 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  50. 50. Even without changing the implementation there is a PROBLEM with Queues; use Queues; procedure Client is Q1 : Queue; Q2 : Queue; begin Add (Q1, 123); Q2: Q2 is in an 3 3 inconsistent Add (Q2, 3); First state Last Q2.Last := null; end Client; null 50 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  51. 51. You need PRIVACY • Exposing your data structures is risky – Client code may manipulate the structures directly without using your own services – Client code is hard to change 51 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  52. 52. Think BIG what if the Queues package is used by 1000s of other packages 52 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  53. 53. • If there is a bug concerning a Queue, you may have to look at 1000s of packages to find the bug • If you change the implementation you may have to update 1000s of packages 53 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  54. 54. • Programming in the Large – privacy • private types • private types & discriminants • limited private types 54 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  55. 55. Private types package Queues is package Queues is type Queue is private; type Queue is private; procedure Add (Q ::Queue; Element ::Integer); procedure Add (Q Queue; Element Integer); function First (Q ::Queue) return Integer; function First (Q Queue) return Integer; function Get (Q :: Queue) return Integer; function Get (Q Queue) return Integer; function Empty (Q ::Queue) return Boolean; function Empty (Q Queue) return Boolean; private private type Queue is …; type Queue is …; end Queues; end Queues; 55 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  56. 56. In any implementation ... package Queues is type Queue is private; procedure Add (Q : Queue; Element : Integer); function First (Q : Queue) return Integer; function Get (Q : Queue) return Integer; function Empty (Q : Queue) return Boolean; private type Queue_Element; type Element_Ptr is access Queue_Element; type Queue_Element is record Val : Integer; Next : Element_Ptr; end record; type Queue is record First : Element_Ptr; Last : Element_Ptr; end record; end Queues; 56 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  57. 57. … private types are PRIVATE with Queues; use Queues; procedure Client is Q1 : Queue; Q2 : Queue; begin Add (Q1, 123); Add (Q2, 3); Compilation Q2.Last := null; error end Client; 57 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  58. 58. Advantages of private types • Enforces the contract of a specification • No client code can corrupt your data structures • Can change implementation without changing client code 58 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  59. 59. Why is the private part in the spec ? package Queues is type Queue is private; procedure Add (Q : Queue; Element : Integer); function First (Q : Queue) return Integer; function Get (Q : Queue) return Integer; function Empty (Q : Queue) return Boolean; private type Queue_Element; type Element_Ptr is access Queue_Element; type Queue_Element is record Val : Integer; Next : Element_Ptr; end record; type Queue is record First : Element_Ptr; Last : Element_Ptr; end record; end Queues; © ACT Europe under the GNU Free Documentation License 59 http://libre.act-europe.fr
  60. 60. … because we still need to compile the clients code with Queues; use Queues; procedure Client is Q1 : Queue; begin Add (Q1, 123); end Client; 60 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  61. 61. … but you can make a private type quite private package Queues is type Queue is private; procedure Add (Q : Queue; Element : Integer); function First (Q : Queue) return Integer; function Get (Q : Queue) return Integer; function Empty (Q : Queue) return Boolean; private type Queue_Info; type Queue is access Queue_Info; end Queues; 61 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  62. 62. package body Queues is type Queue_Element; type Element_Ptr is access Queue_Element; type Queue_Element is record Val : Integer; Next : Element_Ptr; end record; type Queue_Info is record First : Element_Ptr; Last : Element_Ptr; end record; ... end Queues; 62 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  63. 63. • Programming in the Large – privacy • private types • private types & discriminants • limited private types 63 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  64. 64. package Queues is type Queue (Max_Size : Natural) is private; procedure Add (Q : Queue; Element : Integer); function First (Q : Queue) return Integer; function Get (Q : Queue) return Integer; function Empty (Q : Queue) return Boolean; private type Q_array (Natural range <>) of Integer; type Queue (Max_Size : Natural) is record Q : Q_Array (0 .. Max_Size); First : Natural; Last : Natural; Size : Natural; end record; end Queues; 64 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  65. 65. with Queues; use Queues; procedure Client is Q1 : Queue (100); Q1 can have up to 100 elmts Q2 : Queue (250); Q2 can have up to 250 elmts begin Add (Q1, 123); Add (Q2, 3); end Client; 65 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  66. 66. • Programming in the Large – privacy • private types • private types & discriminants • limited private types 66 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  67. 67. with Queues; use Queues; procedure Client is Q1 : Queue; Q2 : Queue; X : Integer; begin Add (Q1, 123); Add (Q1, 3); Does this affect Q1 ? Q2 := Q1; X := Get (Q2); end Client; 67 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  68. 68. it depends on … the implementation ! • If a Queue is implemented with – a pointer then Get (Q2) MODIFIES Q1 – a record containing an array then Get (Q2) does NOT modify Q1 68 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  69. 69. with Queues; use Queues; Pointer procedure Client is Pointer Q1 : Queue; implementation implementation Q2 : Queue; X : Integer; begin Q1: 123 123 Add (Q1, 123); Add (Q1, 3); 123 3 Q1: 123 3 Q2 := Q1; Q2: Q1: 123 123 3 3 X := Get (Q2); end Client; Q2: 69 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  70. 70. limited private types • NO assignment := • NO equality comparison = 70 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  71. 71. package Queues is package Queues is type Queue is limited private; type Queue is limited private; procedure Add (Q ::Queue; Element ::Integer); procedure Add (Q Queue; Element Integer); function First (Q ::Queue) return Integer; function First (Q Queue) return Integer; function Get (Q :: Queue) return Integer; function Get (Q Queue) return Integer; function Empty (Q ::Queue) return Boolean; function Empty (Q Queue) return Boolean; private private type Queue is …; type Queue is …; end Queues; end Queues; 71 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  72. 72. with Queues; use Queues; procedure Client is Q1 : Queue; Q2 : Queue; X : Integer; begin Add (Q1, 123); Add (Q1, 3); COMPILATION ERROR Q2 := Q1; := forbidden end Client; 72 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  73. 73. • Programming in the Large – specification & implementation – privacy – abstract data types – hierarchical packages 73 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  74. 74. Queue is an Abstract Data Type (ADT) • Set of abstract values (data domain) ADT = • collection of abstract Operations (routines that manipulate the values) 74 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  75. 75. Queue is an ADT values package Queues is package Queues is type Queue is limited private; type Queue is limited private; procedure Add (Q ::Queue; Element ::Integer); procedure Add (Q Queue; Element Integer); function First (Q ::Queue) return Integer; function First (Q Queue) return Integer; function Get (Q :: Queue) return Integer; function Get (Q Queue) return Integer; function Empty (Q ::Queue) return Boolean; function Empty (Q Queue) return Boolean; private private type Queue is …; type Queue is …; operations end Queues; end Queues; 75 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  76. 76. Objects & Variables • OBJECT = instance of an ADT – piece of memory containing values of the ADT • VARIABLE = name of a specific object • not all objects have a name 76 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  77. 77. an object My_Q : Queue; Queue object name of the object Memory 77 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  78. 78. not all objects have a name type Queue_Ptr is access Queue; Queue object Ptr : Queue_Ptr; Ptr := new Queue; object has no name Ptr is just a pointer to the object Memory 78 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  79. 79. CLASS = ADT + inheritance 79 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  80. 80. operation invoke some operation in the SPEC operation to use ADT services operation Private data ENCAPSULATION 80 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  81. 81. The object model 81 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  82. 82. The C model GLOBAL DATA C module C module C module 82 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  83. 83. • Programming in the Large – specification & implementation – privacy – abstract data types – hierarchical packages 83 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  84. 84. To add functionality ... function Last (Q : Queue) return Integer; package Queues is type Queue is private; procedure Add (Q : Queue; Element : Integer); function First (Q : Queue) return Integer; function Get (Q : Queue) return Integer; function Empty (Q : Queue) return Boolean; function Last (Q : Queue) return Integer; private type Queue is …; Must add end Queues; it to Queues Queue is a private type 84 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  85. 85. But ... • Every time you change a spec you must recompile all its clients • Every time you change a module you must RETEST the whole module 85 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  86. 86. Solution: use child units queues.ads package Queues is type Queue is private; procedure Add (Q : Queue; Element : Integer); function First (Q : Queue) return Integer; function Get (Q : Queue) return Integer; function Empty (Q : Queue) return Boolean; private type Queue is …; Child end Queues; subprogram queues-last.ads function Queues . Last (Q : Queue) return Integer; 86 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  87. 87. Child Units Rules • The body or private part of a child unit can see the private part of all of its parents • The spec of a child unit does NOT 87 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  88. 88. Using a child unit with Queues; use Queues; with Queues.Last; procedure Client is Q : Queue; X : Integer; begin Add (Q, 123); Add (Q, 3); X := Queues.Last (Q); end Client; 88 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  89. 89. queues.ads package Queues is type Queue is private; procedure Add (Q : Queue; Element : Integer); function First (Q : Queue) return Integer; function Get (Q : Queue) return Integer; function Empty (Q : Queue) return Boolean; private type Queue is …; end Queues; Child package queues-new_functionality.ads package Queues . New_Functionality is function Last (Q : Queue) return Integer; end Queues . New_Functionality 89 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  90. 90. with Queues; use Queues; with Queues.New_Functionality; procedure Client is Q : Queue; X : Integer; begin Add (Q, 123); Add (Q, 3); X := Queues.New_Functionality.Last (Q); end Client; 90 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×