在学习事件和代表的同时,我忍不住思考观察者设计模式。
我对 C# 和设计模式都属于新手。当使用事件和委托时,可以说,从子窗体触发事件并由父窗体拾取,这是观察者设计模式吗?
事件和委托还用于哪些其他设计模式?
我很有兴趣了解 .NET 中基于常见设计模式的其他“事物”(抱歉我缺乏术语),例如来自“四人帮”的设计模式。
我认为如果您可以将代码示例和此类主题的解释与模式联系起来,就更容易理解它们。反正就个人而言。
谢谢。
Whilst learning events and delegates I can't help but think about the Observer design pattern.
I'm sort of novice level with both C# and design patterns. When using events and delegates to lets say, firing an event from a child form and being picked up by the parent, is that the Observer design pattern?
What other design patters are events and delegates used in?
I'd be interested to know about other 'things' (sorry for my lack of terminology) in .NET are based on common design patterns, such as those from the Gang of Four.
I think it's easier to understand code examples and explanations with such topics if you can relate it to a pattern. Personally anyway.
Thanks.
发布评论
评论(2)
是的。事件基本上是观察者设计模式的特定于语言的实现。它被认为足够有用,可以直接构建到 C# 语言中。
许多设计模式可以使用 C# 中的委托来编写 - 但这更多的是实现细节而不是模式本身。例如,访问者模式和命令模式可以通过委托来实现(非常优雅)。
Yes. Events are basically a langauge-specific implementaiton of the observer design pattern. It was deemed useful enough to build directly into the language in C#.
Many design patterns can be written using delegates in C# - But that's more of an implementation detail than the pattern itself. For example, the visitor pattern and the command pattern can be implemented (quite elegantly) via delegates.
你是对的 - 事件/委托确实是观察者模式的实现。
据说该模式是 .NET 框架的一等公民。
除了观察者之外,当然还有迭代器(每当您使用
foreach
和IEnumerable
\IEnumarable
时)。You are correct - events/delegates are indeed an implementation of the Observer pattern.
It has been said that the pattern is a first class citizen of the .NET framework.
Apart from Observer, there is of course Iterator (whenever you use
foreach
andIEnumerable
\IEnumarable<T>
).