Java 8:使用静态方法而不是静态util类的接口 [英] Java 8: Interface with static methods instead of static util class

查看:149
本文介绍了Java 8:使用静态方法而不是静态util类的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我需要一堆无状态实用程序方法时,Java 8中的最佳实践是什么。拥有一个的接口是否正确,即公共接口签名公共接口环境,或者以旧方式做得更好 - 拥有公共最终类签名公共最终类环境私有构造函数||枚举?

What is best practice in Java 8 when I need a bunch of stateless utility methods. Is it right to have an interface that will not be implemented by anyone i.e. public interface Signatures and public interface Environments, or is it better to do it the old way - have public final class Signatures and public final class Environments with private constructors || enums?

推荐答案

接口的主要目的是提供该类型的操作(方法)的类型和词汇表。它们是有用且灵活的,因为它们允许多个实现,实际上它们被设计为允许在类层次结构中不相关的实现。

The main purpose of interfaces is to provide a type and a vocabulary of operations (methods) on that type. They're useful and flexible because they allow multiple implementations, and indeed they are designed to allow implementations that are otherwise unrelated in the class hierarchy.

问题是,


拥有一个不会被任何人实现的界面是对的吗??

Is it right to have an interface that will not be implemented by anyone...?

在我看来,这样可以减少对接口的影响。人们不得不环顾API以确定没有实现此接口的类,并且没有此接口的生产者或使用者。有人可能会感到困惑,并试图创建一个接口的实现,但当然他们不会走得太远。虽然可以使用所有静态方法的实用程序接口,但这并不像旧的不可构造的最终类习惯用语那样清晰。后者的优点是类可以强制执行,不能创建任何实例。

This seems to me to cut against the grain of interfaces. One would have to look around the API to determine that there are no classes that implement this interface, and that there are no producers or consumers of this interface. Somebody might be confused and try to create an implementation of the interface, but of course they wouldn't get very far. While it's possible to have a "utility interface" with all static methods, this isn't as clear as the old unconstructible final class idiom. The advantage of the latter is that the class can enforce that no instances can ever be created.

如果查看新的Java 8 API ,你会看到尽管能够在接口上添加静态方法,仍然使用最终的类习惯用法。

If you look at the new Java 8 APIs, you'll see that the final class idiom is still used despite the ability to add static methods on interfaces.

接口上的静态方法已用于工厂方法之类的东西创建这些接口的实例,或者用于在这些接口的所有实例中具有普遍适用性的实用程序方法。例如,请参阅 java.util.stream Stream Collector 接口C $ C>。每个都有静态工厂: Stream.of() Stream.empty() Collector.of()

Static methods on interfaces have been used for things like factory methods to create instances of those interfaces, or for utility methods that have general applicability across all instances of those interfaces. For example, see the Stream and Collector interfaces in java.util.stream. Each has static factories: Stream.of(), Stream.empty(), and Collector.of().

但请注意,每个都有伴侣工具类 StreamSupport 收藏家。这些是纯实用程序类,仅包含静态方法。可以说它们可以合并到相应的接口中,但这会使接口混乱,并且会模糊类中包含的方法的关系。例如, StreamSupport 包含一系列相关的静态方法,这些方法都是 Spliterator 之间的所有适配器流。将这些合并到 Stream 可能会让事情变得混乱。

But also note that each has companion utility classes StreamSupport and Collectors. These are pure utility classes, containing only static methods. Arguably they could be merged into the corresponding interfaces, but that would clutter the interfaces, and would blur the relationship of the methods contained in the classes. For example, StreamSupport contains a family of related static methods that are all adapters between Spliterator and Stream. Merging these into Stream would probably make things confusing.

这篇关于Java 8:使用静态方法而不是静态util类的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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