Java中的接口命名 [英] Interface naming in Java

查看:35
本文介绍了Java中的接口命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数面向对象语言的接口名称都以大写字母 I 为前缀,为什么 Java 不这样做?不遵守这个约定的理由是什么?

为了说明我的意思,如果我想要一个用户界面和一个用户实现,我在 Java 中有两个选择:

<块引用>

  1. 类 = 用户,界面 = 用户界面
  2. 类 = UserImpl,接口 = 用户

在大多数语言中的位置:

<块引用>

类 = 用户,接口 = IUser

现在,您可能会争辩说,您总是可以为用户实现选择一个最具描述性的名称,问题就会迎刃而解,但 Java 正在推动 POJO 方法,并且大多数 IOC 容器广泛使用 DynamicProxies.这两件事一起意味着您将拥有许多接口和单个 POJO 实现.

所以,我想我的问题可以归结为:是否值得遵循更广泛的接口命名约定,尤其是考虑到 Java 框架的发展方向?"

解决方案

我不喜欢在接口上使用前缀:

  • 前缀会降低可读性.

  • 在客户端中使用接口是标准的最佳编程方式,因此接口名称应尽可能简短且令人愉快.实现类应该更难看,以阻止它们的使用.

  • 当从抽象类更改为接口时,带有前缀 I 的编码约定意味着重命名所有出现的类 --- 不好!

Most OO languages prefix their interface names with a capital I, why does Java not do this? What was the rationale for not following this convention?

To demonstrate what I mean, if I wanted to have a User interface and a User implementation I'd have two choices in Java:

  1. Class = User, Interface = UserInterface
  2. Class = UserImpl, Interface = User

Where in most languages:

Class = User, Interface = IUser

Now, you might argue that you could always pick a most descriptive name for the user implementation and the problem goes away, but Java's pushing a POJO approach to things and most IOC containers use DynamicProxies extensively. These two things together mean that you'll have lots of interfaces with a single POJO implementation.

So, I guess my question boils down to: "Is it worth following the broader Interface naming convention especially in light of where Java Frameworks seem to be heading?"

解决方案

I prefer not to use a prefix on interfaces:

  • The prefix hurts readability.

  • Using interfaces in clients is the standard best way to program, so interfaces names should be as short and pleasant as possible. Implementing classes should be uglier to discourage their use.

  • When changing from an abstract class to an interface a coding convention with prefix I implies renaming all the occurrences of the class --- not good!

这篇关于Java中的接口命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