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.

242 Polymorphism<br />

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

type Observable is tagged private;<br />

type P_Observer is access all Observer'Class;<br />

procedure Add_Observer ( The:<strong>in</strong> out Observable;<br />

O:<strong>in</strong> P_Observer );<br />

procedure Delete_Observer( The:<strong>in</strong> out Observable;<br />

O:<strong>in</strong> P_Observer );<br />

procedure Notify_Observers( The:<strong>in</strong> out Observable'Class );<br />

procedure Set_Changed( The:<strong>in</strong> out Observable );<br />

Note:<br />

Observers are manipulated by us<strong>in</strong>g their access values, rather than the <strong>in</strong>stance of the observer object<br />

directly.<br />

16.9.3 Putt<strong>in</strong>g it all together<br />

The complete class specifications for the observer and observable classes are comb<strong>in</strong>ed <strong>in</strong>to a s<strong>in</strong>gle package as<br />

follows:<br />

package Class_Observe_Observer is<br />

type Observable is tagged private;<br />

type Observer is tagged private;<br />

type P_Observer is access all Observer'Class;<br />

procedure Add_Observer ( The:<strong>in</strong> out Observable;<br />

O:<strong>in</strong> P_Observer );<br />

procedure Delete_Observer( The:<strong>in</strong> out Observable;<br />

O:<strong>in</strong> P_Observer );<br />

procedure Notify_Observers( The:<strong>in</strong> out Observable'Class );<br />

procedure Set_Changed( The:<strong>in</strong> out Observable );<br />

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

private<br />

Max_Observers : constant := 10;<br />

subtype Viewers_Range is Integer range 0 .. Max_Observers;<br />

subtype Viewers_Index is Viewers_Range range 1 .. Max_Observers;<br />

type Viewers_Array is array( Viewers_Index ) of P_Observer;<br />

type Observable is tagged record<br />

Viewers : Viewers_Array := ( Others => null );<br />

Last : Viewers_Range := 0;<br />

State_Changed : Boolean := True;<br />

end record;<br />

type Observer is tagged null record;<br />

end Class_Observe_Observer;<br />

Note:<br />

Unfortunately as the methods of the classes Observer and Observable are mutually<br />

<strong>in</strong>terdependent it is not possible to easily separate this package <strong>in</strong>to two dist<strong>in</strong>ct packages.<br />

The implementation of the package Class_Observe_Observer is as follows:<br />

package body Class_Observe_Observer is<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!