观察者模式和委托
我需要帮助来理解观察者模式和委托。 我在另一个网站上找到了这段代码,我试图了解它实际上在做什么。 有人可以帮我吗。
当我执行代码时,我收到两条消息“服务器已启动并正在运行”和“服务器已关闭,我们正在处理它,很快就会恢复”。 我想我收到了这两条消息,因为在 Main 中,有一个 server.ServerStatus = true; 和 server.ServerStatus = false。 但是,如果我注释掉 server.ServerStatus = true; 并运行然后我收到消息“服务器已启动并正在运行”,但我预计只会看到“服务器已关闭,我们正在处理它,它很快就会恢复。”。 有人可以解释一下吗? 苏珊
class Program
{
static void Main(string[] args)
{
Server server = new Server();
server.ServerStatusChanged += new EventHandler(ProcessServerStatus);
server.ServerStatus = true;
server.ServerStatus = false;
Console.Read();
}
public class Server
{
public event EventHandler ServerStatusChanged;
private bool _ServerStatus;
public bool ServerStatus
{
get { return this._ServerStatus; }
set {
if (this._ServerStatus == value) return; // Dont need to do anything;
if (this.ServerStatusChanged != null) // make sure the invocation list is not empty
ServerStatusChanged(value, new EventArgs()); // Firing Event
this._ServerStatus = value;
}
}
}
public static void ProcessServerStatus(object sender, EventArgs e)
{
bool status = (bool)sender;
if (status)
Console.WriteLine("Server is up and running");
else
Console.WriteLine("Server is down, We are working on it it will be back soon");
}
}
I need help trying to understand the Observer Pattern and Delegates. I found this code on another website and I am trying to understand what it is actually doing. Can someone help me out.
When I execute the code, I get both of the messages "Server is up and running" and "Server is down, We are working on it it will be back soon". I think I am getting both of the message because in the Main, there is a server.ServerStatus = true; and a server.ServerStatus = false. However, if I comment out the server.ServerStatus = true; and run then I I get the message "Server is up and running" but I expected to only see "Server is down, We are working on it it will be back soon.". Can someone explain?
Susan
class Program
{
static void Main(string[] args)
{
Server server = new Server();
server.ServerStatusChanged += new EventHandler(ProcessServerStatus);
server.ServerStatus = true;
server.ServerStatus = false;
Console.Read();
}
public class Server
{
public event EventHandler ServerStatusChanged;
private bool _ServerStatus;
public bool ServerStatus
{
get { return this._ServerStatus; }
set {
if (this._ServerStatus == value) return; // Dont need to do anything;
if (this.ServerStatusChanged != null) // make sure the invocation list is not empty
ServerStatusChanged(value, new EventArgs()); // Firing Event
this._ServerStatus = value;
}
}
}
public static void ProcessServerStatus(object sender, EventArgs e)
{
bool status = (bool)sender;
if (status)
Console.WriteLine("Server is up and running");
else
Console.WriteLine("Server is down, We are working on it it will be back soon");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是执行您想要的操作的代码。
注意 _initialized 变量。 这是必要的,因为否则第一次就不会发生任何事情。 在它开始正常工作之前,您必须将状态设置为 True。
另外,你所描述的情况并没有发生在我身上。 我没有收到消息说它已启动并正在运行。 我什么也没得到。
Here is code that does what you want.
Notice the _initialized variable. This is needed because otherwise nothing happens the the first time through. You would have to set the status to True before it starts working correctly.
Also, I what you describe didn't happen for me. I didn't get the message saying it was up and running. I didn't get anything at all.
这是一个事件注册...
它说“每当有 ServerStatusChanged 时,调用 ProcesServerStatus 方法”。 因此,当您将 ServerStatus 设置为 true 时,您将收到对 ProcessServerStatus 的调用,传递 true 并打印“服务器已启动...”; 当您将其更改为 false 时,会触发另一个事件,但这次 ServerStatus 为 false,因此您会得到“服务器已关闭...”因此,您正在监视服务器状态并在其更改时执行某些操作(ProcessServerStatus)。
This is an event registration ...
It says "whenever there is a ServerStatusChanged, call the ProcesServerStatus method." So, when you set the ServerStatus to true, you get a call to ProcessServerStatus passing true and it prints "Server is up..."; when you change it to false, another event is fired, but this time ServerStatus is false, so you get "Server is down ..." So, you are watching the server status and doing something (ProcessServerStatus) when it changes.
本质上发生的情况是,您在 Main 中将 ServerStatus 设置为 true/false 的分配将调用 ServerStatus 属性的“set”函数。
在该函数中,创建一个事件并将其触发到 ServerStatusChanged 处理程序,注册事件的任何人都将收到该事件。 另请注意,仅当状态更改时才会触发该事件。
在本例中,Main 注册了该行中的事件
。
由于您将 ServerStatus 设置为 true,然后在 Main 中设置为 false,因此会触发两个事件,导致您的控制台显示“服务器已启动并正在运行”和“服务器已关闭,我们正在处理它......”
Essentially what is occuring is that the assignments you have in Main that are setting ServerStatus to true/false will call the "set" function of the ServerStatus property.
Within that function, an event is created and fired to the ServerStatusChanged handler, which will be received by anyone who registered for events. Also note that the event is fired only if the status changes.
In this case, Main registered for the event in the
line.
Since you set ServerStatus to true, then false in Main, two events are fired, causing your console to display both "Server is up and running", and "Server is down, We are working on it...."
如果您以前没有接触过观察者模式,可能会本能地认为所有方法调用都是静态完成的——我们在代码中调用的方法就是被调用的方法。 然而,观察者模式可以让你更动态地做事情。 你可以在运行时说“嘿,当这样那样的事情发生时,让我知道!你可以通过调用这样那样的方法让我知道”被观察者会记录每个对它说过话的人“当这样的时候”发生这种情况,请让我知道”——然后,当它发生时,它就会这样做——让所有人都知道。 这是通过保留必须调用的方法列表,然后在事件“触发”时调用所有方法来完成的。
该行:
告诉服务器对象“嘿,当服务器状态更改时,请告诉我。您可以通过调用 ProcessServerStatus 方法让我知道”。 因此,当
运行以下行时,它会触发 ServerStatusChanged 事件,然后该事件调用所有表示“让我知道”的内容。 因此它将调用 ProcessServerStatus 方法,因为这是其列表中唯一的方法。
该列表中的每个方法都必须具有特定的方法签名; 在本例中,
void ProcessServerStatus(object sender, EventArgs e)
。 在这种情况下,sender
参数是服务器状态 - 您可以在以下行中看到这一点:因此,当调用
ProcessServerStatus
时,它会将其转换为布尔值,并打印适当的字符串。当我运行此代码时,它按预期工作 - 如果我注释掉
serverStatus = true;
行,它不会打印任何内容(因为 _serverStatus 默认为 false,并且如果状态没有',则不会触发该事件) t 改变)。 相反,如果我注释掉serverStatus = false;
行,它会显示“服务器已启动并正在运行”If you haven't come across the Observer pattern before, it might be instinctive to think that all method calls are done quite statically - the methods we call in our code are what's called. However, the observer pattern lets you do things more dynamically. You can have something say at runtime "hey, when such and such happens, let me know! You can let me know by calling such and such a method" The one being observed keeps a record of everyone that has said to it "when such and such happens, let me know" - and then, when it happens, it does just that - lets all of them know. This is done by keeping a list of the methods which must be called, and then calling them all when the event is 'triggered'.
The line:
is telling the server object "hey, when the server status is changed, let me know. You can let me know by calling the ProcessServerStatus method". And so, when the line:
is run, it triggers the ServerStatusChanged event, which then calls everything that has said "let me know". So it will call the
ProcessServerStatus
method, since that's the only method in its list.Every method that is in that list must have a certain method signature; in this case,
void ProcessServerStatus(object sender, EventArgs e)
. Thesender
parameter in this case is the server status - you can see this in the line:And so when
ProcessServerStatus
is called, it casts it so a boolean value, and prints the appropriate string.When I run this code, it works as expected - if I comment out the
serverStatus = true;
line, it prints nothing (since _serverStatus defaults to false, and the event is not triggered if the status hasn't changed). Instead, if I comment out theserverStatus = false;
line, it says "the server is up and running"