Java Interface Naming Conventions – Java 接口命名约定

最后修改: 2023年 9月 4日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.导言

An interface in Java is a core concept typically used to define a contract, implement polymorphism, or simply create a new layer of abstraction. Naming interfaces is an essential step in developing more readable code.

Java 中的接口是一个核心概念,通常用于定义契约、实现多态性或简单地创建一个新的抽象层。命名接口是开发可读性更强的代码的重要一步。

In this tutorial, we’ll look at the naming conventions for interfaces in Java.

在本教程中,我们将了解 Java 中接口的命名约定。

2. The Java Naming Convention for Interfaces

2.Java 接口命名规则

First, let’s remember the essential purposes of an interface in Java. If we explore the source code of Java internal libraries, we’ll encounter two main use cases.

首先,让我们记住 Java 中接口的基本用途。如果我们探索 Java 内部库的源代码,我们会遇到两种主要用例。

It’s typically used as a capability for other classes or to implement polymorphism. In both cases, Java has a naming convention.

它通常用作其他类的能力或实现多态性。在这两种情况下,Java 都有一个命名约定。

Exceptionally, functional interfaces don’t follow a particular naming pattern, so we’ll not discuss them here.

功能接口并不遵循特定的命名模式,因此我们不在此讨论。

2.1. Interface as a Capability

2.1.接口作为一种能力

A capability defines what an object can do if it implements the interface. In that case, the convention is that the interface name is an adjective.

能力定义了一个对象在实现接口的情况下可以做什么。在这种情况下,惯例是接口名称是一个形容词

The Comparable interface is a good example of a capability that follows the adjective convention:

Comparable 接口是遵循形容词约定的能力的一个很好的例子:

public interface Comparable<T>

One Comparable implementation is the Integer class, which has the following signature:

其中一个可比较实现是整数类,其签名如下:

public final class Integer implements Comparable<Integer>

That means that Integer defines an object able to be compared with another Integer.

这意味着,Integer 定义了一个可以与另一个 Integer 进行比较的对象。

Other examples of interfaces named as adjectives are the Runnable and Callable interfaces. They define the capability of an object to be used as a task in multi-thread programming.

以形容词命名接口的其他示例还有 RunnableCallable 接口。它们定义了对象在多线程编程中作为任务使用的能力。

2.2. Interface as a Polymorphic Type

2.2.接口作为多态类型

Another broadly used pattern in Java libraries is polymorphism. One way to achieve it is by using an interface as a supertype with two or more implementations.

Java 库中另一种广泛使用的模式是 多态性。实现这种模式的一种方法是将一个接口作为具有两个或多个实现的超级类型。

When implementing polymorphism, the convention is that the interface name is a noun. Additionally, their subclasses’ names combine their specialty and the interface name. To illustrate that, let’s look at the List interface definition:

在实现多态性时,约定俗成的做法是接口名称是一个名词此外,它们的子类名称会将它们的专业名称和接口名称结合起来。为了说明这一点,让我们来看看 List 接口定义:

public interface List<E> extends Collection<E>

Now, let’s have a look at the signature of one polymorphic implementation of it, the LinkedList:

现在,让我们来看看 LinkedList 的多态实现的签名:

public class LinkedList<E> implements List<E>

Let’s glance at another implementation of List, the ArrayList:

让我们看看 List 的另一种实现,即 ArrayList:

public class ArrayList<E> implements List<E>

Noticeably, both classes contain the word List in their names. Additionally, their specialty names are Array and Linked, based on internal implementation. Thus the names ArrayList and LinkedList.

值得注意的是,这两个类的名称中都包含 List 一词。此外,根据内部实现,它们的专业名称分别是 Array Linked。因此,它们的名称分别是 ArrayListLinkedList.

The Connection interface, used to define a database connection type, is another example in Java libraries that follows that convention.

用于定义数据库连接类型的 Connection 接口是 Java 库中遵循该约定的另一个例子。

3. Exercising the Java Naming Convention

3.使用 Java 命名规则

To illustrate the usage of that convention, we’ll use a polymorphic User type capable of being identified. Thus, let’s first define the Identifiable interface:

为了说明该约定的用法,我们将使用一个能够被识别的多态 User 类型。因此,让我们首先定义 Identifiable 接口:

