java 单例和静态实用程序类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5582881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:46:39  来源:igfitidea点击:

Singleton and Static Utility classes

javadesign-patterns

提问by mre

What factors influence the appropriate design pattern to use?

哪些因素会影响要使用的适当设计模式?

Clarification:

澄清:

The reason I ask this question is because I'm designing an application that requires multiple static factory classes and singleton manager classes. At times, I become confused as to which design I should employee and I thought asking this community whyand whenmay help clarify things for me a bit.

我问这个问题的原因是因为我正在设计一个需要多个静态工厂类和单例管理器类的应用程序。有时,我对我应该采用哪种设计感到困惑,我想询问这个社区为什么以及何时可能有助于我澄清一些事情。

采纳答案by mikera

I use static utility classes for shared functions that will be called from many different contexts- e.g. maths functions similar to those in java.util.Math. This is an appropriate pattern assuming that these are "pure" functions (i.e. don't manipulate any state or access any data other than than the parameters they are given).

我将静态实用程序类用于共享函数,这些函数将从许多不同的上下文中调用- 例如类似于 java.util.Math 中的数学函数。这是一个适当的模式,假设这些是“纯”函数(即,除了给定的参数之外,不操纵任何状态或访问任何数据)。

I very rarely use singletons, and in particular try to avoid global singletons. They suffer from all the usual problems associated with global variables. They make testing difficult, and unless your singleton is also immutable they introduce problems of global state. The main place I have found them useful is in performance hacks that depend on object identity- for example:

我很少使用单例,特别是尽量避免全局单例。它们遭受与全局变量相关的所有常见问题。它们使测试变得困难,除非您的单例也是不可变的,否则它们会引入全局状态问题。我发现它们有用的主要地方是依赖于对象身份的性能黑客- 例如:

  public static final END_OF_SEQUENCE_MARKER=new EndMarker();

Then when traversing a sequence you can just test if (object==END_OF_SEQUENCE_MARKER). Because it's a static final reference, the JIT will turn this into an extremely fast test....

然后在遍历序列时,您可以测试 if (object==END_OF_SEQUENCE_MARKER)。因为它是一个静态的最终引用,JIT 将把它变成一个非常快的测试......

EDIT

编辑

Having just seen your clarification, some quick extra comments:

刚刚看到你的澄清,一些快速的额外评论:

  • Static factory classes don't usually make sense. The whole point of a factory class is that you can instantiate it (or a subclass!), make some configuration changes on the factory object, then use it to generate object instances according to the configuration that you need. If you're going to make it static, you might as well just create a static MyObject.create(..) method rather than having a whole static MyObjectFactory class....
  • Likewise, why have a separate singleton manager class? Usually the best class to manage the singleton is the singleton class itself, since you will typically need it to access a private constructor, assuming you want to guarantee that only one instance will ever be created. Just having a simple static MySingleton.getInstance() method will usually do everything that you need.
  • 静态工厂类通常没有意义。工厂类的全部意义在于您可以实例化它(或子类!),对工厂对象进行一些配置更改,然后根据您需要的配置使用它来生成对象实例。如果您打算将其设为静态,您不妨创建一个静态 MyObject.create(..) 方法,而不是拥有一个完整的静态 MyObjectFactory 类....
  • 同样,为什么有一个单独的单例管理器类?通常管理单例的最佳类是单例类本身,因为您通常需要它来访问私有构造函数,假设您想保证只会创建一个实例。只需一个简单的静态 MySingleton.getInstance() 方法通常就可以完成您需要的一切。

回答by roberttdev

Singleton is used when a single object needs to be instantiated and all requested object access goes through this particular instance. This object can maintain state if desired.

当需要实例化单个对象并且所有请求的对象访问都通过此特定实例时,将使用单例。如果需要,该对象可以保持状态。

Static Utility is used when you have a class that is just stateless utility functions.. it does not maintain state. An instance of the object is never instantiated.

当您拥有一个只是无状态实用程序函数的类时,将使用静态实用程序。它不维护状态。永远不会实例化对象的实例。

回答by Sanjay T. Sharma

IMO static utility classes chalk down a concrete contract between the caller and the class. This is different than singletons wherein you can change the implementation behind the scenes to make your so called 'singleton' provider hand out a new instance each time a call to getInstanceis made.

IMO 静态实用程序类在调用者和类之间制定了具体的合同。这与单例不同,在单例中,您可以更改幕后的实现,使您所谓的“单例”提供程序在每次调用时分发一个新实例getInstance

So yes, basically use static utility methods when you are damn sure (e.g. Math) you'd never need an instance and use singletons when you think that a single instance is good enough for the time being but might change in the future (e.g. connection providers).

所以是的,当您确信(例如Math)您永远不需要实例时,基本上使用静态实用程序方法,而当您认为单个实例暂时足够好但将来可能会改变(例如连接提供程序)时,请使用单例)。

回答by AndyT

I'm not sure what the question is here.

我不确定这里的问题是什么。

Singleton patterns are used where the instance has state that may be preserved or altered across a number of calls - this might be a connection pool or some other shared object that the class provides access to.

单例模式用于实例具有可以在多次调用中保留或更改的状态 - 这可能是连接池或类提供访问的其他一些共享对象。

Static utility classes are used where each individual method is stateless, and has no bearing on the other methods that the class provides.

静态实用程序类用于每个单独的方法都是无状态的,并且与类提供的其他方法无关。