Java接口/实现命名约定 [英] Java Interfaces/Implementation naming convention

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

问题描述

如何命名您创建的不同类/接口?
有时候我没有添加到实现名称的实现信息 - 比如接口 FileHandler 和类 SqlFileHandler

How do you name different classes / interfaces you create? Sometimes I don't have implementation information to add to the implementation name - like interface FileHandler and class SqlFileHandler.

当发生这种情况时,我通常将该界面命名为普通名称,例如 Truck 并命名实际班级 TruckClass

When this happens I usually name the interface in the "normal" name, like Truck and name the actual class TruckClass.

如何在这方面命名接口和类?

How do you name interfaces and classes in this regard?

推荐答案

命名接口它是什么。 卡车。不是 ITruck 因为它不是 ITruck 它是 Truck

Name your Interface what it is. Truck. Not ITruck because it isn't an ITruck it is a Truck.

Java中的接口类型。然后你有 DumpTruck TransferTruck WreckerTruck CementTruck 实施Truck

An Interface in Java is a Type. Then you have DumpTruck, TransferTruck, WreckerTruck, CementTruck, etc that implement Truck.

当您使用接口代替子类时,您只需将其转换为卡车。与列表< Truck> 中一样。把放在前面只是匈牙利风格符号重言式只需更多内容输入您的代码。

When you are using the Interface in place of a sub-class you just cast it to Truck. As in List<Truck>. Putting I in front is just Hungarian style notation tautology that adds nothing but more stuff to type to your code.

所有现代Java IDE都标记了接口和实现以及没有这种愚蠢符号的东西。请不要将其称为 TruckClass ,即重言式 IInterface 重言式一样糟糕。

All modern Java IDE's mark Interfaces and Implementations and what not without this silly notation. Don't call it TruckClass that is tautology just as bad as the IInterface tautology.

如果是实现它是一个类。这个规则唯一真正的例外,总有例外,可能是 AbstractTruck 。由于只有子类才会看到这个并且你永远不应该转换为 Abstract 类,它确实会添加一些类是抽象的信息以及它应该如何使用。您仍然可以提供比 AbstractTruck 更好的名称并使用 BaseTruck DefaultTruck 而是因为 abstract 在定义中。但由于 Abstract 类永远不应该成为任何面向公众的接口的一部分,我相信它是规则的可接受的例外。使构造函数受保护大大超越了这个鸿沟。

If it is an implementation it is a class. The only real exception to this rule, and there are always exceptions, could be something like AbstractTruck. Since only the sub-classes will ever see this and you should never cast to an Abstract class it does add some information that the class is abstract and to how it should be used. You could still come up with a better name than AbstractTruck and use BaseTruck or DefaultTruck instead since the abstract is in the definition. But since Abstract classes should never be part of any public facing interface I believe it is an acceptable exception to the rule. Making the constructors protected goes a long way to crossing this divide.

Impl 后缀也是更多的噪音。更多的重言式。任何不是接口的东西都是一个实现,甚至是部分实现的抽象类。你是否会在每个 Impl 后缀concepts / class.htmlrel =noreferrer>类

And the Impl suffix is just more noise as well. More tautology. Anything that isn't an interface is an implementation, even abstract classes which are partial implementations. Are you going to put that silly Impl suffix on every name of every Class?

接口是公共方法和属性必须支持的合同,它也是也键入信息。实现 Truck 的所有内容都是类型 Truck

The Interface is a contract on what the public methods and properties have to support, it is also Type information as well. Everything that implements Truck is a Type of Truck.

查看Java标准库本身。你看到 IList ArrayListImpl LinkedListImpl ?不,您会看到列表 ArrayList ,以及 LinkedList 。这是一个很好的文章关于这个确切的问题。任何这些愚蠢的前缀/后缀命名约定都违反了 DRY 原则。

Look to the Java standard library itself. Do you see IList, ArrayListImpl, LinkedListImpl? No, you see List and ArrayList, and LinkedList. Here is a nice article about this exact question. Any of these silly prefix/suffix naming conventions all violate the DRY principle as well.

此外,如果您发现自己添加 DTO JDO BEAN 或对象的其他愚蠢重复后缀,然后它们可能属于 package 而不是所有这些后缀。正确打包的命名空间是自我记录的,并且减少了这些非常缺乏构思的专有命名方案中的所有无用的冗余信息,大多数地方甚至没有以一致的方式内部遵守这些命名方案。

Also, if you find yourself adding DTO, JDO, BEAN or other silly repetitive suffixes to objects then they probably belong in a package instead of all those suffixes. Properly packaged namespaces are self documenting and reduce all the useless redundant information in these really poorly conceived proprietary naming schemes that most places don't even internally adhere to in a consistent manner.

如果只能让你的 Class 名称唯一,那么后缀为 Impl ,那么你需要重新考虑一下接口因此,当您遇到接口和单个实施的情况时界面您可能不需要界面

If all you can come up with to make your Class name unique is suffixing it with Impl, then you need to rethink having an Interface at all. So when you have a situation where you have an Interface and a single Implementation that is not uniquely specialized from the Interface you probably don't need the Interface.

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

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