Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Implementing Bubble Events in VB.NET Windows Forms (Chain of Responsibility Pattern)

0.00/5 (No votes)
5 Jan 2004 1  
Here you will know how to implement a pattern which will allow you to handle events on different levels of the object hierarchy.

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:

  1. a list of several connections
  2. the number of bytes sent and received for each connection
  3. last executed command for each connection

One of the possible approaches is:

  1. create a base class for low-level serial communication (Connection)
  2. create a class that will handle the commands and send the response back to the caller (Communicator)
  3. create a class that will contain several Communicators (CommManager)

You need to handle the following events:

  1. OnData

    It is fired when data is sent or received to/from the serial port.

  2. OnCommand

    It is fired when a command has been handled by the Communicator.

  3. 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.

' BubbleEvent

  ' This is the base class for BubbleEvents

  ' It provdes basic event properties:

  ' Name � event ID for reference purposes

  ' Parameters collection � event parameters

 
  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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here