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.

240 Polymorphism<br />

with <strong>Ada</strong>.Tags;<br />

use <strong>Ada</strong>.Tags;<br />

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

Withdrawals_In_A_Week : constant Natural := 3;<br />

subtype Money is Float;<br />

type Account is tagged record<br />

Balance_Of : Money := 0.00; --Amount <strong>in</strong> account<br />

end record;<br />

type Account_Ltd is new Account with record<br />

Withdrawals : Natural := Withdrawals_In_A_Week;<br />

end record;<br />

Normal : Account;<br />

Restricted : Account_Ltd;<br />

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

Normal := ( Balance_Of => 20.0 );<br />

Restricted := ( Normal with 4 );<br />

Restricted := ( Normal with Withdrawals => 4 );<br />

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

Note:<br />

with is used to extend normal, an <strong>in</strong>stance of an Account, <strong>in</strong>to restricted, an <strong>in</strong>stance of an<br />

Account_Ltd.<br />

The components may be named:<br />

restricted := ( normal with withdrawals => 4 );<br />

If there are no additional components, with null record is used to form the extension.<br />

16.9 The observe-observer pattern<br />

A danger <strong>in</strong> writ<strong>in</strong>g any program is that <strong>in</strong>put and output of data values become entangled <strong>in</strong> the body of the code<br />

of the program. When this happens the program becomes less easy to ma<strong>in</strong>ta<strong>in</strong> and will require major changes if<br />

the format of the <strong>in</strong>put or output changes. By separat<strong>in</strong>g the <strong>in</strong>put and output from the functionality of the program<br />

allows a cleaner solution to be formulated.<br />

The model-view paradigm <strong>in</strong> essence consists of:<br />

• An observer: An object that has responsibility for display<strong>in</strong>g a representation of another<br />

object.<br />

• The observed: An object that has one or more observers who will display the state of the<br />

object.<br />

In the game of noughts and crosses, for example, the observed object would be the object represent<strong>in</strong>g the<br />

board. The observer would be an object that has responsibility for display<strong>in</strong>g a representation of the board onto<br />

the computer screen. There could be several implementations of the observer, depend<strong>in</strong>g on how the <strong>in</strong>formation<br />

is to be displayed. For example, the observer may be implemented to display the board as a:<br />

• Textual representation: When a console application is written.<br />

• As a graphical image: When an application with a graphical <strong>in</strong>terface is developed.<br />

This separation of responsibility is illustrated <strong>in</strong> Figure 16.4. In which the observed object oxo is <strong>in</strong>terrogated<br />

by the object oxo_observer so that it can display a graphical representation of the noughts and crosses board<br />

on the computer screen.<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!