English 中文(简体)
空接口用于什么?
原标题:
  • 时间:2008-12-19 11:00:34
  •  标签:

我正在研究nServiceBus,看到了这个接口。

namespace NServiceBus
{
    public interface IMessage
    {
    }
}

一个空接口有什么用途?

最佳回答

通常用于标志类的使用。您可以实现IMessage来表示您的类是一个消息。其他代码可以使用反射来判断您的对象是否是要用作消息并采取相应的行动。

在Java使用注释之前,这是一个经常被使用的东西。在.Net中,使用属性更加简洁。


@Stimpy77 Thanks! I hadn t thought of it that way. I hope you ll allow me to rephrase your comment in a more general way.

注释和属性必须在运行时使用反射进行检查。空接口可以在编译时使用编译器中的类型系统进行检查。这在运行时不会带来任何开销,因此速度更快。

问题回答

也被称为标记界面:

将此翻译成中文:http://en.wikipedia.org/wiki/Marker_interface_pattern 标记接口模式

在Java中, Serializable 是最完美的例子。它没有定义任何方法,但每个实现它的类都必须确保它真正可序列化并且不持有对无法序列化的东西的引用,例如数据库连接,打开的文件等。

在Java中,空接口通常被用于“标记”类 - 这些天通常会使用注解。

这只是一种添加元数据到类中的方式,表示“这个类适用于<此类>的使用”,即使没有共同成员也会涉及。

通常它类似于属性。使用属性优于空接口(至少FxCop知道)。但是.NET本身使用了一些这些接口,如。我认为在使用属性时,元数据查找存在性能损失,这使得他们使用接口代替。

一个空接口只是作为占位符,用于没有更好指定其接口行为的数据类型。

在Java中,接口扩展机制是其良好使用的一个例子。例如,假设我们有以下代码:

interface one {}
interface two {}

interface three extends one, two {}

Interface three will inherit the behaviour of one and two , and so

class four implements three { ... }

必须指定两种方法,类型为三。

正如您所看到的那样,从上面的示例中可以看出,空接口也可以被视为多重继承的一个点(Java中不允许)。

希望这可以通过更进一步的观点来澄清。

它们被称为“标记接口”,旨在表示标记类的实例。

例如,在C++中,将对象标记为“ICollectible”是一种常见做法,以便可以将它们存储在通用非类型化集合中。

所以,就像有人说的那样,它们是用来表示一些对象支持的行为,比如能够被收集、序列化等。

Been working with NServiceBus for the past year. While I wouldn t speak for Udi Dahan my understanding is that this interface is indeed used as a marker primarily.

Though I d suggest you ask the man himself if he d had thoughts of leaving this for future extension. My bet is no, as the mantra seems to be to keep messages very simple or at least practically platform agnostic.

Others answer well on the more general reasons for empty interfaces.

I d say its used for "future" reference or if you want to share some objects, meaning you could have 10 classes each implementing this interface.

And have them sent to a function for work on them, but if the interface is empty, I d say its just "pre"-work.

Empty interfaces are used to document that the classes that implement a given interface have a certain behaviour

For example in java the Cloneable interface in Java is an empty interface. When a class implements the Cloneable interface you know that you can call run the clone() on it.

Empty interfaces are used to mark the class, at run time type check can be performed using the interfaces.

For example An application of marker interfaces from the Java programming language is the Serializable interface. A class implements this interface to indicate that its non-transient data members can be written to an ObjectOutputStream. The ObjectOutputStream private method writeObject() contains a series of instanceof tests to determine writeability, one of which looks for the Serializable interface. If any of these tests fails, the method throws a NotSerializableException.

An empty interface can be used to classify classes under a specific purpose. (Marker Interface)

Example : Database Entities

public interface IEntity {

}

public class Question implements IEntity {
   // Implementation Goes Here
}

public class Answer implements IEntity {
   // Implementation Goes Here
}

For Instance, If you will be using Generic Repository(ex. IEntityRepository), using generic constraints, you can prevent the classes that do not implement the IEntity interface from being sent by the developers.





相关问题
热门标签