C#:事件或观察者界面?优点缺点? [英] C#: events or an observer interface? Pros/cons?

查看:165
本文介绍了C#:事件或观察者界面?优点缺点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下(简化):

interface IFindFilesObserver
{
    void OnFoundFile(FileInfo fileInfo);
    void OnFoundDirectory(DirectoryInfo directoryInfo);
}

class FindFiles
{
    IFindFilesObserver _observer;

    // ...
}

...我有冲突。这基本上是用C ++编写的,但C#有事件。我应该更改代码以使用事件,还是应该单独使用?

...and I'm conflicted. This is basically what I would have written in C++, but C# has events. Should I change the code to use events, or should I leave it alone?

传统观察器界面的事件有哪些优点或缺点?

What are the advantages or disadvantages of events over a traditional observer interface?

推荐答案

考虑事件是一个回调接口,其中接口只有一种方法。

Consider an event to be a callback interface where the interface has only one method.

仅需要您需要的挂钩事件

使用事件,您只需要为有兴趣处理的事件实现处理程序。在观察器界面模式中,您必须在整个界面中实现所有方法,包括实现方法体,以便您不需要处理的通知类型。在您的示例中,您始终必须实现OnFoundDirectory和OnFoundFile,即使您只关心其中一个事件。

Only hook events you need
With events you only need to implement handlers for events you're interested in handling. In the observer interface pattern, you'd have to implement all methods in the entire interface including implementing method bodies for notification types you don't actually care about handling. In your example, you always have to implement OnFoundDirectory and OnFoundFile, even if you only care about one of these events.

少维护

事件的另一个好处是你可以添加一个新的一个到特定的类,以便它会提高它,你不必更改每个现有的观察者。而如果要向接口添加新方法,则必须遍历已经实现该接口的每个类,并在其中实现新方法。事件虽然如此,您只需要更改实际想要做某些事情的现有类,以响应您要添加的新事件。

Less maintenance
Another good thing about events is you can add a new one to a particular class so that it will raise it, and you don't have to change every existing observer. Whereas if you want to add a new method to an interface, you have to go around every class that already implements that interface and implement the new method in all of them. With an event though, you only need to alter existing classes that actually want to do something in response to the new event you're adding.

模式是内置的语言,所以每个人都知道如何使用它

事件是惯用的,因为当你看到一个事件,你知道如何使用它。通过观察器界面,人们通常会采用不同的注册方式来接收通知并将观察者与事件联系在一起,一旦学习了如何注册并使用一个(使用+ =运算符),其余的都是相同的。

The pattern is built into the language so everybody knows how to use it
Events are idiomatic, in that when you see an event, you know how to use it. With an observer interface, people often implement different ways of registering to receive notifications and hook up the observer.. with events though, once you've learnt how to register and use one (with the += operator), the rest are all the same.

接口的优点

我没有很多接口的专业人士。我猜他们迫使某人在界面中实现所有的方法。但是,你不能强迫别人正确地执行所有这些方法,所以我不认为这有很大的价值。

Pros for interfaces
I haven't got many pros for interfaces. I guess they force someone to to implement all methods in the interface. But, you can't really force somebody to implement all those methods correctly, so I don't think there's a lot of value on this.

语法

有些人不喜欢为每个事件声明一个委托类型的方式。另外,.Net框架中的标准事件处理程序还有以下参数:(object sender,EventArgs args)。由于发件人没有指定特定类型,因此如果要使用它,则必须进行降档。如果感觉不正确,因为您正在失去对静态类型系统的保护,这通常在实践中很好。但是,如果您实现自己的事件,并且不遵循.Net框架约定,则可以使用正确的类型,因此不需要潜在的压缩。

Syntax
Some people don't like the way you have to declare a delegate type for each event. Also, standard event handlers in the .Net framework follow have these parameters: (object sender, EventArgs args). As sender doesn't specify a particular type, you have to down-cast if you want to use it. This often is fine in practice, if feels not quite right though because you're losing the protection of the static type system. But, if you implement your own events and don't follow the .Net framework convention on this, you can use the correct type so potential down-casting isn't required.

这篇关于C#:事件或观察者界面?优点缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