29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

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.

13.4.2 Reading from BOSS<br />

You c<strong>an</strong> either load the whole contents of a BOSS file, read individual objects from a BOSS file (or if<br />

you know the position of the object you are interested in) access a single object from <strong>an</strong>y position in the<br />

file. This last technique is much faster th<strong>an</strong> reading n - 1 objects to get to the nth object, but it does<br />

assume that you know that the object you are interested in is the nth object.<br />

The procedure to follow is presented below.<br />

1. First create a data stream to be used to read the BOSS file. This is typically a chieved by send a<br />

readStream message to a file name which represents the name of the BOSS file.<br />

2. Now create a BOSS object by sending <strong>an</strong> onOld: creation message with the newly created data<br />

stream as the parameter. You c<strong>an</strong> optimize this process if you know t hat you are not going to<br />

write to the BOSS file. To do this you c<strong>an</strong> use the onOldNoSc<strong>an</strong>: message (rather th<strong>an</strong> the<br />

onOld: message).<br />

3. Obtain the objects in the BOSS file using the appropriate access message (e.g. contents or<br />

next).<br />

4. Once you have finished reading from the boss file you should close it.<br />

The next message listed in item 3 above will return a single object which c<strong>an</strong> then be saved into <strong>an</strong><br />

appropriate collection / data structure etc. The contents message will return <strong>an</strong> array of objects<br />

which c<strong>an</strong> then be m<strong>an</strong>ipulated in the same way as <strong>an</strong>y other array of objects. For example:<br />

| valuesFileStream bosFileStream|<br />

valuesFileStream := bosFileName asFilename readStream.<br />

bosFileStream := Binary<strong>Object</strong>Storage onNew: valuesFileStream.<br />

aBag := Bag new.<br />

[[bosFileStream atEnd]whileFalse: [aBag add: bosFileStream next]]<br />

valueNowOrOnUnwindDo: [valuesFileStream close].<br />

This example illustrates two things, first it illustrates the equivalent of a while not end of file (EOF)<br />

style construct for BOSS files. This cons truct is the atEnd message. This message is used to test<br />

whether the end of the BOSS file has been reached. It is therefore possible to use it in a while loop<br />

which says “read while not end of file”. The other thing this illustrates is the practical use of a<br />

whileFalse: loop. M<strong>an</strong>y other l<strong>an</strong>guages would force a construct such as not (fileName EOF) onto<br />

the programmer. The whileFalse: construct is far more eleg<strong>an</strong>t.<br />

Finally, the position: message is used to position the BOSS file pointer so that a single objec t<br />

c<strong>an</strong> be read from a specific point in the BOSS file. However, a record must have been maintained of<br />

what the position is (note the third object stored is unlikely to have been stored at position 3). This<br />

message could be used to create a file based versio n of a collection class. We shall look at this in more<br />

detail in the next section <strong><strong>an</strong>d</strong> so will leave a detailed discussion of this message until then.<br />

13.4.3 Warnings about BOSS files<br />

You should only use BOSS files to store data objects <strong><strong>an</strong>d</strong> never for interface objects. This is because the<br />

objects you BOSS out will lose their reference to onscreen objects. You should also avoid BOSSing out<br />

<strong>an</strong>y objects associated with the execution machinery of VisualWorks. Finally, you should take care<br />

never to BOSS out a circular references as this c<strong>an</strong> confuse the BOSS system.<br />

13.5 Using files with the Fin<strong>an</strong>cial M<strong>an</strong>ager<br />

This application builds on that carried out in the last chapter. The aim of that Fin<strong>an</strong>cialM<strong>an</strong>ager<br />

application was to keep a record of deposits <strong>an</strong> d withdrawals from a current account. Associated with<br />

this, it kept <strong>an</strong> up to date bal<strong>an</strong>ce as well as allowing statements to be printed.<br />

A problem with that version of the application was that there was no way of perm<strong>an</strong>ently storing the<br />

account information. As soon as the Fin<strong>an</strong>cialM<strong>an</strong>ager object was destroyed (or reference to it<br />

lost) then all the information associated with it was also lost.<br />

The aim of this exercise is to extend the existing application by adding the following features:<br />

116

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

Saved successfully!

Ooh no, something went wrong!