public interface Identifiable {
    void identify();
}

Now, let’s create the User interface that inherits from Identifiable:

现在,让我们创建继承自 IdentifiableUser 接口:

public interface User extends Identifiable {
    void authorize();
}

The User is divided into two implementations, the RegularUser and the RootUser.

用户分为两种实现,即 RegularUserRootUser

Let’s first create the RegularUser class:

让我们首先创建 RegularUser 类:

public class RegularUser implements User {
    @Override
    public void identify() {
        // some implementation
    }

    @Override
    public void authorize() {
        // some implementation
    }
}

Then, let’s define another implementation of User, the RootUser class:

然后,让我们定义 User 的另一个实现,即 RootUser 类:

public class RootUser implements User {
    @Override
    public void identify() {
        // some implementation
    }

    @Override
    public void authorize() {
        // some implementation
    }
}

Noticeably, we’ve used the Java convention to name our interfaces. The capability interface Identifiable is named as an adjective. The polymorphic type User and its subtypes, RootUser and RegularUser, are named as nouns.

值得注意的是,我们使用了 Java 惯例来命名我们的接口。能力接口 Identifiable 被命名为形容词。多态类型 User 及其子类型 RootUserRegularUser 被命名为名词。

This convention makes the code more fluent to read. It’s evident in the interface names that a user is identifiable and that it can be a root or a regular user. The code is closer to plain English, which improves readability.

这种约定俗成的做法使代码阅读起来更加流畅。从界面名称中可以明显看出,用户是可识别的,可以是 root 用户,也可以是普通用户。代码更接近纯英文,从而提高了可读性

4. Examples of Incorrect Interface Naming

4.不正确的接口命名示例

Another widespread pattern in enterprise Java applications comes from the Hungarian Notation. In this section, we’ll briefly talk about the preceding I and the Impl suffix patterns and why we should avoid them.

企业 Java 应用程序中另一种广泛使用的模式来自 Hungarian Notation。在本节中,我们将简要谈谈前面的 IImpl 后缀模式,以及为什么要避免它们。

The preceding I pattern suggests that any interface name starts with the capital letter I, an abbreviation for Interface. This is more common in C# code, where we don’t have a keyword to differentiate interface implementation from inheritance. However, this wouldn’t be necessary in Java since we can differentiate implementation from inheritance by looking at the keywords implements and extends.

前面的 I 模式表明,任何接口名称都以大写字母 I 开头,这是 Interface 的缩写。这在 C# 代码中更为常见,因为我们没有关键字来区分接口的实现和继承。但是,在 Java 中就没有这个必要了,因为我们可以通过关键字 implementsextends 来区分实现和继承。

The Impl suffix pattern suggests naming the interface’s implementations instead of the interface itself. Hence, all implementation names end with Impl. This usually appears when we create an interface with a single implementation and we can’t find a name that truly represents its specialization. However, the word Impl doesn’t add anything since the class signature shows it’s implementing something.

Impl 后缀模式建议命名接口的实现,而不是接口本身。因此,所有实现名称都以 Impl 结尾。这种情况通常出现在我们创建了一个具有单一实现的接口,并且找不到一个能真正代表其特殊化的名称时。然而,Impl 这个词并没有增加任何东西,因为类签名显示它正在实现某些东西

Therefore, we must avoid naming interfaces and classes using the I or Impl patterns, for example, UserImpl, IUser, IIdentifiable, and IdentifiableImpl. 

因此,我们必须避免使用 I Impl 模式来命名接口和类,例如 UserImplIUserIIdentifiableIdentifiableImpl。</em

5. Conclusion

5.结论

In this tutorial, we’ve looked at different naming conventions for interfaces.

在本教程中,我们了解了接口的不同命名约定。

The Java convention translates interface names to something closer to plain English. This helps to improve the code readability and maintainability.

Java 约定将接口名称翻译成更接近普通英语的名称。这有助于提高代码的可读性和可维护性。

Naming conventions are a matter of taste and company standards. However, Java applications align more with the Java internal libraries convention.

命名约定取决于个人喜好和公司标准。不过,Java 应用程序更符合 Java 内部库约定

As always, the source code is available over on GitHub.

与往常一样,源代码可在 GitHub 上获取