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.

Polymorphism 243<br />

The method Add_Observer adds the access value of an observer to the array Viewers. The exception<br />

Constra<strong>in</strong>t_Error is raised if this operation cannot be accomplished due to lack of space.<br />

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

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

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

for I <strong>in</strong> 1 .. The.Last loop --Check for empty slot<br />

if The.Viewers( I ) = null then<br />

The.Viewers( I ) := O;<br />

--Populate<br />

return;<br />

end if;<br />

end loop;<br />

if The.Last >= Viewers_Index'Last then --Extend<br />

raise Constra<strong>in</strong>t_Error; -- Not enough room<br />

else<br />

The.Last := The.Last + 1; -- Populate<br />

The.Viewers( The.Last ) := O;<br />

end if;<br />

end Add_Observer;<br />

The <strong>in</strong>verse method Delete_Observer removes an observer for the observed object.<br />

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

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

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

for I <strong>in</strong> 1 .. The.Last loop --For each observer<br />

if The.Viewers( I ) = O then --Check if to be removed<br />

The.Viewers( I ) := null;<br />

end if;<br />

end loop;<br />

end Delete_Observer;<br />

When the model of the object (the observed) has changed and it is required to redisplay a representation of it<br />

the method Notify_Observers is called. This calls the Update method <strong>in</strong> each observer.<br />

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

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

for I <strong>in</strong> 1 .. The.Last loop --For each observer<br />

if The.Viewers( I ) /= null then -- call it's<br />

Update( The.Viewers( I ).all, The );-- update method<br />

end if;<br />

end loop;<br />

The.State_Changed := True; --<br />

end Notify_Observers;<br />

Note:<br />

The second parameter is the observed object that is to be displayed by the Update method.<br />

The method Set_Changed simply records that the state of the observed object has changed.<br />

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

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

The.State_Changed := True;<br />

end Set_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!