Observer模式与事件驱动模型有何不同?

时间:2022-06-29 03:40:03

I am a senior level developer but I haven't had a lot of formal training and I although I have used many design patterns and seen them used in my years as a developer, no one really went out of their way to say. "Oh this is an observer pattern, or this is a Singleton pattern."

我是一名高级开发人员,但我没有接受过很多正式培训,虽然我已经使用了很多设计模式并且看到它们在我作为开发人员使用的年代中使用过,但没有人真正想要说出来。 “哦,这是一个观察者模式,或者这是一个单身人士模式。”

Reading over some of the design patterns, I came across the Observer pattern and it seems to be to be very similar to the way the .NET framework events work. Am I missing something fundamental about this?

阅读一些设计模式,我遇到了Observer模式,它似乎与.NET框架事件的工作方式非常相似。我错过了一些基本的东西吗?

4 个解决方案

#1


19  

The .NET Event model is pretty much a integrated implementation of the observer pattern in the common language runtime. The .NET languages implement observer directly in their language specific manner, using the framework's built-in support for this.

.NET事件模型几乎是公共语言运行库中观察者模式的集成实现。 .NET语言使用框架内置的支持,直接以特定语言的方式实现观察者。

In most programming languages, the observer pattern requires customized development or libraries.

在大多数编程语言中,观察者模式需要定制开发或库。

It comes for free as part of the language in C#, VB.NET and most other languages built to use the CLR.

它作为C#,VB.NET语言的一部分免费提供,以及为使用CLR而构建的大多数其他语言。

#2


6  

From MSDN

Those of you with passing familiarity of the types exposed in the FCL will note that no IObserver, IObservable, or ObservableImpl types are present in the Framework. The primary reason for their absence is the fact that the CLR makes them obsolete after a fashion. Although you can certainly use these constructs in a .NET application, the introduction of delegates and events provides a new and powerful means of implementing the Observer pattern without developing specific types dedicated to support this pattern. In fact, as delegates and events are first class members of the CLR, the foundation of this pattern is incorporated into the very core of the .NET Framework. As such, the FCL makes extensive use of the Observer pattern throughout its structure.

那些熟悉FCL中公开的类型的人会注意到框架中不存在IObserver,IObservable或ObservableImpl类型。它们缺席的主要原因是CLR使它们在时尚之后过时了。虽然您当然可以在.NET应用程序中使用这些构造,但委托和事件的引入提供了一种新的强大的方法来实现Observer模式,而无需开发专用于支持此模式的特定类型。实际上,由于委托和事件是CLR的第一类成员,因此该模式的基础已合并到.NET Framework的核心中。因此,FCL在其整个结构中广泛使用Observer模式。

#3


4  

Many event models, like the Java 1.1 and beyond, as well as the .NET event model are basically implementations of the Observer pattern.

许多事件模型(如Java 1.1及更高版本)以及.NET事件模型基本上都是Observer模式的实现。

Note that this even applies to older mechanisms, such as using callback methods in C for event handling. It's the same intent, just implemented slightly differently.

请注意,这甚至适用于较旧的机制,例如在C中使用回调方法进行事件处理。这是相同的意图,只是略有不同。

#4


1  

Why do you think there must be a difference?

为什么你认为一定有区别?

Don't you think the .NET designers read Design Patterns as well?

你不觉得.NET设计师也读过设计模式吗?

Actually, the Observer pattern (like all in the book) were well known long before they were categorized and named by the Gof4. It was used to implement the .Net event model, as well as the Win32 & Win16 event models, and probably many others.

实际上,观察者模式(就像书中的所有内容)早在被Gof4分类和命名之前就已为人所熟知。它用于实现.Net事件模型,以及Win32和Win16事件模型,可能还有许多其他模型。

#1


19  

The .NET Event model is pretty much a integrated implementation of the observer pattern in the common language runtime. The .NET languages implement observer directly in their language specific manner, using the framework's built-in support for this.

.NET事件模型几乎是公共语言运行库中观察者模式的集成实现。 .NET语言使用框架内置的支持,直接以特定语言的方式实现观察者。

In most programming languages, the observer pattern requires customized development or libraries.

在大多数编程语言中,观察者模式需要定制开发或库。

It comes for free as part of the language in C#, VB.NET and most other languages built to use the CLR.

它作为C#,VB.NET语言的一部分免费提供,以及为使用CLR而构建的大多数其他语言。

#2


6  

From MSDN

Those of you with passing familiarity of the types exposed in the FCL will note that no IObserver, IObservable, or ObservableImpl types are present in the Framework. The primary reason for their absence is the fact that the CLR makes them obsolete after a fashion. Although you can certainly use these constructs in a .NET application, the introduction of delegates and events provides a new and powerful means of implementing the Observer pattern without developing specific types dedicated to support this pattern. In fact, as delegates and events are first class members of the CLR, the foundation of this pattern is incorporated into the very core of the .NET Framework. As such, the FCL makes extensive use of the Observer pattern throughout its structure.

那些熟悉FCL中公开的类型的人会注意到框架中不存在IObserver,IObservable或ObservableImpl类型。它们缺席的主要原因是CLR使它们在时尚之后过时了。虽然您当然可以在.NET应用程序中使用这些构造,但委托和事件的引入提供了一种新的强大的方法来实现Observer模式,而无需开发专用于支持此模式的特定类型。实际上,由于委托和事件是CLR的第一类成员,因此该模式的基础已合并到.NET Framework的核心中。因此,FCL在其整个结构中广泛使用Observer模式。

#3


4  

Many event models, like the Java 1.1 and beyond, as well as the .NET event model are basically implementations of the Observer pattern.

许多事件模型(如Java 1.1及更高版本)以及.NET事件模型基本上都是Observer模式的实现。

Note that this even applies to older mechanisms, such as using callback methods in C for event handling. It's the same intent, just implemented slightly differently.

请注意,这甚至适用于较旧的机制,例如在C中使用回调方法进行事件处理。这是相同的意图,只是略有不同。

#4


1  

Why do you think there must be a difference?

为什么你认为一定有区别?

Don't you think the .NET designers read Design Patterns as well?

你不觉得.NET设计师也读过设计模式吗?

Actually, the Observer pattern (like all in the book) were well known long before they were categorized and named by the Gof4. It was used to implement the .Net event model, as well as the Win32 & Win16 event models, and probably many others.

实际上,观察者模式(就像书中的所有内容)早在被Gof4分类和命名之前就已为人所熟知。它用于实现.Net事件模型,以及Win32和Win16事件模型,可能还有许多其他模型。