C USES agents to implement detailed observer design patterns
- 2020-06-12 10:18:12
- OfStack
In interface development, the observer design mode is often used to implement the document/view mode. When the content of the document changes, the user view as an observer must adjust accordingly to present the state of the document to the user. Due to different language mechanisms, observer design patterns are implemented differently in different languages.
In MFC's Document/view mode, every time the document content changes, the UpdateAllView function is called to update the view. This function traverses every view of the document, and calls the update function of each view to update the view. For this purpose, the document has to register every view that USES the document. C # the realization of the observer design pattern can also be used in this method, but C # provided by the agent (delegate) mechanism to implement the observer pattern provides a better method, the method and the method of MFC are similar, but the view to the document registered this 1 behavior change is for the document class instances of proxy generation, below the concrete realization method.
Make the following assumptions:
1. Document class is UserData;
2. The view class is View. In practice, the View may be 1 Form or 1 UserControl.
3. Main form is MainForm;
The three parties that participate in the observer pattern are the publisher (data/document class), the subscriber (view class), and the main form (MainForm).
Publisher:
The publisher's job is to define the data and notify subscribers when the data changes. Notifications can be implemented using either normal agents or events. First, create agents and events in UserData. Each event is triggered when the corresponding property of the UserData class changes.
public delegate void UserNameChangedEventHander(object sender, EventArgs e); // Declaration agency
public event UserNameChangedEventHander NameChanged; // The statement event
private string m_userName;
public string UserName// Custom properties
{
get
{
return m_userName;
}
set
{
if (m_userName != value)
{
m_userName = value;
NameChanged(this, EventArgs.Empty); // Triggering event
}
}
}
The above code first declares the agent, then declares the events corresponding to the agent (events count as a special agent as well), which will be generated in the view and then triggered in the set function of the property, which will be responded to in each subscriber.
The subscriber:
The subscriber's job is to respond to the publisher's notification of data changes, presenting the user with a real-time (relatively) state of the system.
Look at the code below:
private UserData m_userData = null;
public UserData UserDataObj // Define data (document) objects
{
get
{
return m_userData;
}
set
{
m_userData = (UserData)value; // The following 1 Row adds a data object event response function
m_userData.NameChanged += new UserData.UserNameChangedEventHander(UserNameChanged);
}
}
private void UserNameChanged(object sender,EventArgs e) // Define the data object event response function
{
this.tbName.Text = m_userData.UserName;// Updates content based on the data object
this.Invalidate(); // Redraw the view
}
The above code first defines the 1 data object property in the view class and adds the response to the notification published by the data object to the set function of the property. Next, you define the function that responds to the data object notification to update the view data and redraw it.
Main form:
The main form's task is to define a data object equivalent to the global, assign it to each subscribed view of the object, and change the content of the data object as needed.
Look at the code below:
private UserData m_userData; // The publisher
private View m_view;// The subscriber
private void MainForm_Load(object sender, EventArgs e)
{
m_userData = new UserData(); // An instance
m_view = new View();
m_view.UserDataObj = m_userData; // Specify publishers for subscribers
m_view.Show(); // According to
m_userData.UserName = "ZPY"; // Changing publisher data
m_view.TopMost = true;
}
Generate instances of the publisher and subscriber in the frame form class, and then assign the publisher instance to the subscriber's data object attribute. Since the class in C# is passed by reference by default, temporary objects are not generated during the assignment process. m_userData in MainForm and m_userData in View refer to the same object. Next, change the publisher data in the main form so that subscribers can update themselves via C#'s proxy (delegate) mechanism.
summary
MFC has put together a framework for developers that, despite a lot of development, many of you may not know much about the observer mode. C# offers a fully open design that may be hard to work with, but is no longer confusing.
The learning model focuses on the essence rather than the template. In order to illustrate, this paper assumes 3 aspects and divides the functions of the 3 aspects. The practical application is not limited to this. Designing the data (document) class as a singleton is also a good choice if the situation is right! In a word: master the essence, give full play to it!