Ste*_*n C 16
拥有一个只包含一个类的包是不是一种坏习惯?
不必要.这可能是某人对分类事物痴迷的一种表现.另一方面,它可能只是在一个不寻常的情况下适用的合理的一般分类方案的逻辑结果.
An example of the latter might be where you have a general API, and multiple implementations of that API, where each of the implementations consists of multiple classes. But one of those implementations (lets call it the Null implementation) consists of just one class.
The real test is whether the package structure is serving its purpose(s):
Is it making it easier to find library classes?
Do the packages organize the application classes along the lines of the application's logical module structure?
Does the structure allow you to effectively make use of "package private" visibility?
Would it make more sense just to move the single class to a util package that would contain other random useful classes?
Not necessarily. If the class is just another "randomly useful" leaf class, then there is a good case for moving it. On the other hand, if it has a specific function and is not intended to be used generally, then it may be better to leave it where it is.
It is best not to get too obsessed with creating elegant package hierarchies, or with rejigging them when they turn out to be not as elegant (or useful) as you first thought. There are usually more important things to do, like implementing functionality, writing tests, writing documentation and so on.
拥有一个只包含一个类的包是不是一种坏习惯?
不必要.包用于将逻辑相关的实体组合在一起.它不会阻止您在包中只有一个这样的实体.
将单个类移动到
util包含其他随机有用类的包是否更有意义?
不是我,原因有两个:
Util具有特定的含义.util出于孤独的原因移动任意实体将成为使用滥用的边缘情况.