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

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

问题描述

当我需要一堆无状态实用程序方法时,Java 8 中的最佳实践是什么.拥有一个不会被任何人实现的接口是否正确,例如public interface Signaturespublic interface Environments,或者更好它是旧的方式 - 具有带有私有构造函数的 public final class Signaturespublic final class Environments ||枚举?

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 中的 StreamCollector 接口.每个都有静态工厂: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().

但还要注意,每个都有配套的实用程序类 StreamSupportCollectors.这些是纯实用程序类,仅包含静态方法.可以说它们可以合并到相应的接口中,但这会使接口变得混乱,并且会模糊类中包含的方法之间的关系.例如,StreamSupport 包含一系列相关的静态方法,它们都是SpliteratorStream 之间的适配器.将这些合并到 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天全站免登陆