自定义 Java 实用程序包

发布于 2024-12-01 12:20:52 字数 308 浏览 2 评论 0原文

我当前项目中的一个常见操作是将 IP 地址的字符串版本转换为整数表示形式,并且可以通过单个静态方法轻松处理。我通常会尝试使其尽可能接近使用它的代码,但应用程序的完全不同部分也需要它。

由于在非常不同的包中让类相互引用这一实用函数似乎是有害的,因此我创建了一个 util 包并移动了静态方法(一个用于 int 到 String,一个用于 String 到 int) ) 到该包中的 Ip 类中。

我认识到这可能是一个迹象,表明我应该重新考虑项目的组织,但是添加一个包来保存项目范围的实用功能有什么害处吗? Java 中有处理这种情况的标准方法吗?

A common operation in my current project is converting a string version of an IP address into an integer representation, and it's easily handled by a single static method. I would normally try to keep this as close to the code that uses it as possible, but it's also needed in completely different parts of the application.

Since it seemed harmful to have classes in very different packages referring to each other for this one utility function, I created a util package and moved the static methods (one for int to String, one for String to int) into an Ip class in that package.

I recognize that this is probably a sign that I should rethink the project's organization, but is there anything harmful about adding a package to hold project-wide utility functions? Is there a standard way to handle this situation in Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

ι不睡觉的鱼゛ 2024-12-08 12:20:52

这是一个普遍的问题,不仅仅是 Java 的问题。

“因为它看起来有害”——有什么危害?耦合?单点故障?

您可以通过一个示例准确地说明您在 JDK 本身中所做的事情:java.util 有许多到处都在使用的类。

我认为你的设计是站得住脚的。保持这种状态,直到经验告诉你一个更好的地方来放置该类。

您应该警惕的一件事是循环依赖。您的 util 包中没有任何内容依赖于其任何依赖项。如果必要的话,用接口打破循环。

It's a generic problem, not just Java.

"Since it seemed harmful" - what's the harm? Coupling? Single point of failure?

You have an example of exactly what you did in the JDK itself: java.util has lots of classes that are used all over the place.

I think your design is defensible. Keep it that way until experience tells you a better place to put that class.

The one thing you should guard against is cyclic dependencies. Don't have anything in your util package depend on any of its dependencies. Break cycles with interfaces if you must.

姜生凉生 2024-12-08 12:20:52

我不认为这有什么问题。事实上,如果您发现有多个属于同一问题域的此类特定操作,并且并非都可以在第三方库中方便地使用,或者您希望避免添加对外部代码的额外依赖项,那么您可能会非常担心。我们会把它变成一个单独的项目。拥有一个自己的 utils jar 以便在整个应用程序中重复使用可能会很好。

据我所知,通过识别一段将被重用的代码并将其单独放置在某个地方,您做了完全正确的事情。

I don't see anything wrong with that. As a matter of fact, if you find you have multiple such specific operations that belong in the same problem domain and aren't all conveniently available in third-party libraries, or you wish to avoid adding additional dependencies on external code, you may very well turn this into a separate project. A utils jar of your own for reuse throughout your apps could be good to have around.

As far as I can tell, you did the exact right thing by identifying a piece of code that will be reused and placing it somewhere separately for that purpose.

︶葆Ⅱㄣ 2024-12-08 12:20:52

就我个人而言,我会做类似于你所做的事情。我通常在基础包中为项目范围的实用程序函数创建一个 util 包,我知道这些函数将在我的整个项目中使用。

如果我需要一个仅由特定类使用的实用函数,则该类本身可以处理它。如果我需要一个由特定包中的类使用的实用程序,请在该包中创建一个 util 包。

只是我的两分钱。

Personally I would have done something similar to what you have done. I usually create a util package in the base package for project wide utility functions that I know are going to be used throughout my project.

If I need a utility function that is only used by a specific class, the class itself can handle it. If I need a utility that will be used by classes in a specific package, create a util package in that one.

Just my two cents.

失而复得 2024-12-08 12:20:52

添加一个包来保存项目范围有什么害处吗
效用函数?

并不真地。 util 包的存在是有原因的,即保存 util 类。

特别是当它在其他包中有用时。所以它可以是您分类的逻辑顺序

is there anything harmful about adding a package to hold project-wide
utility functions?

Not really. util packages exist for a reason, to hold util classes.

Especially when it is useful across other packages. So it can be a logical sequence for your classification

土豪我们做朋友吧 2024-12-08 12:20:52

我不知道它是否“正确”,但我想我可能在我从事过的每个项目中都有某种类型的实用程序包。

I don't know if it's "correct" but I think I've probably had some type of utility package in every project I've ever worked on.

ぶ宁プ宁ぶ 2024-12-08 12:20:52

我只想添加“高扇入”作为此类实用程序类的术语。来自Code Complete(第二版),将其称为“设计的理想特征”之一”:

高扇入是指有大量的类使用给定的类。高扇入意味着系统被设计为充分利用系统较低级别的实用程序类。

I'd just like to add "high fan-in" as a term for such utility classes. From Code Complete (2nd Ed.) referring to it as one of several "desirable characteristics of a design":

High fan-in refers to having a high number of classes that use a given class. High fan-in implies that a system has been designed to make good use of utility classes at the lower levels in the system.

挽袖吟 2024-12-08 12:20:52

实用程序类是定义一组执行常见且经常重复使用的功能的方法的类。大多数实用程序类在静态范围内定义这些通用方法。实用程序类的示例包括 java.util.Collections,它为实现 Collection 的对象提供多种实用程序方法(例如排序)。
实用程序类应该始终是最终的并且具有私有构造函数。

a utility class is a class that defines a set of methods that perform common, often re-used functions. Most utility classes define these common methods under static scope. Examples of utility classes include java.util.Collections which provides several utility methods (such as sorting) on objects that implement a Collection.
Utility classes should always be final and have a private constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文