ada 95 - programming in the large

Post on 01-Nov-2014

1.416 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Author: Franco Gasperoni. License: GFDL

TRANSCRIPT

1http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Franco Gasperonigasperon@act-europe.frhttp://libre.act-europe.fr

2http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Copyright NoticeCopyright 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

3http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

4http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

What is the largest What is the largest software systemsoftware systemyou have built ?you have built ?

(in SLOC)(in SLOC)

5http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Small Software SystemsSmall 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)

6http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Medium/Large SystemsMedium/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

7http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large– specification & implementation– privacy– abstract data types– hierarchical packages

8http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Separate CompilationSeparate CompilationTHE

PROBLEM

Compiler objectCODE

Compiler objectCODE

Compiler objectCODE

Linker

executable

libraries

9http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Problem with this approachProblem with this approach

CODE

CODE

CODE

• No structure

• To write your own code– YOU MUST understand

everybody else’s code

CODE

CODE

10http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

IdeaIdea

SPECIFY WHAT EACH

MODULESHOULD DO

11http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

SPECIFICATION

Software module

????

BODY

12http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large– specification & implementation

• specification• implementation• specification rules in Ada

13http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

A Specification is a ...A Specification is a ...

CONTRACTCONTRACTCONTRACTCONTRACTImplementorof themodule

Users/clientsof themodule

• On the SERVICES provided by the module

14http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• SPEC = list of services provided

• BODY = implementation of the services (hidden)

Software module

Service_1Service_2Service_3

Service_1implementation

Service_2implementation

Service_3implementation

15http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

SPECIFICATION

????BODY

16http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

ExampleExample

• 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

17http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Queue isprocedure Add (Element : Integer);

function First return Integer;function Get return Integer;

function Empty return Boolean;end Queue;

package Queue isprocedure Add (Element : Integer);

function First return Integer;function Get return Integer;

function Empty return Boolean;end Queue;

queue.adsqueue.ads

18http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Queue

????

BODY

package Queue isprocedure Add (Element : Integer);

function First return Integer;function Get return Integer;

function Empty return Boolean;end Queue;

package Queue isprocedure Add (Element : Integer);

function First return Integer;function Get return Integer;

function Empty return Boolean;end Queue;

queue.adsqueue.ads

19http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Using package QueueUsing package Queue

with Queue;procedure Client is

Queue_Error : exception;X : Integer;

beginQueue.Add (3);Queue.Add (4);

if not Queue.Empty thenX := Queue.Get;

elseraise Queue_Error;

end if;end Client;

client.adb

20http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Specifications Reduce ComplexitySpecifications Reduce Complexity

SPECSPEC

SPECSPEC

SPECSPECSPECSPEC

• To write your own code– only need to understand

specs for the services you need

21http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Queue????

package Queue isprocedure Add (Element : Integer);

function First return Integer;function Get return Integer;

function Empty return Boolean;end Queue;

package Queue isprocedure Add (Element : Integer);

function First return Integer;function Get return Integer;

function Empty return Boolean;end Queue;

queue.adsqueue.ads

To write Client only need to look atTo write Client only need to look at

22http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Aside: use clauseAside: use clause

with Queue; use Queue;procedure Client is

Queue_Error : exception;X : Integer;

beginQueue. Add (3);Queue. Add (4);

if not Queue. Empty thenX := Queue. Get;

elseraise Queue_Error;

end if;end Client;

23http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large– specification & implementation

• specification• implementation• specification rules in Ada

24http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

ONEONEONEONEONEONEONEONE possible possible implementationimplementation

of packageof packageQueueQueue

This implementationThis implementationraises Constraint_Errorraises Constraint_Errorif more than Max_Sizeif more than Max_Size

elements are put in the Queue.elements are put in the Queue.

25http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Circular BufferCircular Buffer

Q

0 1 Max_Size - 1

Q_Last Q_First

26http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package body Queue isMax_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) isbegin

Q (Q_Last) := Element;Q_Last := Q_Last + 1;Size := Size + 1;

end Add;...

end Queue;

queue.adb

27http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package body Queue is...function First return Integer isbegin

return Q (Q_First);end First;

function Get return Integer isbegin

Q_First := Q_First + 1;Size := Size - 1;return Q (Q_First - 1);

end Get;

function Empty return Boolean isbegin

return Size = 0;end Empty;

end Queue;

queue.adb

28http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

ANOTHERANOTHERANOTHERANOTHERANOTHERANOTHERANOTHERANOTHER possible possible implementationimplementation

of packageof packageQueueQueue

29http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Linked ListLinked List

Q_LastQ_First

Free

30http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package body Queue istype Queue_Element;type Element_Ptr is access Queue_Element;

type Queue_Element is recordVal : Integer;Next : Element_Ptr;

end record;

Q_First : Element_Ptr;Q_Last : Element_Ptr;

Free : Element_Ptr := new Queue_Element;...

end Queue;

queue.adb

31http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package body Queue is...procedure Add (Element : Integer) isbegin

if Q_First = null thenQ_First := Free;

elseQ_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;...

end Queue;

queue.adb

32http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

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;

queue.adb

33http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package body Queue is...function First return Integer isbegin

return Q_First;end First;

function Empty return Boolean isbegin

return Q_First = null;end Empty;

end Queue;

queue.adb

34http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

A Spec can have several A Spec can have several implementationsimplementations

• Can change implementation

• WITHOUT having to change ANY of the client’s code

package Queue isprocedure Add (Element : Integer);

function First return Integer;function Get return Integer;

function Empty return Boolean;end Queue;

package Queue isprocedure Add (Element : Integer);

function First return Integer;function Get return Integer;

function Empty return Boolean;end Queue;

queue.adsqueue.ads

firstimplement.

firstimplement.

secondimplement.second

implement.

35http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large– specification & implementation

• specification• implementation• specification rules in Ada

36http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

In AdaIn Ada

• Spec always checked against implementation

• Must with the specs that you are going to use (not in C)

• Packages provide multiple name spaces

37http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Spec is checked against its bodySpec is checked against its body

package Queue isprocedure Add (Element : Integer);...

end Queue;

package Queue isprocedure Add (Element : Integer);...

end Queue;

package body Queue is...procedure Add (Element : Integer; X : Float) is

...end Add;...

end Queue;

package body Queue is...procedure Add (Element : Integer; X : Float) is

...end Add;...

end Queue;

Compilationerror

38http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Must with Specs usedMust with Specs used

with Queue;procedure Client is

...begin

Queue.Add (3);...

end Client;

Compilationerror

39http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Multiple Name SpacesMultiple Name Spacespackage Queue is

procedure Add (E : Integer);...

end Queue;

package Queue isprocedure Add (E : Integer);...

end Queue;

package Set isprocedure Add (E : Integer);...

end Set;

package Set isprocedure Add (E : Integer);...

end Set;

with Queue;with Set;procedure Client isbegin

Queue.Add (3);

Set.Add (99);end Client;

40http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Use Clause and AmbiguitiesUse Clause and Ambiguitiespackage Queue is

procedure Add (E : Integer);...

end Queue;

package Queue isprocedure Add (E : Integer);...

end Queue;

package Set isprocedure Add (E : Integer);...

end Set;

package Set isprocedure Add (E : Integer);...

end Set;

with Queue; use Queue;with Set; use Set;procedure Client isbegin

Add (123);end Client;

Compilationerror

ambiguity

41http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

But … Ada has overloadingBut … Ada has overloadingpackage Queue is

procedure Add (E : Integer);procedure Add (E : Float);...

end Queue;

package Queue isprocedure Add (E : Integer);procedure Add (E : Float);...

end Queue;

with Queue; use Queue;

procedure Client isbegin

Add (123);

Add (3.141);end Client;

42http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large– specification & implementation– privacy– abstract data types– hierarchical packages

43http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Queues istype Queue is …;

procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

end Queues;

package Queues istype Queue is …;

procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

end Queues;

Having Several QueuesHaving Several Queues

44http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Queues is

type Queue is …;

end Queues;

package Queues is

type Queue is …;

end Queues;

!!! WARNING !!!!!! WARNING !!!

