更新时间:2022-05-30 05:21:27
过去,如果你有一个接口 Foo
并想要分组界面 - 相关的utils或工厂方法,您需要创建一个单独的utils类 FooUtils
并将所有内容存储在那里。
In the past, if you had an interface Foo
and wanted to group interface-related utils or factory methods, you would need to create a separate utils class FooUtils
and store everything there.
这些类除了名称之外没有任何共同点,另外,需要将utils类设为 final
并有一个私有构造函数来禁止不必要的使用。
Those classes would not have anything in common other than the name, and additionally, the utils class would need to be made final
and have a private constructor to forbid unwanted usage.
现在,由于接口静态方法,您可以将所有内容保存在一个位置而无需创建任何其他类。
Now, thanks to the interface static methods, you can keep everything in one place without creating any additional classes.
同样重要的是不要忘记所有良好实践,不要盲目地将所有内容抛给一个接口类 - 正如这个答案
It's also important to not forget all good practices and not throw everything mindlessly to one interface class - as pointed out in this answer