Publish-Subscribe Implementation (continued)
Subscribers to the
BuildCarEvent are not dependent on the EventProductionLine class. However, they must implement an event handler for the
BuildCarEvent that the EventProductionLine class raises through its helper class. The code in that event handler can extract the values from the BuildCarEventArgs instance to get information about the event. In the following example, the code updates the Hashtable containing the count of each model built, and stores it in the session:
public void BuildCarEventHandler(Object sender, BuildCarEventArgs args)
{
// the calling code links this to the BuildCarEvent so that
// it executes automatically when a new car is built
// code can query properties of custom BuildCarEventArgs
// class to get information about the event if required
String carName = args.ModelName;
// then increment count of cars built
if (partsUsed.ContainsKey(carName))
{
partsUsed[carName] = (Int32)partsUsed[carName] + 1;
}
else
{
partsUsed.Add(carName, 1);
}
// save count in session
HttpContext.Current.Session[SESSION_KEY_NAME] = partsUsed;
}
Of course, the subscriber classes will also expose methods and properties appropriate to their individual tasks. In the example application, they expose only the Hashtable containing the count of each car built so that the client code can display the results:
public Hashtable CountOfPartsUsed
{
// expose the count of notifications as a property
get { return partsUsed; }
}
In the
sample application the ASPX page named
TransferPage4.aspx demonstrates the Publish-Subscribe implementation just described. When a user clicks the "Build Car" button, the click-handler code begins by creating an EventProductionLine class instance and instances of the three subscriber classes: EventPartsDept, EventTransportDept, and EventSalesDept (all located in the
App-Code folder of the example). Here's the code:
protected void btn_Build_Click(object sender, EventArgs e)
{
// create instance of subject class will raise the
// BuildCarEvent through its helper class BuildCarHelper
EventProductionLine pLine = new EventProductionLine();
// create instances of required subscriber classes
EventPartsDept parts = new EventPartsDept();
EventTransportDept transport = new EventTransportDept();
EventSalesDept sales = new EventSalesDept();
Next, the page must subscribe the three "subscriber" classes to the
BuildCarEvent event. Depending on the settings of three checkboxes in the page, the code adds the
BuildCarEvent handler of each subscriber class to the
BuildCarEvent of the helper instance associated with the current EventProductionLine instance. Then it can call the EventProductionLine's
BuildNewMotorCar method, specifying the model name selected in the drop-down list in the ASPX page. This sends the
BuildCarEvent to each subscriber. The code continues by displaying the resulting values from the Hashtable instance of each subscriber:
// subscribe the appropriate instances to the BuildCarEvent
// of the publisher (the EventProductionLine class) according
// to the checkbox settings in the page.
if (chk_Parts.Checked)
{
pLine.Helper.BuildCarEvent += parts.BuildCarEventHandler;
}
if (chk_Transport.Checked)
{
pLine.Helper.BuildCarEvent +=
transport.BuildCarEventHandler;
}
if (chk_Sales.Checked)
{
pLine.Helper.BuildCarEvent += sales.BuildCarEventHandler;
}
// call the main method of the subject class to do the
// actual work required, specifying any properties required
// (in this example, just the name of the car to build)
pLine.BuildNewMotorCar(lstCarName.SelectedValue);
// display counter values from subscribers to show that they
// were notified by the EventProductionLine main routine
lblResults.Text += "<b>Cars to deliver</b>: " +
ShowAllCarsCount(transport.CountOfCarsToDeliver);
lblResults.Text += "<b>Cars available to sell</b>: " +
ShowAllCarsCount(sales.CountOfCarsBuilt);
lblResults.Text += "<b>Parts to order</b>: " +
ShowAllCarsCount(parts.CountOfPartsUsed);
}
 | |
Figure 3. Sample Application: The figure shows the sample application demonstrating the Publish-Subscribe pattern. |
The only other relevant code in the client page is the
ShowAllCarsCount routine called by the
btn_Build_Click event handler shown above. The
ShowAllCarsCount routine simply iterates through the items in the Hashtable to concatenate a string that the application uses to display the car model names and counts:
private String ShowAllCarsCount(Hashtable values)
{
// get list of car models and count as a String
String result = String.Empty;
foreach (DictionaryEntry item in values)
{
result += item.Key.ToString() + ":" +
item.Value.ToString() + " ";
}
return result + "<br />";
}
Figure 3 shows the example page after building a few cars and changing the settings in the three checkboxes so that only some subscribers receive each event.
Author's Note: You can open this page by selecting either "Publish-Subscribe" or "TransferPage4.aspx" in the drop-down list at the bottom of the Default.aspx page. |