都是空的接口code味道? [英] Are empty interfaces code smell?

查看:92
本文介绍了都是空的接口code味道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有同样的返回类型的对象(查询结果),但没有公共属性或方法的功能。为了有一个共同的I型使出使用空接口作为返回类型和实施,关于两者。

I have a function that returns same kind of objects (query results) but with no properties or methods in common. In order to have a common type I resorted using an empty interface as a return type and "implemented" that on both.

这听起来并不当然的权利。我只能通过抱住希望有​​一天这些类将有共同之处安慰自己,我会的常见逻辑移动到我的空接口。但我不满意和思考我是否应该有两种不同的方法,并有条件地调用next。那会是一个更好的方法吗?

That doesn't sound right of course. I can only console myself by clinging to hope that someday those classes will have something in common and I will move that common logic to my empty interface. Yet I'm not satisfied and thinking about whether I should have two different methods and conditionally call next. Would that be a better approach?

我一直还告诉.NET框架使用空接口用于标记目的。

I've been also told that .NET Framework uses empty interfaces for tagging purposes.

我的问题是:是一个空的接口设计问题的一个强有力的信号,或者是广泛使用?

My question is: is an empty interface a strong sign of a design problem or is it widely used?

修改:对于那些有兴趣,我后来发现,在函数式语言歧视工会是什么,我试图达到完美的解决方案。 C#似乎并不友好这一概念还没有。

EDIT: For those interested, I later found out that discriminated unions in functional languages are the perfect solution for what I was trying to achieve. C# doesn't seem friendly to that concept yet.

推荐答案

虽然它似乎存在着一个设计模式为用例(很多都提到标记接口现在),我认为,这种做法的使用是code气味的指示(大部分时间至少)

Although it seems there exists a design pattern (a lot have mentioned "marker interface" now) for that use case, i believe that the usage of such a practice is an indication of a code smell (most of the time at least).

作为@ V4Vendetta贴,还有就是针对这种静态分析规则:
<一href=\"http://msdn.microsoft.com/en-us/library/ms182128(v=VS.100).aspx\">http://msdn.microsoft.com/en-us/library/ms182128(v=VS.100).aspx

As @V4Vendetta posted, there is a static analysis rule that targets this: http://msdn.microsoft.com/en-us/library/ms182128(v=VS.100).aspx

如果您的设计包括类型有望实现空接口,您可能正在使用一个接口作为标记或方法来识别的一组类型。 如果此标识将在运行时有发生,要完成这个正确的方法是使用自定义属性。使用presence与否的属性或属性的属性,以确定目标类型。 如果必须发生在编译​​时的标识,那么它是可以接受的使用空接口。

If your design includes empty interfaces that types are expected to implement, you are probably using an interface as a marker or a way to identify a group of types. If this identification will occur at run time, the correct way to accomplish this is to use a custom attribute. Use the presence or absence of the attribute, or the properties of the attribute, to identify the target types. If the identification must occur at compile time, then it is acceptable to use an empty interface.

这是引述MSDN建议:

This is the quoted MSDN recommendation:

删除界面或添加成员到它。如果空接口被用来标注一组的类型,与自定义属性替换接口

Remove the interface or add members to it. If the empty interface is being used to label a set of types, replace the interface with a custom attribute.

这也反映了已经发布维基百科链接的批判部分。

This also reflects the Critique section of the already posted wikipedia link.

与标志接口的一个主要问题是,一个接口定义用于实现类的合同,而该合同是由所有的子类继承。这意味着你不能unimplement的标志。在这个例子中给出,如果您创建一个你不想序列化(也许是因为它取决于瞬态)的子类,则必须求助于明确抛出NotSerializableException(每ObjectOutputStream的文档)。

A major problem with marker interfaces is that an interface defines a contract for implementing classes, and that contract is inherited by all subclasses. This means that you cannot "unimplement" a marker. In the example given, if you create a subclass that you do not want to serialize (perhaps because it depends on transient state), you must resort to explicitly throwing NotSerializableException (per ObjectOutputStream docs).

这篇关于都是空的接口code味道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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