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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

19 Persistence<br />

This chapter shows how to create persistent objects. A persistent object will have a life-time beyond the<br />

life-time of the program that created it.<br />

19.1 A persistent <strong>in</strong>dexed collection<br />

The life-time of an object <strong>in</strong> <strong>Ada</strong> depends on its declaration, but its life-time will never exist beyond that of the<br />

program. For an object to exist beyond the life-time of an <strong>in</strong>dividual execution of a program requires the object’s<br />

state to be saved to disk, allow<strong>in</strong>g the object’s state to be restored <strong>in</strong> another program. The above process makes<br />

the object persistent. Normally this process is visible to a programmer.<br />

For example, a program to pr<strong>in</strong>t the IDC (International Diall<strong>in</strong>g Code) for countries selected by a user could<br />

use a persistent object to hold IDC details for <strong>in</strong>dividual countries. These details could be amended by the user of<br />

the program and the changes would be reta<strong>in</strong>ed for subsequent re-runn<strong>in</strong>g of the program.<br />

A program of this k<strong>in</strong>d could uses the persistent object Tel_List that is an <strong>in</strong>stance of the class Pic<br />

(Persistent Indexed Collection). The class Pic implements a persistent <strong>in</strong>dexed collection of data items. The<br />

<strong>in</strong>dex can be an arbitrary value as can the data stored with the <strong>in</strong>dex.<br />

The responsibilities of the class PIC are as follows:<br />

Method Responsibility<br />

Initialize Initialize the object.<br />

When the object is <strong>in</strong>itialized with an identity, the state of the<br />

named persistent object is restored <strong>in</strong>to the object.<br />

F<strong>in</strong>alize If the object has an identity, save the state of the object under<br />

this name.<br />

Add<br />

Add a new data item to the object.<br />

Extract Extract the data associated with an <strong>in</strong>dex.<br />

Update Update the data associated with an <strong>in</strong>dex.<br />

Set_Name Set the identity of the object.<br />

Get_Name Return the identity of the object.<br />

The package Pack_types conta<strong>in</strong>s str<strong>in</strong>g def<strong>in</strong>itions for the Country and the IDC.<br />

package Pack_Types is<br />

subtype Country is Str<strong>in</strong>g(1 .. 12); --Country<br />

subtype Idc is Str<strong>in</strong>g(1 .. 6); --International Diall<strong>in</strong>g Code<br />

end Pack_Types;<br />

The Class Tel_List is an <strong>in</strong>stantiation of the generic class Pic.<br />

with Class_Pic, Pack_Types;<br />

use Pack_Types;<br />

pragma Elaborate_All( Class_Pic );<br />

package Class_Tel_List is new Class_Pic( Country, Idc, ">" );<br />

Note: The generic class Pic is described <strong>in</strong> Section 19.2.<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!