18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

282 Persistence<br />

19.1.1 Putt<strong>in</strong>g it all together<br />

When compiled with the class Tel_list and the package Pack_types an example <strong>in</strong>teraction us<strong>in</strong>g the<br />

program would be as follows:<br />

=UK<br />

-- Previously stored<br />

IDC for UK is 44<br />

New Zealand 64 -- Add IDC for New Zealand<br />

Sweden 46 -- Add IDC for Sweden<br />

Portugal 3510 -- Invalid IDC<br />

=Portugal -- Lookup IDC for Portugal<br />

IDC for Portugal is 3510<br />

Portugal 351 -- Try to add new IDC for Portugal<br />

Name already <strong>in</strong> directory<br />

*Portugal 351 -- Correct <strong>in</strong>valid IDC<br />

=Portugal -- Lookup IDC for Portugal<br />

IDC for Portugal is +351<br />

Note:<br />

The user’s <strong>in</strong>put is <strong>in</strong>dicated by bold type.<br />

The actions allowed are:<br />

+ Add country and IDC to collection<br />

= Extract IDC for country<br />

* Change IDC for exist<strong>in</strong>g country<br />

19.1.2 Sett<strong>in</strong>g up the persistent object<br />

The follow<strong>in</strong>g program creates the <strong>in</strong>itial persistent collection:<br />

with <strong>Ada</strong>.Text_Io, Class_Tel_List;<br />

use <strong>Ada</strong>.Text_Io, Class_Tel_List;<br />

procedure Ma<strong>in</strong> is<br />

Tel_List : Pic;<br />

beg<strong>in</strong><br />

Put("Creat<strong>in</strong>g Telephone list"); New_L<strong>in</strong>e;<br />

Set_Name( Tel_List, "tel_list.per" );<br />

Add( Tel_List, "Canada ", "+1 " );<br />

Add( Tel_List, "USA ", "+1 " );<br />

Add( Tel_List, "Netherlands ", "+31 " );<br />

Add( Tel_List, "Belgium ", "+32 " );<br />

Add( Tel_List, "France ", "+33 " );<br />

Add( Tel_List, "Gibraltar ", "+350 " );<br />

Add( Tel_List, "Ireland ", "+353 " );<br />

Add( Tel_List, "Switzerland ", "+41 " );<br />

Add( Tel_List, "UK ", "+44 " );<br />

Add( Tel_List, "Denmark ", "+45 " );<br />

Add( Tel_List, "Norway ", "+47 " );<br />

Add( Tel_List, "Germany ", "+49 " );<br />

Add( Tel_List, "Australia ", "+61 " );<br />

Add( Tel_List, "Japan ", "+81 " );<br />

end Ma<strong>in</strong>;<br />

19.2 The class PIC<br />

The class Pic (Persistent Indexed Collection) implements an <strong>in</strong>dexed collection as a b<strong>in</strong>ary tree. An identity is<br />

given to an <strong>in</strong>stance of the class PIC so that when the object’s life-time ends, its state will be saved to disk. The<br />

file name used to save the state is the object’s identity.<br />

The specification for the class PIC is as follows:<br />

© M A Smith - May not be reproduced without permission

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!