Java 实用程序类与服务

发布于 2024-07-20 04:25:19 字数 1431 浏览 8 评论 0原文

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

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

发布评论

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

评论(5

场罚期间 2024-07-27 04:25:19

通过使用不同的服务对象可以获得不同的行为。 实用程序类中的静态方法无法交换。 这对于测试、更改实现和其他目的非常有用。

例如,您提到了带有 crypt 方法的 CryptoUtil 。 拥有可以支持不同加密策略、不同消息接收者等的不同对象将非常有用。

Different behaviors can be obtained by using different service objects. Static methods in a utility class can't be swapped out. This is extremely useful for testing, changing implementations, and other purposes.

For example, you mention a CryptoUtil with an encrypt method. It would extremely useful to have different objects that could support different encryption strategies, different message recipients, etc.

最丧也最甜 2024-07-27 04:25:19

不同之处在于服务类可能有状态。 我所说的状态是指会话状态。 考虑一个名义上的订购系统。

interface OrderSystem {
  void login(String username, String password);
  List<Item> search(String criteria);
  void order(Item item);
  void order(Item item, int quantity);
  void update(Item item, int quantity);
  void remove(Item item);
  void checkout();
  Map<Item, Integer> getCart();
  void logout();
}

这样的事情可以通过有状态会话 bean 来完成(作为一个示例),尽管在这种情况下,身份验证可能会涵盖更传统的 EJB 机制。

这里的要点是,存在一种会话状态,即一次调用的结果会影响后续调用。 您可以将静态方法视为一堆在本地执行的简单无状态服务。

服务具有更广泛的含义,包括但不限于: 有

  • 状态的;
  • 偏僻的; 和
  • 实现相关(即通过接口)。

我认为最佳实践是简单地使用静态方法作为便捷方法(特别是考虑到 Java 缺乏扩展方法)。 服务比这丰富得多。

The difference is that service classes might have state. And by state I mean conversational state. Consider a notional ordering system.

interface OrderSystem {
  void login(String username, String password);
  List<Item> search(String criteria);
  void order(Item item);
  void order(Item item, int quantity);
  void update(Item item, int quantity);
  void remove(Item item);
  void checkout();
  Map<Item, Integer> getCart();
  void logout();
}

Such a thing could be done with stateful session beans (as one example), although in that case authentication would probably be covered more traditional EJB mechanisms.

The point here is that there is conversational state in that the results of one call affects subsequent calls. You could view static methods as a bunch of simple stateless services that execute locally.

A service has a much broader meaning that includes, but is not limited to, being:

  • stateful;
  • remote; and
  • implementation dependent (ie through an interface).

Best practice I think is to simply use static methods as convenience methods (especially given Java's lack of extension methods). Services are much richer than that.

临风闻羌笛 2024-07-27 04:25:19

您无法覆盖静态方法,如果您想以两种不同的方式实现服务并在它们之间切换,这可能是一个大问题。 出于这个原因,我将静态实用程序类的使用限制为简单的事情,这些事情“从不”(对于足够长的“从不”值:))需要以多种方式完成。

You cannot override static method, which can be a huge problem in case you'd like to implement your service in two different ways and switch between them. For this reason, I would limit the use of static utility classes to simple things which will "never" (for sufficiently long value of "never" :)) need to be done in more than one way.

从此见与不见 2024-07-27 04:25:19

我认为没有硬性规定。

我通常使用静态方法来实现需要很少参数的功能,并且可以在单个方法调用中完成。 示例:

  • 计算字符串的哈希值
  • 将日期转换为标准表示

如果某个功能需要许多参数,并且创建了多个相关结果,那么更实际的做法是拥有一个可以在其构造函数中接受共享参数的类,有几种执行实际操作的方法。

典型示例:数据库连接,您首先连接,然后用它来执行查询,然后用它来获取结果......

I think there are no hard and fast rules.

I typically use static methods for functionality that requires few parameters, and can be accomplished in a single method call. Example:

  • calculate a hash value for a string
  • convert a date to standard representation

If a functionality requires many parameters, and if there are several related results being created, then it's more practical to have a classe that can accept the shared parameters in its constructor, with several methods that perform the actual action.

Typical example: A database connection, which you first connect, then use to do a query, then use to get the result...

紫竹語嫣☆ 2024-07-27 04:25:19

我之前在这里回答过这个问题,但我发现改变服务的行为非常容易——将其重构为多个服务——如果使用静态类,则需要进行相当重要的重构。

如果这是唯一的区别(我相信是这样),那么使用静态类就没有任何意义。

每当有人说“永远不会有超过 1 个”时,就为其中 n 个进行编码。

I answered this question here somewhere before, but what I found was that it was very easy to change the behavior of a Service--to refactor it into multiple services--where it takes a pretty significant refactor if you use a static class.

If that is the only difference (and I believe it is), then it never makes any sense to use static classes.

Any time someone says "There will never ever ever be more than 1 of these", code for n of them.

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