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.

74 Packages as classes<br />

Likewise <strong>in</strong> the software world, there are objects that a user or programmer can make effective use of without<br />

hav<strong>in</strong>g to know how the object has been implemented. On a very simple level an <strong>Ada</strong> program may declare<br />

objects to hold float<strong>in</strong>g po<strong>in</strong>t numbers, which can then be used with arithmetic operations to sum, multiply, etc.<br />

these values. Most programmers however, do not know the exact details of how these operations are performed;<br />

they accept the <strong>in</strong>terface provided by the programm<strong>in</strong>g language.<br />

At one po<strong>in</strong>t it was fashionable for programm<strong>in</strong>g languages to provide a rich set of data types. The designers<br />

of these languages hoped the data types provided would be adequate for all occasions. The problem was, and still<br />

is, that no one language could ever hope to provide all the different types of item that a programmer may need or<br />

wish to use.<br />

<strong>Ada</strong> gives a programmer the ability to declare new data types, together with a range of operations that may be<br />

performed on an <strong>in</strong>stance of the type. Naturally, a programmer may also use types and operations on these types<br />

that have been def<strong>in</strong>ed by other programmers.<br />

6.2 <strong>Object</strong>s, messages and methods<br />

A car can be thought of as an object. The car conta<strong>in</strong>s complex details and processes that are hidden from the<br />

driver. For example, to make the car go faster the driver presses the accelerator pedal. The car receives the<br />

message ‘go faster’ and evokes an <strong>in</strong>ternal method to speed up the eng<strong>in</strong>e.<br />

In the above description of driv<strong>in</strong>g a car many object-<strong>oriented</strong> ideas have been used. These ideas are as<br />

follows:<br />

object<br />

message<br />

method<br />

An item that has a hidden <strong>in</strong>ternal structure. The hidden structure<br />

is manipulated or accessed by messages sent by a user.<br />

A request sent to the object to obey one of its methods.<br />

A set of actions that manipulates or accesses the <strong>in</strong>ternal state of<br />

the object. The detail of these actions is hidden from a user of the<br />

object.<br />

6.3 <strong>Object</strong>s, messages and methods <strong>in</strong> <strong>Ada</strong><br />

In <strong>Ada</strong> an object is an <strong>in</strong>stance of either a user-def<strong>in</strong>ed type or an <strong>in</strong>stance of one of the <strong>in</strong>-built types.<br />

An object for a user-def<strong>in</strong>ed type can be imag<strong>in</strong>ed diagrammatically as Figure 6.2.<br />

Data<br />

Method 1<br />

Messages sent to an object<br />

which evoke methods that<br />

access the <strong>in</strong>ternal hidden<br />

data<br />

Method 2<br />

Method 3<br />

Figure 6.2 Diagrammatic representation of an object.<br />

A message is implemented as either a procedure or function call, the body of which is the method that is<br />

evoked when the message is sent to the object. The user of the object has no knowledge of the implementation<br />

code conta<strong>in</strong>ed <strong>in</strong> the body of the procedure or function.<br />

Note:<br />

The idea of b<strong>in</strong>d<strong>in</strong>g code and data together <strong>in</strong> a unit that does not allow direct access to the data is<br />

often referred to as encapsulation.<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!