Design decisions: Why and when to make an interface private?

前端 未结 3 854
我在风中等你
我在风中等你 2021-02-04 05:26

Are private interfaces ever used in design decisions ? If so, what are the reasons and when do you know the need for a private interface?

3条回答
  •  借酒劲吻你
    2021-02-04 05:55

    IMHO You cannot usefully make an interface private.

    However I often have two interfaces, one for public use and one for internal use. The internal use interface I make package local if possible e.g.

    public interface MyInterface {
       public void publicMethod();
    }
    
    interface DirectMyInterface extends MyInterface {
       public void internalUseOnlyMethod();
    }
    

    The internal use methods expose methods I don't want other developers to use and/or I want to be able to change easily. The reason I have the interface at all is that I have several implementations which I want to use internally via an interface.

提交回复
热议问题