Create a Custom Application Block That Decouples Your Code, Page 2
Turning on the Radio
Next, you need a class that represents the radio station (or broadcaster). I used the Singleton pattern to ensure there was only one broadcaster. Although you could implement multiple radio stations, using just one keeps things simple.The Broadcaster has only shared public methods and an internal reference to the typed ListenerCollection. To begin receiving messages, implement IListener and invoke Broadcaster.Add(me). To stop listening, invoke Broadcaster.Remove(me). Anyone can send messages by calling Broadcaster.Broadcast.
Broadcasting works simply by iterating through the list of listeners. If a listener's Listening property returns True, then that listener's Listen method is called. (This is pretty much how multicast delegates work too.) Listing 3 contains the complete implementation of the Broadcaster class.
Listing 3: The Broadcaster Class
Public Class Broadcaster
Private list As ListenerCollection
Private Shared this As Broadcaster = Nothing
Protected Sub New()
list = New ListenerCollection
End Sub
Protected Shared ReadOnly Property Instance() As Broadcaster
Get
If (this Is Nothing) Then this = New Broadcaster
Return this
End Get
End Property
Public Shared Function Add(ByVal value As IListener) As Integer
Return Instance.list.Add(value)
End Function
Public Shared Sub Remove(ByVal value As IListener)
Instance.list.Remove(value)
End Sub
Public Shared Sub Broadcast(ByVal message As String)
Dim listener As IListener
For Each listener In Instance.list
Try
If (listener.Listening) Then
listener.Listen(message)
End If
Catch ex As Exception
Instance.list.Remove(listener)
End Try
Next
End Sub
End Class
To complete the application block, use the sn.exe utility (sn k) to create a strong name if you want the class library to be installed in the GAC.
Finally, use the setup wizard to create a .msi file. (I won't explain how to use the wizard; I know you can figure that out.)
That's it! Distribute the .msi file and you have implemented your first application block.
Testing the Radio Application Block
To test the radio application block, use the following steps:- Create a new Windows Forms application.
- Indicate that the main form implements Ilistener.
- Make Listening return True.
- Add a statusbar to the main form and define Listen to write to the StatusBar.Text property.
- In Form_Load, call Broadcaster.Add(Me) and then call Broadcaster.Broadcast("My first application block");.
That's all there is to it. When you run the Windows Forms application, the text should appear in the StatusBar. The key here is that the Radio Application Block is reusable and has no specific knowledge of any of the listeners' types; that is, the RAB can go anywhere without dragging your applications along with it. Listing 4 contains an example Windows Form (without the automatically generated code).
Listing 4: A Sample Test Windows Forms Form
Imports ApplicationBlock.Radio
Public Class Form1
Inherits System.Windows.Forms.Form
Implements IListener
#Region [ Windows Form Designer generated code ]
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Broadcaster.Add(Me)
Broadcaster.Broadcast("This is my first application block")
End Sub
Public ReadOnly Property LIstening() As Boolean _
Implements IListener.Listening
Get
Return True
End Get
End Property
Public Sub Listen(ByVal message As String) _
Implements IListener.Listen
StatusBar1.Text = message
End Sub
End Class
Loosely Coupled Code
The Observer pattern and the Radio Application Block, which represents an implementation of the Observer pattern, are perfect examples of loosely coupled code. If you don't get it, that's okay. It's important that you try to get it, however.If someone you are working with poo-poos it as overly complex, then go ahead and feel superior. At least in this instance, you are.
About the Author
Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his upcoming book UML DeMystified from McGraw-Hill/Osborne (Spring 2005) and Expert One-on-One Visual Studio 2005 from Wrox (Fall 2005). Paul is also the founder and chief architect for Software Conceptions, Inc, founded 1990. He is available to help design and build software worldwide. You may contact him for consulting opportunities or technology questions at pkimmel@softconcepts.com.
If you are interested in joining, sponsoring a meeting, or posting a job, check out www.glugnet.org, the Web page of the Greater Lansing area Users Group for .NET.
Copyright © 2005 by Paul T. Kimmel. All Rights Reserved.
