How the Publish/Subscribe Pattern Is Applied
To see how this pattern is applied, examine the sample application, EventApp. EventApp is an MDI environment in which the data in multiple MDI child windows is synchronized when changes are made (click here for the complete source code
In your own apps, you will need to create a series of base classes for implementing the Publish/Subscribe pattern. The rest of the classes in the project will be derived from these classes (see Figure 3). The EventApp application has the following three base classes:
- clsEventChannel An abstract class for creating event channels.
- clsEvent An abstract class used for creating types of events. It exposes four properties:
- Name: The name of the event
- Value: The value of the event
- ExtraData: Other data that is associated to the events
- Origin: An optional reference identifying the publisher of the event
- frmSubscriber An abstract Windows Form used for creating forms that can receive events.
|Figure 3 | Click here to get a close-up view of Base Classes.
To make an application work, you also will need to create several concrete classes that inherit from the base classes. In the sample application, these classes are:
- clsDataEvents A concrete class that inherits from clsEventChannel. The application will use this class as an event channel to disseminate information about changes to the underlying data model.
- clsDataEvent A concrete class that inherits from clsEvent and is used to create an event type that represents changes to the underlying data. (To create an inherited form, you need to select "Add -> Inherited Windows Form" from the Solution Explorer context menu. The assistant will prompt you for the class from which to derive and then generate all the necessary code to make this work.)
- frmList A concrete class that is derived from the frmSubscriber and is a generic MDI child form used to display a list of data. In EventApp, this form is hard-coded to load a list of 10 items in a Listview control.