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

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

问题描述

大多数面向对象的语言都将其接口名称加上大写字母I,为什么Java不能这样做呢?没有遵循这个约定的理由是什么?

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?

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

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


大多数语言中的位置:


Class = User,Interface = IUser

Class = User, Interface = IUser

现在,你可能会争辩说你总是可以为用户实现选择一个最具描述性的名称,问题就会消失,但是Java正在推动POJO方法,大多数IOC容器都使用DynamicProxies。这两个因素共同意味着你将拥有大量具有单个POJO实现的接口。

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.

所以,我想我的问题归结为:是吗?值得遵循更广泛的接口命名约定,特别是考虑到Java框架似乎在哪里?

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.

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

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天全站免登陆