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一书并使用了许多其他设计模式后,我试图理解Observer模式。这是不是已经使用.NET Framework中的事件实现了?
8 个解决方案
#1
25
Yes, it is. The observer pattern is also called the publish/subscribe pattern, which is exactly what events allow you to do.
是的。观察者模式也称为发布/订阅模式,这正是事件允许您执行的操作。
#2
22
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.
我会说是的,根据他对Delphi的经验,Anders Heljsberg打算使观察者模式成为C#中事件的一流语言特征。安德斯在软件工程广播电台的精彩采访中明确了这一点和其他设计意图。
#3
8
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.
注意:如果你真的想了解事件,我建议学习观察者模式并自己实现一段时间。一旦你完全理解它,就自己停止使用并使用专业且记录完备的实现,除非你真的需要这样做。
#4
4
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.
没错,事件是观察者模式的实现。不过,我已经阅读了那些仍在编写自己的人,为他们提供更多灵活性,或者只是为了避免提高事件语法的人。
#5
4
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.
使用委托和事件,找出实际“订阅”了多少“委托”来处理事件可能会更麻烦。
#6
0
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 say about that, too.
大多数现代语言都支持某些设计模式。有人认为语言越好,他们本地支持的模式就越多,而不需要明确地实现它们,并且Lisp在这方面非常出色。杰夫也有话要说。
#7
0
Microsoft Itself 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库中描述了“事件模式”。
参考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
基于事件和委托,FCL非常广泛地使用Observer模式。 FCL的设计者充分意识到了这种模式的内在力量,将其应用于整个框架中的用户界面和非UI特定功能。但是,这种用法是基础Observer模式的一个细微变化,框架团队称之为事件模式。通常,此模式表示为事件通知过程中涉及的委托,事件和相关方法的正式命名约定。 Microsoft建议使用事件和委托的所有应用程序和框架都采用此模式,尽管CLR或标准编译器中没有强制实施
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.
基于Observer模式的这种检查,显而易见的是,该模式提供了一种理想的机制,以确保应用程序中对象之间的清晰边界,而不管它们的功能(UI或其他)。尽管通过回调(使用IObserver和IObservable接口)实现起来相当简单,但委托和事件的CLR概念处理了大部分“繁重的工作”,并降低了主题和观察者之间的耦合程度。
#8
-2
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.
不,他们达到了相同的意图,但他们是不同的。我会说Observer模式是一个过度设计的黑客,可以通过函数式编程轻松实现,而.NET事件使用函数式编程来实现相同的目标。
#1
25
Yes, it is. The observer pattern is also called the publish/subscribe pattern, which is exactly what events allow you to do.
是的。观察者模式也称为发布/订阅模式,这正是事件允许您执行的操作。
#2
22
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.
我会说是的,根据他对Delphi的经验,Anders Heljsberg打算使观察者模式成为C#中事件的一流语言特征。安德斯在软件工程广播电台的精彩采访中明确了这一点和其他设计意图。
#3
8
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.
注意:如果你真的想了解事件,我建议学习观察者模式并自己实现一段时间。一旦你完全理解它,就自己停止使用并使用专业且记录完备的实现,除非你真的需要这样做。
#4
4
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.
没错,事件是观察者模式的实现。不过,我已经阅读了那些仍在编写自己的人,为他们提供更多灵活性,或者只是为了避免提高事件语法的人。
#5
4
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.
使用委托和事件,找出实际“订阅”了多少“委托”来处理事件可能会更麻烦。
#6
0
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 say about that, too.
大多数现代语言都支持某些设计模式。有人认为语言越好,他们本地支持的模式就越多,而不需要明确地实现它们,并且Lisp在这方面非常出色。杰夫也有话要说。
#7
0
Microsoft Itself 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库中描述了“事件模式”。
参考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
基于事件和委托,FCL非常广泛地使用Observer模式。 FCL的设计者充分意识到了这种模式的内在力量,将其应用于整个框架中的用户界面和非UI特定功能。但是,这种用法是基础Observer模式的一个细微变化,框架团队称之为事件模式。通常,此模式表示为事件通知过程中涉及的委托,事件和相关方法的正式命名约定。 Microsoft建议使用事件和委托的所有应用程序和框架都采用此模式,尽管CLR或标准编译器中没有强制实施
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.
基于Observer模式的这种检查,显而易见的是,该模式提供了一种理想的机制,以确保应用程序中对象之间的清晰边界,而不管它们的功能(UI或其他)。尽管通过回调(使用IObserver和IObservable接口)实现起来相当简单,但委托和事件的CLR概念处理了大部分“繁重的工作”,并降低了主题和观察者之间的耦合程度。
#8
-2
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.
不,他们达到了相同的意图,但他们是不同的。我会说Observer模式是一个过度设计的黑客,可以通过函数式编程轻松实现,而.NET事件使用函数式编程来实现相同的目标。