Object | design | data | Data synchronization using PUBLISH/SUBSCRIBE design pattern to achieve data synchronization between objects
Applications often need to change and exchange data, and these changed data must be transferred to synchronize objects, especially in a windowed user interface application, where the potential data update information must be reflected in all of the included subforms.
For example, a personnel information management application. You can open multiple windows with one name at a time, and if you modify and deposit the person's name in one of the windows, you will expect the name change to appear immediately in all other windows. You can do this by using the Publish/subscribe design pattern. This design pattern is a variant of the Observer pattern (see figure I). The Observer pattern is described in detail in the design Patterns, Elements of reusable Object oriented software, in Observer mode, An object (Observer observer) is registered to another object (the principal subject), which is used to listen for events. The Observer (Observer) implicitly reflects the change of (the subject subject).
Figure I
The publish/subscribe pattern (see figure II) Adds a layer of spacing between the principal (SUBJECT) and the Observer (Observer). This layer removes the bundle between the Observer (Observer) and the subject (Subject) and establishes a loosely coupled relationship between the two. Event Channel can be described as a data center.
Figure II
Publisher (the subject (SUBJECT) in The Observer (Observer) mode) Channel to this event. The responsibility of event Channel is to distribute events to all Subscribers (The Observer (OBSERVER) in The Observer (Observer) pattern). An application can contain one or more event Channel, so spread different events to each subscribers of interest. In addition, events in The Observer (Observer) pattern only come from special sources, and any object that notices event channel in Publish/subscribe mode can publish events.
This construct removes dependencies between the Observer (Observer) and the subject (Subject). The result is that you can add more generic designs to your application and, by asking for no direct relationship between Publisher and Subscriber, it greatly increases the maintainability of your application.
Using Publish/subscribe mode
Learn how publish/subscribe patterns are implemented with a simple example. Eventapp. Eventapp is an application environment that has an MDI form and many subforms. (Download the code for this article).
In your program, you will need to create a series of base classes to implement the Publish/subscribe pattern. Other classes in the project derive from these base classes (see figure III). The Eventapp application has the following 3 base classes:
Figure Three
Clseventchannel----------------------an abstract class that constructs the event channels.
Clsevent--------------------------an abstract class used for the type of the build event. It exposes 4 properties:
Name: Names of events
Value: Values of events
Extradata: Other data associated with those events
Origin: Alternative Reference authentication Event Publisher
frmsubscriber– an abstract Windows Form that is used to create windows and receive events
To make an application work, you need to build several specific classes that inherit from those base classes. In this example, these classes are:
Clsdataevents-A specific class that inherits from the Clseventchannel class. In the application, it is used as an event channel, which is used to disseminate updated information to those potential data models.
Clsdataevent-A specific class that inherits from the Clsevent class and is used to create events that render updates of potential data. (To create an inherited form, you can select the shortcut menu in Solution Explorer to add-> the inherited form (inherited Windows form).
frmlist– a specific class derived from the Frmsubscriber class. It is a subform of an MDI form that displays a list of data. In this example, the form displays 10 of data with a ListView control in hard coding.
How Eventapp Works
When Eventapp is started, Frmmain (MDI parent form) 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 an MDI form is run, you can choose "File->new List" in the menu to create an instance of the 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 frmlist ()
Mobjdataeventchannel.attachsubscriber (objNewForm)
Objnewform.mdiparent = Me
Objnewform.show ()
End Sub
When a subform is created, it is registered to the event channel, and during the registration process, the event channel will initialize the Subscriber.
Public Overridable Sub Initialize (ByVal Token as _
String, ByVal Eventchannel as Clseventchannel)
Mstrtoken = Token
Mobjeventchannel = Eventchannel
End Sub
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.