What is an empty interface used for

后端 未结 12 2669
醉话见心
醉话见心 2020-12-13 17:18

I am looking at nServiceBus and came over this interface

namespace NServiceBus
{
    public interface IMessage
    {
    }
}

What is the us

相关标签:
12条回答
  • 2020-12-13 17:56

    An empty interface acts simply as a placeholder for a data type no better specified in its interface behaviour.

    In Java, the mechanism of the interface extension represents a good example of use. For example, let's say that we've the following

    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 { ... }
    

    has to specify the two methods, being of type 'three'.

    As you can see, from the above example, empty interface can be seen also as a point of multiple inheritance (not allowed in Java).

    Hoping this helps to clarify with a further viewpoint.

    0 讨论(0)
  • 2020-12-13 18:02

    Usually it's to signal usage of a class. You can implement IMessage to signal that your class is a message. Other code can then use reflection to see if your objects are meant to be used as messages and act accordingly.

    This is something that was used in Java a lot before they had annotations. In .Net it's cleaner to use attributes for this.


    @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.

    Annotations and attributes have to be checked at runtime using reflection. Empty interfaces can be checked at compile-time using the type-system in the compiler. This brings no overhead at runtime at all so it is faster.

    0 讨论(0)
  • 2020-12-13 18:02

    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.

    0 讨论(0)
  • 2020-12-13 18:02

    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.

    0 讨论(0)
  • 2020-12-13 18:06

    Also known as a Marker Interface:

    http://en.wikipedia.org/wiki/Marker_interface_pattern

    0 讨论(0)
  • 2020-12-13 18:08

    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.

    0 讨论(0)
提交回复
热议问题