在Java中创建无状态实用程序类的最佳实践是什么 [英] Whats the best practice for creating Stateless Utility classes in Java

查看:72
本文介绍了在Java中创建无状态实用程序类的最佳实践是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中创建实用程序(不包含任何状态)类的最佳实践是什么.

What's the best practice for creating the Utility (which do not hold any state) classes in Java.

在大多数情况下,我们最终会为此类任务创建静态方法.其他可能的方式可能是创建单例对象"以执行此操作.

In most of the cases we end up creating static methods for such tasks. Other possible way could be "create the singleton objects" for performing this operation.

当要求代码应易于进行单元测试时,设计上应考虑什么?

What should be the design consideration when requirement is the code should be easily unit testable?

推荐答案

如果您沉迷于我的隐喻……

If you’ll indulge my metaphor for a bit…

您以前可能已经看过其中之一:

You probably have seen one of these before:

请注意,我们称它为烤面包机.我们将其称为"BreadUtil".

Notice that we call it a toaster. We do not call it a "BreadUtil."

类似地,实用程序方法可以并且应该放在为特定功能命名的类中,而不是与面包相关的杂项".

Similarly, utility methods can and should be placed in a class named for specific functionality, not "miscellaneous stuff related to bread."

大多数时候,您的静态方法属于一个相关的类.例如 Integer.parseInt 是Integer类的静态方法,不是理论上的IntegerUtil或NumberUtil类的成员.

Most of the time, your static methods belong on a related class; for instance, Integer.parseInt is a static method of the Integer class, not a member of a theoretical IntegerUtil or NumberUtil class.

过去,创建单独的实用程序类的一种情况是,当感兴趣的主要类是接口时.例如, java.util.Collections .但是,从Java 8开始,这不是借口,因为接口可以具有静态方法和默认方法.实际上,Collections.sort(List)已经迁移到

In the past, one case for creating a separate utility class was when the primary class of interest was an interface. An example of this is java.util.Collections. However, as of Java 8, this is not an excuse, as interfaces can have static methods and default methods. In fact, Collections.sort(List) has already been migrated to List.sort.

如果您有很多实用程序方法,并且觉得它们会使相关的类杂乱无章,则可以将它们放在单独的类中,而不要放在"BreadUtil"类中.在类名(或"utils","utilities","misc","miscellaneous","general","shared","common"或"framework")中放入"util"一词是绝对不能接受的..给该类一个有意义的名称,该名称描述方法的用途.如果方法过于多样化而无法使用这样的类名,则可能需要将它们分成多个类.(只有少数几种方法的小型类是完全可以接受的;许多人甚至认为这种设计不错.)

If you have a lot of utility methods and you feel they would clutter the relevant class, it’s fine to put them in a separate class, but not a "BreadUtil" class. It is never acceptable to put the word "util" in a class name (or "utils," "utilities," "misc," "miscellaneous," "general," "shared," "common," or "framework"). Give the class a meaningful name that describes what the methods are for. If the methods are too diverse to allow for such a class name, you probably need to split them up into multiple classes. (Small classes with only a few methods are perfectly acceptable; many people even consider that good design.)

回到Integer示例,如果您觉得方法使类混乱,则可以创建如下这样的新类:

Going back to the Integer example, if you felt the methods were cluttering the class, you could create new classes like this:

public class IntegerMath {
    private IntegerMath() { }

    public static int compare(int x, int y) { /* ... */ }
    public static int compareUnsigned(int x, int y) { /* ... */ }
    public static int divideUnsigned(int dividend, int divisor) { /* ... */ }
    public static int min(int a, int b) { /* ... */ }
    public static int max(int a, int b) { /* ... */ }
    public static int remainderUnsigned(int dividend, int divisor) { /* ... */ }
    public static int signum(int i) { /* ... */ }
    public static int sum(int a, int b) { /* ... */ }
    public static long toUnsignedLong(int i) { /* ... */ }
}

public class IntegerBits {
    private IntegerBits() { }

    public static int bitCount(int i) { /* ... */ }
    public static int highestOneBit(int i) { /* ... */ }
    public static int lowestOneBit(int i) { /* ... */ }
    public static int numberOfLeadingZeros(int i) { /* ... */ }
    public static int numberOfTrailingZeros(int i) { /* ... */ }
    public static int reverse(int i) { /* ... */ }
    public static int reverseBytes(int i) { /* ... */ }
    public static int rotateLeft(int i, int distance) { /* ... */ }
    public static int rotateRight(int i, int distance) { /* ... */ }
}

public class IntegerParser {
    private IntegerParser() { }

    public static int parseInt(String s) { /* ... */ }
    public static int parseInt(String s, int radix) { /* ... */ }
    public static int parseUnsignedInt(String s) { /* ... */ }
    public static int parseUnsignedInt(String s, int radix) { /* ... */ }
}

最后一个是没有静态方法可能会更好的示例:

The last of those is an example of something that might be better without static methods:

public class IntegerParser {
    public IntegerParser() { this(10); }
    public IntegerParser(int radix) { /* ... */ }

    public int parseInt(String s) { /* ... */ }
    public int parseUnsignedInt(String s) { /* ... */ }
}

这篇关于在Java中创建无状态实用程序类的最佳实践是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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