单例和静态实用程序类

发布于 2024-10-30 17:09:26 字数 166 浏览 0 评论 0原文

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

澄清

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

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 employ and I thought asking this community why and when may help clarify things for me a bit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

电影里的梦 2024-11-06 17:09:27

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

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

  public static final END_OF_SEQUENCE_MARKER=new EndMarker();

然后在遍历序列时,您可以只测试 if (object==END_OF_SEQUENCE_MARKER)。因为它是静态最终引用,所以 JIT 会将其转变为极其快速的测试...

编辑

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

  • 静态工厂类通常没有意义。工厂类的全部要点在于,您可以实例化它(或子类!),对工厂对象进行一些配置更改,然后使用它根据您需要的配置生成对象实例。如果您打算将其设为静态,那么您不妨创建一个静态 MyObject.create(..) 方法,而不是拥有整个静态 MyObjectFactory 类...
  • 同样,为什么要有一个单独的单例管理器类?通常管理单例的最佳类是单例类本身,因为假设您想保证只创建一个实例,那么您通常需要它来访问私有构造函数。只需一个简单的静态 MySingleton.getInstance() 方法通常就可以完成您需要的一切。

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).

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....

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.
终陌 2024-11-06 17:09:27

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

所以,是的,当你非常确定(例如数学)你永远不需要实例时,基本上使用静态实用方法;当你认为单个实例暂时足够好但可能需要实例时,请使用单例未来的变化(例如连接提供商)。

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 getInstance is made.

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).

旧伤还要旧人安 2024-11-06 17:09:27

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

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

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

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.

心凉 2024-11-06 17:09:26

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文