27/02/2006l3 miage - glo ada1 les paquetages permettent d’encapsuler un groupe d’entités...
TRANSCRIPT
27/02/2006 L3 MIAGE - GLO ADA 1
Les Paquetages
• Permettent d’encapsuler un groupe d’entités logiquement reliées.
• Comme toute unité de programme, le paquetage consiste en une spécification et un corps.
• L’utilisateur du paquetage n ’a accès qu’à la spécification.
• Machine abstraite & Type Abstrait
27/02/2006 L3 MIAGE - GLO ADA 2
Machine Abstraite :Pile (LIFO)
Principe :
elem1
?????
elem3
elem2
?????
?????
?????
elem1
elem4
elem3
elem2
?????
?????
?????
elem1
elem4
elem3
elem2
?????
?????
?????
Pro
fond
eur
TopTop
Top
Push(elem4) Pop
(elem4)
27/02/2006 L3 MIAGE - GLO ADA 3
Implémentation Naïve
-- LA PILE
function Is_Empty return Boolean;
function Top return Float;
procedure Push (X : in Float);
procedure Pop;
-- Réalisation de la pile :
Max_Size : constant Integer := 10;
Items : array (1 .. Max_Size) of Float;
Index_Of_Top : Integer range 0 .. Max_Size := 0;
with Io;
procedure Main is
-- Corps des sous-programmes --
a, b : float := 0.0; -- Les 2 variables sont initialisées !!
begin
a := 1.0;
b := 2.0;
push(a);
push(b);
a := top;
pop;
Io.put(a);
b := top;
pop;
Io.put(b);
end Main;
27/02/2006 L3 MIAGE - GLO ADA 4
Encapsulation
package Stack is
function Is_Empty return Boolean;
function Top return Float;
procedure Push (X : in Float);
procedure Pop;
Max_Size : constant Integer := 10;
Items : array (1 .. Max_Size) of Float;
Index_Of_Top : Integer range 0 .. Max_Size := 0;
end Stack;
with Io;
procedure Main is
a, b : float := 0.0; -- Les 2 variables sont initialisées !!
begin
a := 1.0;
b := 2.0;
Stack.push(a);
Stack.push(b);
a := Stack.top;
Stack.pop;
Io.put(a);
b := Stack.top;
Stack.pop;
Io.put(b);
end Main;
package body Stack is
-- Déclarations locales --
-- ATTENTION : Spécification --
-- Corps des sous-programmes --
end Stack;
Spéc
ific
atio
nC
orps
27/02/2006 L3 MIAGE - GLO ADA 5
Danger de cette Implémentation
package Stack is
function Is_Empty return Boolean;
function Top return Float;
procedure Push (X : in Float);
procedure Pop;
Max_Size : constant Integer := 10;
Items : array (1 .. Max_Size) of Float;
Index_Of_Top : Integer range 0 .. Max_Size := 0;
end Stack;
with Io;
procedure Main is
begin
Stack.Index_Of_Top :=1; -- DANGER !!!
...
end Main;
package body Stack is
-- Déclarations locales --
-- ATTENTION : Spécification --
-- Corps des sous-programmes --
end Stck;
Spéc
ific
atio
nC
orps
-- Compilation OK
27/02/2006 L3 MIAGE - GLO ADA 6
Masquage de l ’Information
package Stack is
function Is_Empty return Boolean;
function Top return Float;
procedure Push (X : in Float);
procedure Pop;
end Stack;
with Io;
procedure Main is
begin
Stack.Index_Of_Top :=1; -- ERREUR !!!
end Main;package body Stack is
Max_Size : constant Integer := 10;
Items : array (1 .. Max_Size) of Float;
Index_Of_Top : Integer range 0 .. Max_Size := 0;
-- Corps des sous-programmes --
end Stack;
Spéc
ific
atio
nC
orps
-- Compilation KO
27/02/2006 L3 MIAGE - GLO ADA 7
Premier Bilan
• La partie spécification du paquetage, à l’instar de celle d’un sous-programme, est visible du client.
• La partie corps, à l’instar de celle d’un sous-programme, ne l’est pas.
• On ne rend visible que le strict nécessaire en rejetant le maximum de choses dans le corps.
27/02/2006 L3 MIAGE - GLO ADA 8
Traitement des exceptions
with Io;
procedure Main is
a : array (1..4) of float := (11,22,33,44) ;
Begin
begin
a(0) := 2.5;
exception
when Constraint_Error => Io.put ("C_E");
end;
for I in a’range loop
Stack.push(a);
end loop;
exception
when Stack.FULL_STACK => Io.put(" Pile Pleine ");
end Main;
package body Stack is
Max_Size : constant Integer := 3;
...
procedure Push (X : in Float) is
begin
if Index_Of_Top = Max_Size then
raise FULL_STACK;
end if;
Index_Of_Top := Index_Of_Top + 1;
Items (Index_Of_Top) := X;
end Push;
end Stack;
Cor
ps
package Stack is
function Is_Empty return Boolean;
function Top return Float;
procedure Push (X : in Float);
procedure Pop;
FULL_STACK : exception ;
end Stack;
Spéc
ific
atio
n
27/02/2006 L3 MIAGE - GLO ADA 9
Unités de Bibliothèque
• Une unité de biliothèque est une partie de code que l’on peut compiler individuellement. Il s’agit le plus souvent de sous-programmes ou de paquetages.
• Elle est en général écrite dans des fichiers distincts de celui du programme principal:
– fichier.ads : Spécification
– fichier.adb : Corps
• On fait appel aux éléments de cette unité avec la clause with
27/02/2006 L3 MIAGE - GLO ADA 10
Unités de Bibliothèque
package Stack is
function Is_Empty return Boolean;
function Top return Float;
procedure Push (X : in Float);
procedure Pop;
end Stack;
package body Stack is
Max_Size : constant Integer := 10;
Items : array (1 .. Max_Size) of Float;
Index_Of_Top : Integer range 0 .. Max_Size := 0;
-- Corps des sous-programmes --
end Stack;
Stac
k.ad
sSt
ack.
adb
with Io;
with Stack; -- !!!!!
procedure Main is
a, b : float := 0.0; -- Les 2 variables sont initialisées !!
begin
a := 1.0;
b := 2.0;
Stack.push(a); -- !!!!!!!!!!!!
Stack.push(b);
a := Stack.top;
Stack.pop;
Io.put(a);
b := Stack.top;
Stack.pop;
Io.put(b);
end Main; Mai
n.ad
b
Il suffit de lancer la compilation de Main.adb !!
27/02/2006 L3 MIAGE - GLO ADA 11
Type Abstrait
L’utilisateur peut vouloir manipuler un type « Pile »– Pour gérer un tableau de piles
– Pour passer des piles en paramètre de sous-programmes
Il faut lui fournir un type « Pile » et les sous-programmes de manipulation associés : pop, push, etc...
27/02/2006 L3 MIAGE - GLO ADA 12
1ère Idée
package Stack is
Max_Size : constant Integer := 10;
type Vector is array (1 .. Max_Size) of Float;
type Pile is record
Items : Vector;
Index_Of_Top : Integer range 0 .. Max_Size := 0;
end record;
function Is_Empty(S : Pile) return Boolean;
function Top(S : Pile) return Float;
procedure Push (X : in Float, S : in out Pile);
procedure Pop(S : in out Pile);
end Stack;
Stac
k.ad
s
Quel est le problème ?
27/02/2006 L3 MIAGE - GLO ADA 13
Solution
package Stack is
type Pile is private;
function Is_Empty(S : Pile) return Boolean;
function Top(S : Pile) return Float;
procedure Push (X : in Float, S : in out Pile);
procedure Pop(S : in out Pile);
Stac
k.ad
s
Intérêt :L’utilisation du type estcomplètement indépendantedu choix d ’implémentationde ce type.
end Stack;
private
Max_Size : constant Integer := 10;
type Vector is array (1 .. Max_Size) of Float;
type Pile is record
Items : Vector;
Index_Of_Top : Integer range 0 .. Max_Size := 0;
end record;On y reviendra…
27/02/2006 L3 MIAGE - GLO ADA 14
Introduction à la Généricité
generic
Max_Size : integer;
package Stack is
function Is_Empty return Boolean;
function Top return Float;
procedure Push (X : in Float);
procedure Pop;
end Stack;
package body Stack is
-- Plus de déclaration de Max_Size
Items : array (1 .. Max_Size) of Float;
Index_Of_Top : Integer range 0 .. Max_Size := 0;
-- Corps des sous-programmes --
end Stack;
Stac
k.ad
sSt
ack.
adb
with Io;
with Stack;
procedure Main is
package myStack is new Stack(20);
a, b : float := 0.0; -- Les 2 variables sont initialisées !!
begin
a := 1.0;
b := 2.0;
myStack.push(a); -- !!!!!!!!!!!!
myStack.push(b);
a := myStack.top;
myStack.pop;
Io.put(a);
b := myStack.top;
myStack.pop;
Io.put(b);
end Main; Mai
n.ad
b
Tout le traitement de la pile ne dépend pas de sa profondeur
27/02/2006 L3 MIAGE - GLO ADA 15
Type Générique
generic
type genType is private
Max_Size : integer;
package Stack is
function Is_Empty return Boolean;
function Top return genType;
procedure Push (X : in genType);
procedure Pop;
end Stack;
package body Stack is
Items : array (1 .. Max_Size) of genType;
Index_Of_Top : Integer range 0 .. Max_Size := 0;
-- Corps des sous-programmes --
end Stack;
Stac
k.ad
sSt
ack.
adb
with Io;
with Stack;
procedure Main is
package IntegerStack is new Stack(integer,20);
package CharacterStack is new Stack(character,10);
begin
IntegerStack.Push (1);
IntegerStack.Push (2);
CharacterStack.Push ('A');
CharacterStack.Push ('B');
Io.Put (IntegerStack.Top);
Io.Put (CharacterStack.Top);
end Main; Mai
n.ad
b
Tout le traitement de la pile ne dépend pas du type des éléments
27/02/2006 L3 MIAGE - GLO ADA 16
Généricité : à retenir...
La généricité se construit en 4 étapes :
– Déclaration des paramètres génériques
– Réalisation de l’objet générique en s’appuyant sur les paramètres génériques
– Instantiation de l’objet générique
objet non-générique.
– Utilisation de l’objet non-générique dans un corps.