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.

Polymorphism 241<br />

Screen<br />

Observed<br />

Observer<br />

oxo<br />

oxo_observer<br />

Sqrs : Board_Array<br />

Figure 16.4 Oxo observer and observed oxo board.<br />

The observed object oxo is unaware of how its representation will be displayed, whilst the observer object<br />

oxo_observer is unaware of how the observed object oxo represents and manipulates the board.<br />

16.9.1 The Observer’s responsibilities<br />

An observer class for an object is required to <strong>in</strong>herit from the type Observer and override the method Update<br />

with code that will display the state of the observed object.<br />

Method<br />

Update<br />

Responsibility<br />

Display the state of the observed object.<br />

An implementor of an Observer class overrides the method Update with a method that displays an<br />

appropriate representation of the observed object passed as a formal parameter to the method. The method<br />

Update is called when the state of the object be<strong>in</strong>g observed changes and an update of the representation of the<br />

objects state is required.<br />

The <strong>Ada</strong> specification of this responsibility is:<br />

type Observer<br />

is tagged private;<br />

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

Note:<br />

The object be<strong>in</strong>g observed (What) is passed as it base representation. This object will need to be<br />

converted back <strong>in</strong>to its true type before it can be <strong>in</strong>terrogated.<br />

16.9.2 The responsibilities of the observable object<br />

An observable class for an object is required to <strong>in</strong>herit from the type Observable so that the follow<strong>in</strong>g methods<br />

may be made available to it.<br />

Method<br />

Add_Observer<br />

Delete_Observer<br />

Notify_Observers<br />

Set_Changed<br />

Responsibility<br />

Add an observer to the observable object.<br />

Removes an observer.<br />

If the object has changed, tells all observers to update<br />

their view of the object.<br />

Sets a flag to <strong>in</strong>dicate that the object has changed.<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!