What is the observer mode in asp.net? Is there anything special about the observer mode in asp.net? Well, the Http-based Application is inevitably somewhat forgetful. I implemented it in this way. I don't know if there is any better solution?
Let's talk about the demand first, so as to avoid being empty talk.
In the latest Case, many clients constantly submit data to the Web Application. The administrator can view the data on the Web Management page, and multiple administrators can view the data at the same time, the data browsed by the administrator cannot be displayed from the time the Administrator starts to monitor. In this scenario, we can see that in the Observer mode, when the Administrator starts to monitor, he subscribes to data and broadcasts data to all the administrators who subscribe to the data when the data arrives.
Requirements include:
A publisher also needs a subscriber. we implement the Administrator class to subscribe to Data.
Public class Admin
{
/** // <Summary>
/// Use this to save all received data
/// </Summary>
Public IList <string> MessageList
{Get; set ;}
Public Admin (Monitor monitor)
{
MessageList = new List <string> ();
Monitor. DataIn + = new EventHandler <DataEventArgs> (ReciveMessage );
}
[Img] http://www.cnblogs.com/images/outliningindicators/inblock.gif#/img]
[Img] http://www.cnblogs.com/images/outliningindicators/inblock.gif#/img]
Private void ReciveMessage (object sender, DataEventArgs e)
[Img] http://www.cnblogs.com/images/outliningindicators/expandedsubblockstart.gif#/img]
[Img] http://www.cnblogs.com/images/outliningindicators/contractedsubblock.gif#/img]
[Img] http://www.cnblogs.com/images/dot.gif#/img] {
[Img] http://www.cnblogs.com/images/outliningindicators/inblock.gif#/img]
MessageList. Add (e. Message );
[Img] http://www.cnblogs.com/images/outliningindicators/expandedsubblockend.gif#/img]
}
[Img] http://www.cnblogs.com/images/outliningindicators/expandedblockend.gif#/img]
}
[Img] http://www.cnblogs.com/images/outliningindicators/none.gif#/img]
Okay. We have all the elements we need, but how can we make them work? If you use the Winform program, there will be no suspense.
Analysis: problems we encountered
The first question: when the client sends a data packet, do we instantiate a new Monitor? If so, why does one instantiate a brand new Monitor, and all the events subscribed to on it disappear? If not, how does the Monitor exist? There is always no such vacuum. How do I save data between two http requests? But read the requirement again, as if there is only one such Monitor in the whole application. It is time to play in the single-piece mode.
In the above Monitor implementation, add the following code:
[Img] http://www.cnblogs.com/images/outliningindicators/none.gif#/img]
Private static Monitor _ instance = null;
[Img] http://www.cnblogs.com/images/outliningindicators/none.gif?/img=public static
Monitor Current
[Img] http://www.cnblogs.com/images/outliningindicators/expandedblockstart.gif#/img]
[Img] http://www.cnblogs.com/images/outliningindicators/contractedblock.gif#/img]
[Img] http://www.cnblogs.com/images/dot.gif#/img] {
[Img] http://www.cnblogs.com/images/outliningindicators/inblock.gif#/img]
Get
[Img] http://www.cnblogs.com/images/outliningindicators/expandedsubblockstart.gif#/img]
[Img] http://www.cnblogs.com/images/outliningindicators/contractedsubblock.gif#/img]
[Img] http://www.cnblogs.com/images/dot.gif#/img] {
[Img] http://www.cnblogs.com/images/outliningindicators/inblock.gif#/img]
If (_ instance = null)
[Img] http://www.cnblogs.com/images/outliningindicators/inblock.gif#/img]
_ Instance = new Monitor ();
[Img] http://www.cnblogs.com/images/outliningindicators/inblock.gif#/img]
Return _ instance;
[Img] http://www.cnblogs.com/images/outliningindicators/expandedsubblockend.gif#/img]
}
[Img] http://www.cnblogs.com/images/outliningindicators/expandedblockend.gif#/img]}
However, the system has multiple clients. To avoid the problem caused by multithreading, Double Check and modify the Code as follows:
[Img] http://www.cnblogs.com/images/outliningindicators/none.gif#/img]
Public static Monitor Current
[Img] http://www.cnblogs.com/images/outliningindicators/expandedblockstart.gif#/img]
[Img] http://www.cnblogs.com/images/outliningindicators/contractedblock.gif#/img]
[Img] http://www.cnblogs.com/images/dot.gif#/img] {
[Img] http://www.cnblogs.com/images/outliningindicators/inblock.gif#/img]
Get
[Img] http://www.cnblogs.com/images/outliningindicators/expandedsubblockstart.gif#/img]
[Img] http://www.cnblogs.com/images/outliningindicators/contractedsubblock.gif#/img]
[Img] http://www.cnblogs.com/images/dot.gif#/img] {
[Img] http://www.cnblogs.com/images/outliningindicators/inblock.gif#/img]
Object o = new object ();
[Img] http://www.cnblogs.com/images/outliningindicators/inblock.gif#/img]
If (_ instance = null)
[Img] http://www.cnblogs.com/images/outliningindicators/expandedsubblockstart.gif#/img]
[Img] http://www.cnblogs.com/images/outliningindicators/contractedsubblock.gif#/img]
[Img] http://www.cnblogs.com/images/dot.gif#/img] {
[Img] http://www.cnblogs.com/images/outliningindicators/inblock.gif#/img]
Lock (o)
[Img] http://www.cnblogs.com/images/outliningindicators/expandedsubblockstart.gif#/img]
[Img] http://www.cnblogs.com/images/outliningindicators/contractedsubblock.gif#/img]
[Img] http://www.cnblogs.com/images/dot.gif#/img] {
[Img] http://www.cnblogs.com/images/outliningindicators/inblock.gif#/img]
If (_ instance = null)
[Img] http://www.cnblogs.com/images/outliningindicators/inblock.gif#/img]
_ Instance = new Monitor ();
[Img] http://www.cnblogs.com/images/outliningindicators/expandedsubblockend.gif#/img]
}
[Img] http://www.cnblogs.com/images/outliningindicators/expandedsubblockend.gif#/img]
}
[Img] http://www.cnblogs.com/images/outliningindicators/inblock.gif#/img]
Return _ instance;
[Img] http://www.cnblogs.com/images/outliningindicators/expandedsubblockend.gif#/img]
}
[Img] http://www.cnblogs.com/images/outliningindicators/expandedblockend.gif#/img]
}
[Img] http://www.cnblogs.com/images/outliningindicators/none.gif#/img]
(PS: why can I use a single piece to save instances across requests? Because a static member is used to save the reference of the Monitor, static member is used as the Root in the. net GC. For details, see the Framework Program Design book)
- Three pages in total:
- Previous Page
- 1
- 2
- 3
- Next Page