java 设计决策:为什么以及何时将接口设为私有?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4573713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:01:17  来源:igfitidea点击:

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

javainterfaceprivatedesign-decisions

提问by Jon

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?

设计决策中是否使用过私有接口?如果是,原因是什么?您何时知道需要专用接口?

回答by Andy Thomas

A top-level interfacecannot be private. It can only have publicor package access. From the Java Language Specification, section 9.1.1: "Interface Modifiers":

一个顶级接口不能是私有的。它只能具有public或包访问权限。来自Java 语言规范的第 9.1.1 节:“接口修饰符”

The access modifiers protected and private pertain only to member interfaces whose declarations are directly enclosed by a class declaration (§8.5.1).

访问修饰符 protected 和 private 仅适用于其声明直接包含在类声明中的成员接口(第 8.5.1 节)。

A nested interfacecan be privatewhenever it and its subclasses, if any, are an implementation detail of its top-level class.

嵌套接口可以是private每当它和它的子类,如有的话,是一个其顶级类的实现细节

For example, the nested interface CLibrarybelow is used as an implementation detail of the top-level class. It's used purely to define an API for JNA, communicated by the interface's Class.

例如,CLibrary下面的嵌套接口用作顶级类的实现细节。它纯粹用于为 JNA 定义 API,由接口的Class.

public class ProcessController {
    private interface CLibrary extends Library {
        CLibrary INSTANCE = (CLibrary) Native.loadLibrary( "c", CLibrary.class );
        int getpid();
    }

    public static int getPid() {
        return CLibrary.INSTANCE.getpid();
    }
}

As another example, this private interface defines an API used by private nested classes implementing custom formatting symbols.

作为另一个例子,这个私有接口定义了一个由实现自定义格式符号的私有嵌套类使用的 API。

public class FooFormatter {
    private interface IFormatPart { 
        /** Formats a part of Foo, or text.
         * @param foo Non-null foo object, which may be used as input.
         */
        void write( Foo foo ) throws IOException;
    }

    private class FormatSymbol implements IFormatPart { ... }

    private class FormatText implements IFormatPart { ... }

    ...
 }

回答by Peter Lawrey

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.

内部使用方法公开了我不希望其他开发人员使用和/或我希望能够轻松更改的方法。我有这个接口的原因是我有几个我想通过接口在内部使用的实现。

回答by lujop

It has to be package protected if the interface if for internal use. In general if the interface hasn't any interest outside it's ambit it's a good api design decision to hide itbecause there's less complexity for the users of the interface and also allows you to refactor it more easily, because when the interface is public and in the API you loss the liberty to change it.

如果接口供内部使用,则必须对其进行封装保护。一般来说,如果接口在其范围之外没有任何兴趣,那么隐藏它是一个很好的 api 设计决策因为接口用户的复杂性较低,并且还允许您更轻松地重构它,因为当接口是公共的并且在API 你失去了改变它的自由。