C#中:事件或观察员接口?优点缺点? [英] C#: events or an observer interface? Pros/cons?

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

问题描述

我已经得到了以下(简体):

I've got the following (simplified):

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 Framework中的后续标准的事件处理程序有这些参数:(对象发件人,EventArgs参数)。由于发件人未指定特定的类型,你必须向下转换,如果你想使用它。这往往是在实践中好的​​,如果感觉不太对劲,但因为你失去了静态类型系统的保护。但是,如果你实现自己的事件,并且不遵循这个的.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天全站免登陆