问题
I just begin to maintain a Java MVC project which use Java Guice Framework.
In almost the entire code, first developers passed as parameter an empty model interface extending another interface.
Here's the first Interface:
public interface FooModel extends ModelInterface {
}
And the other interface:
public interface ModelInterface {
public void addListener(FooListener fooListener);
void setFoo(boolean blop);
boolean isFoo();
}
That does not make any sense for me.
Is there a good reason/pattern use empty interface, in Java? Maybe for Guice?
回答1:
Here is a good motivation for "marker interfaces". They are especially useful in aspect-oriented programming where they give you a point to attach to (and I guess Guice is just such a framework).
回答2:
Only reason i know to use an empty interface would be as a marker interface. There are some examples like java.lang.Cloneable or Serializable.
回答3:
It could be used to mark a class for a specific use.
Example: IRequiresSessionState
来源:https://stackoverflow.com/questions/10590526/what-is-the-purpose-for-using-an-empty-interface-that-extends-another-in-java