Practical side of the ability to define a class within an interface in Java?

前端 未结 11 1134
暖寄归人
暖寄归人 2020-12-30 06:32

What would be the practical side of the ability to define a class within an interface in Java:

interface IFoo
{
    class Bar
    {
        void foobar (         


        
相关标签:
11条回答
  • 2020-12-30 07:25

    I would urge caution whenever it seems like a good idea to create a non-private nested class. You are almost certainly better off going straight for an outer class. But if you are going to create a public nested class, it doesn't seem any more strange to put it in an interface than a class. The abstractness of the outer class is not necessarily related to the abstractness of a nested class.

    0 讨论(0)
  • 2020-12-30 07:26

    I can say without hesitation that I've never done that. I can't think of a reason why you would either. Classes nested within classes? Sure, lots of reasons to do that. In those cases I tend to consider those inner classes to be an implementation detail. Obviously an interface has no implementation details.

    0 讨论(0)
  • 2020-12-30 07:26

    With a static class inside an interface you have the possibility to shorten a common programming fragment: Checking if an object is an instance of an interface, and if so calling a method of this interface. Look at this example:

    public interface Printable {
        void print();
    
        public static class Caller {
            public static void print(Object mightBePrintable) {
                if (mightBePrintable instanceof Printable) {
                    ((Printable) mightBePrintable).print();
                }
            }
        }
    }
    

    Now instead of doing this:

    void genericPrintMethod(Object obj) {
        if (obj instanceof Printable) {
            ((Printable) obj).print();
        }
    }
    

    You can write:

    void genericPrintMethod(Object obj) {
       Printable.Caller.print(obj);
    }
    
    0 讨论(0)
  • 2020-12-30 07:27

    Google Web Toolkit uses such classes to bind 'normal' interface to asynchronous call interface:

    public interface LoginService extends RemoteService {
    
        /**
         * Utility/Convenience class.
         * Use LoginService.App.getInstance() to access static instance of LoginServiceAsync
         */
        class App {
    
            public static synchronized LoginServiceAsync getInstance() {
                ...
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-30 07:28

    One place this idiom is used heavily is in XMLBeans. The purpose of that project is to take an XML Schema and generate a set of Java classes that you can use bidirectionally to work with XML documents corresponding to the schema. So, it lets you parse XML into xml beans or create the xml beans and output to xml.

    In general, most of the xml schema types are mapped to a Java interface. That interface has within it a Factory that is used to generate instances of that interface in the default implementation:

    public interface Foo extends XmlObject {
      public boolean getBar();
      public boolean isSetBar();
      public void setBar(boolean bar);
    
      public static final SchemaType type = ...
    
      public static final class Factory {
        public static Foo newInstance() {
          return (Foo)XmlBeans.getContextTypeLoader().newInstance(Foo.type, null);
        }
    
        // other factory and parsing methods
      }
    } 
    

    When I first encountered this it seemed wrong to bind all this implementation gunk into the interface definition. However, I actually grew to like it as it let everything get defined in terms of interfaces but have a uniform way to get instances of the interface (as opposed to having another external factory / builder class).

    I picked it up for classes where this made sense (particularly those where I had a great deal of control over the interface/impls) and found it to be fairly clean.

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