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 (
- create a class that will handle the commands and send the response back to the caller (
- create a class that will contain several
You need to handle the following events:
It is fired when data is sent or received to/from the serial port.
It is fired when a command has been handled by the
If any errors occur in the data transmission, this event is fired by
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.
OnError event is defined in
Connection class too. It will notify parent classes about errors in the serial communication.
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.
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)
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
Public ReadOnly Property Parameters() As ArrayList
Public Sub New(ByVal astrEvtName As String, _
ByRef aobjEvtSource As IBubbleEventHandler, _
ByVal ParamArray params() As Object)
_name = astrEvtName
_params = New ArrayList()
For Each p In params
This is what we will implement in the sample application.
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.
I work for Logic Software (http://www.logicsoftware.net) as a lead .NET developer/architect.