Android Code Snippet Reusable Event System

I'm new here and I created a simple class that emulates event subscribing. It's not as fancy as something in the .NET world, but it does provide a simple way to subscribe and unsubscribe multiple listeners to an "event".

B4X:
'Class module
Sub Class_Globals
    Private subscriptions As List
    Private callbacks As List
End Sub

'Initializes the event. Must be called.
Public Sub Initialize()
    subscriptions.Initialize
    callbacks.Initialize
End Sub

'Stores the infromation to create a callback (by using CallSub)
'If the target and callback combination already exist, the existing callback will be replaced with the one provided.
'
'target: The object hosting the sub to call
'callback: The string of the sub to call when this event is raised
Public Sub Subscribe(target As Object, callback As String)
    Dim index As Int = subscriptions.IndexOf(target)
    Dim addItem As Boolean
   
    addItem = index = -1
   
    If (addItem = False) Then
        If (callbacks.Get(index) <> callback) Then
            subscriptions.RemoveAt(index)
            callbacks.RemoveAt(index)
            addItem = True
        End If
    End If
   
    If (addItem) Then
        subscriptions.add(target)
        callbacks.add(callback)
    End If
End Sub

'Removes the object from the subscription list.
'
'target: The object that should be unsubscribed from the event.
Public Sub Unsubscribe(target As Object)
    Dim index As Int = subscriptions.IndexOf(target)
    If (index <> -1) Then
        subscriptions.RemoveAt(index)
        callbacks.RemoveAt(index)
    End If
End Sub

'Calls each subscriber of this event.
Public Sub Raise()
    For Each deleg As Object In subscriptions
        Dim callbackIndex As Int = subscriptions.IndexOf(deleg)
   
        CallSub(deleg, callbacks.Get(callbackIndex))
    Next
End Sub

'Calls each subscriber of this event, providing the supplied argument.
'
'arg1: The first argument to provide to each subscriber.
Public Sub Raise2(arg1 As Object)
    For Each deleg As Object In subscriptions
        Dim callbackIndex As Int = subscriptions.IndexOf(deleg)
   
        CallSub2(deleg, callbacks.Get(callbackIndex), arg1)
    Next
End Sub

'Calls each subscriber of this event, providing the supplied argument.
'
'arg1: The first argument to provide to each subscriber.
'arg2: The second argument to provide to each subscriber.
Public Sub Raise3(arg1 As Object, arg2 As Object)
    For Each deleg As Object In subscriptions
        Dim callbackIndex As Int = subscriptions.IndexOf(deleg)
   
        CallSub3(deleg, callbacks.Get(callbackIndex), arg1, arg2)
    Next
End Sub

It's simple to use. Just declare a variable for the event, add some subscribers to it, and call the appropriate raise method.

B4X:
Dim myEvent As clsEvent

myEvent.Initialize()

myEvent.Subscribe(object1, "MyMethod")
myEvent.Subscribe(object2, "MyMethod")

myEvent.Raise()

And obviously, if object1 or object2 had access to the event variable (say one declared in a module) they could subscribe to it themselves.
 
Top