I think you have a little error in there:
Sub RemoveObserver(o As Observer)
// Part of the Subject interface.
// Remove this observer from our private array (if we know about it).
Var i As Integer = MyObservers.IndexOf
should have the object reference after IndexOf
Var i As Integer = MyObservers.IndexOf( o )
I also prefer a little renaming as I find this easier to understand:
Interface iReceiver
Sub NotificationReceived(sender As Variant, theMessage As Integer)
End Interface
Class TrophySystem Implements iReceiver
Sub NotificationReceived(Sender As Variant, theMessage As Integer)
Select Case theMessage
Case GOING_STRATOSPHERIC
// Code for awarding the trophy goes here...
End Select
End Sub
End Class
Interface iSender
Sub AddReceiver(Receiver As iReceiver)
Sub RemoveReceiver(Receiver As iReceiver)
Sub Notify(Sender As Variant, theMessage As Integer)
End Interface
Class PhysicsEngine Implements iSender
// The actual physics code is omitted obviously!
// Create a private array to hold our receivers.
Private MyReceivers() As iReceiver
Sub AddReceiver(Receiver As iReceiver)
// Part of the Sender interface.
// Add this receiver to our private array (only if we don't already know about it).
If MyReceivers.IndexOf( Receiver ) = -1 Then MyReceivers.AddRow( Receiver )
End Sub
Sub RemoveReceiver(Receiver As iReceiver)
// Part of the Sender interface.
// Remove this sender from our private array (if we know about it).
Var rowIndex As Integer = MyReceivers.IndexOf( Receiver )
If rowIndex <> -1 Then MyReceivers.RemoveRowAt( rowIndex )
End Sub
Sub Notify(Sender As Variant, theMessage As Integer)
// Part of the Sender interface.
// Simply invoke each Receiver's `NotificationReceived()`
// method and pass in the relevant information.
For Each Receiver As iReceiver In MyReceivers
Receiver.NotificationReceived( Sender, theMessage )
Next Receiver
End Sub
End Class