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.

244 Polymorphism<br />

The method Update is overridden by an observed with appropriate code to display the state of the observable<br />

object. This object is passed as the second parameter to the method.<br />

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

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

null;<br />

--Should be overridden<br />

end Update;<br />

end Class_Observe_Observer;<br />

Note:<br />

The parameter What is of type Observable'Class so that re-dispatch<strong>in</strong>g can take place.<br />

16.10 Us<strong>in</strong>g the observe-observer pattern<br />

The follow<strong>in</strong>g is an implementation of the game of noughts and crosses us<strong>in</strong>g the observe-observer pattern.<br />

16.10.1 The observed board object<br />

The class Board that implements the board for the game of noughts and crosses is now def<strong>in</strong>ed as:<br />

with Class_Observe_Observer;<br />

use Class_Observe_Observer;<br />

package Class_Board is<br />

type Board is new Observable with private;<br />

type Game_State is ( W<strong>in</strong>, Playable, Draw );<br />

procedure Add( The:<strong>in</strong> out Board; Pos:<strong>in</strong> Integer;<br />

Piece:<strong>in</strong> Character );<br />

function Valid( The:<strong>in</strong> Board; Pos:<strong>in</strong> Integer ) return Boolean;<br />

function State( The:<strong>in</strong> Board ) return Game_State;<br />

function Cell( The:<strong>in</strong> Board; Pos:<strong>in</strong> Integer ) return Character;<br />

procedure Reset( The:<strong>in</strong> out Board );<br />

private<br />

subtype Board_Index is Integer range 1 .. 9;<br />

type Board_Array is array( Board_Index ) of Character;<br />

type Board is new Observable with record<br />

Sqrs : Board_Array := ( others => ' '); --Initialize<br />

Moves : Natural := 0;<br />

end record;<br />

end Class_Board;<br />

Note: Apart from <strong>in</strong>herit<strong>in</strong>g from the class Observable, this code is identical to that seen <strong>in</strong> Section 8.4.1.<br />

The implementation of the class Board is def<strong>in</strong>ed <strong>in</strong> the body of the package Class_Board as follows:<br />

package body Class_Board is<br />

The procedure add adds a counter either the character 'X'or 'O' to the board.<br />

procedure Add( The:<strong>in</strong> out Board; Pos:<strong>in</strong> Integer;<br />

Piece:<strong>in</strong> Character ) is<br />

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

The.Sqrs( Pos ) := Piece;<br />

end Add;<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!