Use Different names

45http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Using Several QueuesUsing Several Queues

with Queues; use Queues;procedure Client is

Q1 : Queue;Q2 : Queue;

beginAdd (Q1, 123);

Add (Q2, 3);

Add (Q2, Get (Q1));end Client;

46http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

One possible implementation ...One possible implementation ...

package Queues istype Queue is …;

procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

end Queues;

type Q_Element;type Element_Ptr is

access Queue_Element;

type Queue_Element is recordVal : Integer;Next : Element_Ptr;

end record;

type Queue is recordFirst : Element_Ptr;Last : Element_Ptr;

end record;

type Q_Element;type Element_Ptr is

access Queue_Element;

type Queue_Element is recordVal : Integer;Next : Element_Ptr;

end record;

type Queue is recordFirst : Element_Ptr;Last : Element_Ptr;

end record;

47http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Client code allowed to depend Client code allowed to depend on the implementation !on the implementation !

with Queues; use Queues;procedure Client is

Q1 : Queue;Q2 : Queue;

beginAdd (Q1, 123);Add (Q2, 3);

Q2.Last := null;

end Client;

OK

48http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Another implementation ...Another implementation ...

package Queues istype Queue is …;

procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

end Queues;

Max_Size : constant := 100;type Q_array (Natural range <>)

of Integer;

type Queue is recordQ : Q_Array (0 .. Max_Size);First : Natural;Last : Natural;Size : Natural;

end record;

Max_Size : constant := 100;type Q_array (Natural range <>)

of Integer;

type Queue is recordQ : Q_Array (0 .. Max_Size);First : Natural;Last : Natural;Size : Natural;

end record;

49http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

… breaks client code !… breaks client code !

with Queues; use Queues;procedure Client is

Q1 : Queue;Q2 : Queue;

beginAdd (Q1, 123);Add (Q2, 3);

Q2.Last := null;

end Client;

Compilationerror

50http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Even without changing Even without changing the implementationthe implementationthere is a there is a PROBLEMPROBLEM

with Queues; use Queues;procedure Client is

Q1 : Queue;Q2 : Queue;

beginAdd (Q1, 123);Add (Q2, 3);

Q2.Last := null;

end Client;

Q2 is in aninconsistent

state

33FirstLast

null

Q2:

51http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

You need PRIVACYYou 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

52http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

ThinkThink BIGBIG

what if what if the the QueuesQueues package package

is is used byused by 1000s1000s of other of other packagespackages

53http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• 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

54http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large

– privacy• private types• private types & discriminants• limited private types

55http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Private typesPrivate types

package Queues istype 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;

privatetype Queue is …;

end Queues;

package Queues istype 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;

privatetype Queue is …;

end Queues;

56http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

In any implementation ...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;

privatetype 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;

57http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

… private types are PRIVATE… private types are PRIVATE

with Queues; use Queues;procedure Client is

Q1 : Queue;Q2 : Queue;

beginAdd (Q1, 123);Add (Q2, 3);

Q2.Last := null;

end Client;

Compilationerror

58http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Advantages of Advantages of privateprivate typestypes

• Enforces the contract of a specification

• No client code can corrupt your data structures

• Can change implementation without changing client code

59http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Why is the private part in the spec ?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;

privatetype 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;

60http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

… because we still need to … because we still need to compile the clients codecompile the clients code

with Queues; use Queues;

procedure Client is

Q1 : Queue;

beginAdd (Q1, 123);

end Client;

61http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

… but you can make a private type … but you can make a private type quite privatequite private

package Queues istype 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;

privatetype Queue_Info;type Queue is access Queue_Info;

end Queues;

62http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package body Queues is type Queue_Element;type Element_Ptr is access Queue_Element;

type Queue_Element is recordVal : Integer;Next : Element_Ptr;

end record;type Queue_Info is record

First : Element_Ptr;Last : Element_Ptr;

end record;...

end Queues;

63http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large

– privacy• private types• private types & discriminants• limited private types

64http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Queues istype 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;

privatetype Q_array (Natural range <>) of Integer;

