What is a Bubble event?
Sometimes you need to handle one event on different layers of the class hierarchy. Take for example, the following case:
You need to implement a system which will receive data from the serial connection, parse it to single out several commands and handle them accordingly. The application will have to report its activity to a form. The form must present:
- a list of several connections
- the number of bytes sent and received for each connection
- last executed command for each connection
One of the possible approaches is:
- create a base class for low-level serial communication (
Connection
)
- create a class that will handle the commands and send the response back to the caller (
Communicator
)
- create a class that will contain several
Communicator
s (CommManager
)
You need to handle the following events:
OnData
It is fired when data is sent or received to/from the serial port.
OnCommand
It is fired when a command has been handled by the Communicator
.
OnError
If any errors occur in the data transmission, this event is fired by Connection
.
The OnData
event is defined in Connection
class. It is handled in Communicator
to parse the input buffer in order to determine which command to run. Also it has to be propagated to the CommManager
class, which will in turn notify its parent form that some data has been processed.
The OnError
event is defined in Connection
class too. It will notify parent classes about errors in the serial communication.
The OnCommand
event is defined in Communicator
class. It is fired when a command has been handled. It will notify the form about the last executed command.
I defined the BubbleEvent
as an event that is to be handled or propagated through several objects, either containing or aggregating others. The following implementation follows the �Chain of Responsibility� pattern.
Implementation
In order to implement the scenario, we need the following things:
This is the interface that event handlers will have to implement:
Public Interface IBubbleEventHandler
Property EventSuccessor() As IBubbleEventHandler
Function HandleBubbleEvent(ByRef evt As BubbleEvent)
Function ForwardBubbleEvent(ByRef evt As BubbleEvent)
End Interface
EventSuccessor
is a property that sets or returns the next handler in the chain. HandleBubbleEvent
is a method that either handles or calls ForwardBubbleEvent
which forwards the event to the next handler in the chain.
Public MustInherit Class BubbleEvent
Private _name As String
Private _params As ArrayList
Public ReadOnly Property Name() As String
Get
Return _name
End Get
End Property
Public ReadOnly Property Parameters() As ArrayList
Get
Return _params
End Get
End Property
Public Sub New(ByVal astrEvtName As String, _
ByRef aobjEvtSource As IBubbleEventHandler, _
ByVal ParamArray params() As Object)
Dim p
_name = astrEvtName
_params = New ArrayList()
_params.Add(aobjEvtSource)
For Each p In params
_params.Add(p)
Next
End Sub
End Class
This is what we will implement in the sample application.
The Sample
We well implement the above case in terms of abstract objects. See the demonstration project for details.
In the sample we have a base class, a child class inherited from the base class, and a child class collection. The collection will be represented by a ListView
control on a form. Whenever you select an entry in the ListView
and change Prop1
property of the selected item, you�ll see message boxes showing you the levels at which the Change
event is handled.