Use the Publish / Subscribe design pattern to reach the target data synchronization
Applications often need to change and exchange data, and these changes must be transferred to achieve synchronization of objects, especially in multi-window user interface applications, more demanded synchronous coordination in this type of data, in which potential data The update information must be reflected in all included sub-forms.
For example, a person information management application. Once you can open a window that contains a name of the name, if you modify it in one window and report this person's name, you will expect to change the name change should be displayed immediately in other forms. This feature can be accomplished by using the Publish / Subscribe design mode. This design pattern is a variant of the OBServer mode (see Figure 1). Observer mode has a very detailed description in Design Patterns, Elements of Reusable Object Oriented Software, in Observer mode, an object (OBServer observer) is registered to another object (subject Subject) for listening to events. Observer is secretly reflected in the changes in (main Subject).
Figure one
Publish / Subscribe mode (see Figure 2), an interval is added between the main body (Subject) and the Observer. This layer removes the bundling between the Observer and the Subject and establishes a loosely coupled relationship between the two. Event Channel can be described as a data center.
Figure II
Publisher (is a Subject) in the Observer mode) to this Event Channel. Publish event. Event Channel's responsibility is to distribute events to all Subscribers (in the Observer) mode. An application can include one or more Event Channel, so you want to spread different events to each of interest Subscribers. In addition, events are only from special sources in the Observer mode, any payment of Event Channel in the Publish / Subscribe mode can issue an event.
This constructed movement is dependent between the OBServer and the Subject. The result is that you can add more common design for your app, in addition, through no direct relationship between Publisher and Subscriber, it greatly increases the maintenanceability of your application.
Use Publish / Subscribe mode
How to learn how Publish / Subscribe mode is achieved by a simple example. EventApp. EventApp is an application environment with a MDI form and many subforms. (Download this document).
In your program, you will need to create a series of base classes to implement the Publish / Subscribe mode. Other classes in the project come from these base classes (see Figure 3). EventApp applications have the following three base classes:
Figure three
CLSEVENTCHANNEL --------------------- is an abstract class that builds Event Channels.
CLSEVENT ------------------------- is an abstract class that is used for the type of event. It exposes 4 properties: name: The name of the event
Value: Value of Event
EXTRADATA: Other data related to those events
ORIGIN: Selectable Reference Appraisal Event Publisher
FRMSUBSCRIBER - Used to create a window, an abstract Windows Form that receives events
In order to make an application work, you need to build several specific classes from those base classes inherited. In this example, these classes are:
CLSDATAEVENTS - a specific class inherited from the ClseventChannel class. In the application, use it as an Event Channel, which is used to distribute information to those potential data models.
CLSDataEvent - inherits from the CLSEvent class and used to create an event, presenting a specific class of the update of the potential data. (To create a inheritance form, you can select "Add (Add) -> Inherited Windows Form" shortcut menu in Solution Explorer.
FRMLIST - Deleted a specific class from the FRMSUBSCRIBER class. It is a sub-form for the MDI form for displaying a list of data. In this example, this form displays 10 data with a ListView control with a hardcod.
How to work EVENTAPP
When EventApp is started, the frmmain (MDI parent window) creates a data Event Channel instance ----- ClsdataEvents:
Private Sub frmmain_load (byval sender as object, _
BYVAL E as system.eventargs) Handles mybase.load
MobjdataEventchannel = new clsdataevents ()
Me.ismdicontAiner = true
Me.Text = "Publish / Subscribe EVENT SAMPLE"
Me.WindowState = FormWindowState.maximized
End Sub
When the MDI form is run, you can select "File-> New List" in the menu to create an instance of a FRMLIST class.
Private Sub Menuitem2_Click (Byval Sender AS _
System.Object, ByVal e as system.eventargs_
Handles menuitem2.click
DIM Objnewform as frmlist
Objnewform = new fmlist ()
MobjDataEventchannel.attachsubscriber (Objnewform)
Objnewform.mdiparent = me
Objnewform.show ()
End Sub
After the child form is created, it will be registered to Event Channel. During the registration process, Event Channel will initialize Subscriber.
Public Overridable Sub Initialize (Byval token As _
String, ByVal Eventchannel as Clseventchannel
MSTRTOKEN = Token
Mobjeventchannel = Eventchannel
End Sub