type Queue (Max_Size : Natural) is recordQ : Q_Array (0 .. Max_Size);First : Natural;Last : Natural;Size : Natural;

end record;end Queues;

65http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

with Queues; use Queues;procedure Client is

Q1 : Queue (100);

Q2 : Queue (250);

beginAdd (Q1, 123);

Add (Q2, 3);end Client;

Q1 can have up to 100 elmts

Q2 can have up to 250 elmts

66http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large

– privacy• private types• private types & discriminants• limited private types

67http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

with Queues; use Queues;procedure Client is

Q1 : Queue;Q2 : Queue;

X : Integer;

beginAdd (Q1, 123);Add (Q1, 3);

Q2 := Q1;

X := Get (Q2);end Client;

Does this affect Q1 ?

68http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

it depends on … the it depends on … the implementation !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

69http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

with Queues; use Queues;procedure Client is

Q1 : Queue;Q2 : Queue;X : Integer;

beginAdd (Q1, 123);

Add (Q1, 3);

Q2 := Q1;

X := Get (Q2);X := Get (Q2);end Client;

123123Q1: 33

Q2:

123123Q1:

123123Q1: 33

Q2:

Pointer implementation

Pointer Pointer implementationimplementation

70http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

limited private typeslimited private types

• NO assignment :=

• NO equality comparison =

71http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Queues istype Queue is limited 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;

privatetype Queue is …;

end Queues;

package Queues istype Queue is limited 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;

privatetype Queue is …;

end Queues;

72http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

with Queues; use Queues;procedure Client is

Q1 : Queue;Q2 : Queue;

X : Integer;

beginAdd (Q1, 123);Add (Q1, 3);

Q2 := Q1;

end Client;

COMPILATIONERROR

:= forbidden

73http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large– specification & implementation– privacy– abstract data types– hierarchical packages

74http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

QueueQueue is an is an Abstract Data TypeAbstract Data Type

(ADT)(ADT)

• Set of abstract values (data domain)

• collection of abstract Operations(routines that manipulate the values)

ADTADTADTADT ====

75http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Queue Queue is an ADTis an ADT

package Queues istype Queue is limited 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;

privatetype Queue is …;

end Queues;

package Queues istype Queue is limited 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;

privatetype Queue is …;

end Queues;operations

values

76http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Objects & VariablesObjects & 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

77http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

My_Q : Queue;

Memory

Queueobject

an object

name of the object

78http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

type Queue_Ptr is access Queue;

Ptr : Queue_Ptr;

Ptr := new Queue;

Memory

Queueobject

not all objects have a namenot all objects have a name

object has no namePtr is just a pointer to the object

79http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

CLASS = ADT+ inheritance

80http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

operation

PrivatePrivatePrivatePrivatedatadatadatadata

operation

operation

ENCAPSULATION

invoke some operationin the SPECto use ADTservices

81http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

The object modelThe object model

82http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

The C modelThe C model

C module C module C module

GLOBAL DATA

83http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large– specification & implementation– privacy– abstract data types– hierarchical packages

84http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

To add functionality ...To add functionality ...

package Queues istype 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;

privatetype Queue is …;

end Queues;

function Last (Q : Queue) return Integer;

function Last (Q : Queue) return Integer;

Must add it to

QueuesQueue is a private type

85http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

But ...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

86http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Solution: use child unitsSolution: use child units

package Queues istype 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;

privatetype Queue is …;

end Queues;

queues.ads

function Queues . Last (Q : Queue) return Integer;queues-last.ads

Child subprogram

87http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Child Units RulesChild 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

88http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Using a child unitUsing a child unit

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

Q : Queue;X : Integer;

beginAdd (Q, 123);Add (Q, 3);

X := Queues.Last (Q);end Client;

89http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Queues istype 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;

privatetype Queue is …;

end Queues;

queues.ads

package Queues . New_Functionality isfunction Last (Q : Queue) return Integer;

end Queues . New_Functionality

queues-new_functionality.ads

Child package

90http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

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

Q : Queue;X : Integer;

beginAdd (Q, 123);Add (Q, 3);

X := Queues.New_Functionality.Last (Q);end Client;

top related