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.

246 Polymorphism<br />

16.10.2 An observer for the class Board<br />

The specification for the class Display_Board that will display a representation of the board conta<strong>in</strong>s the<br />

s<strong>in</strong>gle method Update that has responsibility for display<strong>in</strong>g a representation of the board on a text output<br />

device.<br />

with Class_Observe_Observer, Class_Board, <strong>Ada</strong>.Text_Io;<br />

use Class_Observe_Observer, Class_Board, <strong>Ada</strong>.Text_Io;<br />

package Class_Display_Board is<br />

type Display_Board is new Observer with private;<br />

procedure Update( The:<strong>in</strong> Display_Board; B:<strong>in</strong> Observable'Class );<br />

private<br />

type Display_Board is new Observer with record<br />

null;<br />

end record;<br />

end Class_Display_Board;<br />

The implementation of the class Display_Board is shown below. The ma<strong>in</strong> po<strong>in</strong>t of <strong>in</strong>terest is the conversion<br />

of the second parameter B <strong>in</strong>to an <strong>in</strong>stance of a Board. This is required as the object is passed as an <strong>in</strong>stance of<br />

the class Observable. This down conversion will be checked to make sure that this object is an <strong>in</strong>stance of Board.<br />

package body Class_Display_Board is<br />

procedure Update( The:<strong>in</strong> Display_Board; B: <strong>in</strong> Observable'Class ) is<br />

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

for I <strong>in</strong> 1 .. 9 loop<br />

Put( Cell( Board(B), I ) ); --Its really a Board<br />

case I is<br />

--after pr<strong>in</strong>t<strong>in</strong>g counter<br />

when 3 | 6 => -- pr<strong>in</strong>t Row Separator<br />

New_L<strong>in</strong>e; Put("---------"); --<br />

New_L<strong>in</strong>e;<br />

when 9 => -- pr<strong>in</strong>t new l<strong>in</strong>e<br />

New_L<strong>in</strong>e;<br />

when 1 | 2 | 4 | 5 | 7 | 8 => -- pr<strong>in</strong>t Col separator<br />

Put(" | ");<br />

end case;<br />

end loop;<br />

end Update;<br />

end Class_Display_Board;<br />

16.10.3 The driver code for the program of nought and crosses<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!