在 C# 中,观察者模式不是已经使用事件实现了吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In C#, isn't the observer pattern already implemented using Events?
提问by Sean Chambers
After reading the Head First Design Patterns book and using a number of other design patterns, I'm trying to understand the Observer pattern. Isn't this already implemented using Events in the .NET Framework?
在阅读了 Head First Design Patterns 一书并使用了许多其他设计模式之后,我试图理解观察者模式。这不是已经使用 .NET Framework 中的事件实现了吗?
采纳答案by Sander
Yes, it is. The observer pattern is also called the publish/subscribe pattern, which is exactly what events allow you to do.
是的。观察者模式也称为发布/订阅模式,这正是事件允许您执行的操作。
回答by Ben Scheirman
That's right, events are an implementation of the observer pattern. I have read discussions , though, of people who still write their own, to give them either more flexibility, or maybe just to avoid the event-raising syntax.
没错,事件是观察者模式的一种实现。不过,我已经阅读了关于仍然自己编写的人的讨论,以给他们更多的灵活性,或者只是为了避免事件引发语法。
回答by Dinah
Yes, it's identical.
是的,它是相同的。
A note: if you really want to understand events, I recommend learning the observer pattern and implementing it yourself a for a while. Once you fully understand it, stop doing it yourself and use the professional and well-documented implementation unless you have a real need to do otherwise.
注意:如果你真的想了解事件,我建议你学习观察者模式并自己实现一段时间。一旦你完全理解它,除非你真的需要做其他事情,否则不要自己做,并使用专业且有据可查的实现。
回答by flipdoubt
I would say yes, it was Anders Heljsberg's intent to make the observer pattern a first-class language feature with events in C#, based on his experience with Delphi. Anders makes this and other design intentions clear in an excellent interview on Software Engineering Radio.
我会说是的,Anders Heljsberg 的意图是根据他在 Delphi 中的经验,使观察者模式成为 C# 中具有事件的一流语言特性。安德斯在软件工程电台的一次精彩采访中阐明了这一点和其他设计意图。
回答by Konrad Rudolph
Most modern languages have native support for some of the design patterns. It has been argued that languages are better the more patterns they support natively without the need to implement them explicitly, and that Lisp is excellent in this regard. Jeff had something to sayabout that, too.
大多数现代语言都对某些设计模式有本机支持。有人认为,语言本身支持的模式越多越好,而无需明确实现它们,而 Lisp 在这方面非常出色。杰夫对此也有话要说。
回答by Hace
Yes, but programming the observer pattern explicitly and thus not using delegates and events can result in easier debugging of your code.
是的,但是显式地对观察者模式进行编程并因此不使用委托和事件可以更轻松地调试代码。
Consider the difference:
考虑差异:
public void NotifyObservers()
{
foreach(Product product in ProductList)
{
if (product is IProductObserver)
{
product.Update(this)
}
}
}
Here it is very clear what products in the list get notified of a change. While debugging you can inspect the ProductList...
这里很清楚列表中的哪些产品会收到更改通知。在调试时,您可以检查 ProductList...
With using delegates and events it can be more cumbersome to find out how many "delegates" were actually "subscribed" to handle the event.
通过使用委托和事件,找出实际“订阅”了多少“委托”来处理事件可能会更加麻烦。
回答by Uchiha Madara
No, they achieve the same intent, however they are different. I would say that the Observer pattern is quite a hack of over design to achieve something you could have achieved easily with functional programming, and that .NET events uses functional programming to achieve the same goal.
不,它们实现了相同的意图,但是它们不同。我想说的是,观察者模式是一种过度设计,可以实现您可以通过函数式编程轻松实现的目标,而 .NET 事件使用函数式编程来实现相同的目标。
回答by Anestis Kivranoglou
MicrosoftItself States that using events and delegates is the c# way of applying the Observer Pattern. Using some basic naming conventions for events and delegates they named their own pattern as "Event Pattern"which does exactly the same thing providing some extra advantages over the classic Observer Pattern.
Microsoft自己声明使用事件和委托是应用观察者模式的 c# 方式。使用事件和委托的一些基本命名约定,他们将自己的模式命名为“事件模式”,这与经典的观察者模式完全相同,提供了一些额外的优势。
"Event Pattern"is described in the MSDN Library inside the "Exploring the Observer Design Pattern" Article.
“事件模式”在“探索观察者设计模式”文章中的 MSDN 库中进行了描述。
Based on events and delegates, the FCL uses the Observer pattern quite extensively. The designers of the FCL fully realized the inherent power of this pattern, applying it to both user interface and non-UI specific features throughout the Framework. This use, however, is a slight variation on the base Observer pattern, which the Framework team has termed the Event Pattern. In general, this pattern is expressed as formal naming conventions for delegates, events, and related methods involved in the event notification process. Microsoft recommends that all applications and frameworks that utilize events and delegates adopt this pattern, although there is no enforcement in the CLR or standard compiler
Based on this examination of the Observer pattern, it should be evident that this pattern provides an ideal mechanism to ensure crisp boundaries between objects in an application, regardless of their function (UI or otherwise). Although fairly simple to implement via callbacks (using the IObserver and IObservable interfaces), the CLR concepts of delegates and events handle the majority of the "heavy lifting," as well as decreasing the level of coupling between subject and observer.
基于事件和委托,FCL 非常广泛地使用观察者模式。FCL 的设计者充分意识到这种模式的内在力量,将其应用于整个框架中的用户界面和非 UI 特定功能。然而,这种用法是基础观察者模式的一个细微变化,框架团队将其称为事件模式。通常,此模式表示为事件通知过程中涉及的委托、事件和相关方法的正式命名约定。Microsoft 建议所有使用事件和委托的应用程序和框架都采用这种模式,尽管 CLR 或标准编译器中没有强制执行
基于对观察者模式的这种检查,很明显该模式提供了一种理想的机制来确保应用程序中对象之间的清晰边界,而不管它们的功能(UI 或其他)如何。尽管通过回调(使用 IObserver 和 IObservable 接口)实现相当简单,但委托和事件的 CLR 概念处理了大部分“繁重工作”,并降低了主体和观察者之间的耦合水平。