简体   繁体   English

java 中的 static 实用程序类的缺点?

[英]Cons of static utility classes in java?

What are the cons of creating static utility classes?创建 static 实用程序类有什么缺点? The more I've made, the more I find them extremely useful.我做得越多,我就越觉得它们非常有用。 I understand that they lack object oriented design, but I still love them probably more than I should.我知道他们缺乏面向 object 的设计,但我仍然可能比我应该更爱他们。 Are there any other cons for their use?他们的使用还有其他缺点吗?

There is nothing wrong with them, in the right context.在正确的上下文中,它们没有任何问题。 If you have free-standing, stateless methods (such as those found in java.lang.Math ), then a static class is the perfect place for them.如果您有独立的、无状态的方法(例如在java.lang.Math中找到的方法),那么 static class 是他们的理想选择。 The only reason they're in a class at all is because Java has no concept of free-standing methods.他们在 class 中的唯一原因是因为 Java 没有独立方法的概念。

The main disadvantage IMO is the impossibility, using most of the mocking frameworks, to mock the implementation of such utility methods in order to unit-test some class using these utility methods. IMO 的主要缺点是不可能使用大多数 mocking 框架来模拟此类实用方法的实现,以便使用这些实用方法对某些 class 进行单元测试。

For example, Using System.currentTimeMillis() is easy to get the current time.例如,使用System.currentTimeMillis()很容易获得当前时间。 But when you have to test a class which uses the current time to do some work, it's impossible to mock the method to make it return a specific point in time.但是当您必须测试使用当前时间做一些工作的 class 时,不可能模拟该方法以使其返回特定时间点。 Using an object implementing a Clock interface and injected into the object to test make it much easier: you can create a mock Clock implementation returning a specific date when asked to get the current time.使用实现Clock接口的 object 并将其注入 object 以进行测试会变得更加容易:您可以创建一个模拟时钟实现,在被要求获取当前时间时返回特定日期。

I had talked about this sometime ago in a post which you can find here .我前段时间在一篇文章中谈到了这个,你可以在这里找到。

The chief problems in the usage of a static method are:使用 static 方法的主要问题是:

  1. Mockability as the other posters pointed out.正如其他海报所指出的那样,可模仿性。
  2. Several versions of a static class can be loaded in an application depending on the class loader hierarchy and class path configured for the entire hierarchy of class loaders. Several versions of a static class can be loaded in an application depending on the class loader hierarchy and class path configured for the entire hierarchy of class loaders. (A third element is also if the class loader is configured as parent first or child first) Makes testing and debugging this a nightmare. (第三个因素也是 class 加载程序配置为父优先或子优先)使测试和调试成为一场噩梦。
  3. A static method cannot be inherited from.不能从 static 方法继承。 Hence violates OCP (Open Closed Principle) ie a static method is not extensible.因此违反了OCP(开放封闭原则) ,即 static 方法不可扩展。 Look at Log4J for typical problems with static methods.查看 Log4J 以了解 static 方法的典型问题。
  4. A static method is not amenable for the application of either the Interface Segregation Principle or the Strategy pattern. static 方法不适用于接口隔离原则策略模式的应用。 It is not possible to have alternate implementations of a similar algorithm depending on individual use cases without recourse to copious amounts of hand written code sprayed liberally with "if conditions".如果不求助于大量带有“如果条件”的手写代码,就不可能根据个别用例来替代类似算法的实现。

But if the method itself is not too cumbersome and is pretty much predictable (ie not too many requirements for different variations of the implementations etc.) then there is no reason why a static method would not fit the bill.但是,如果该方法本身不太麻烦并且几乎是可预测的(即,对实现的不同变体等没有太多要求),那么 static 方法没有理由不符合要求。 So, moral of the story is Use it but be aware of side affects.所以,这个故事的寓意是使用它,但要注意副作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM