单例和静态实用程序类
哪些因素会影响适当的设计模式的使用?
澄清:
我问这个问题的原因是因为我正在设计一个需要多个静态工厂类和单例管理器类的应用程序。有时,我对应该采用哪种设计感到困惑,我想询问这个社区为什么和何时可能会帮助我澄清一些事情。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我将静态实用程序类用于将从许多不同上下文调用的共享函数 - 例如类似于 java.util.Math 中的数学函数。这是一个适当的模式,假设这些是“纯”函数(即除了给定的参数之外不操作任何状态或访问任何数据)。
我很少使用单例,特别是尽量避免全局单例。它们遭受与全局变量相关的所有常见问题。它们使测试变得困难,并且除非您的单例也是不可变的,否则它们会引入全局状态问题。我发现它们有用的主要地方是依赖于对象标识的性能黑客 - 例如:
然后在遍历序列时,您可以只测试 if (object==END_OF_SEQUENCE_MARKER)。因为它是静态最终引用,所以 JIT 会将其转变为极其快速的测试...
编辑
刚刚看到您的澄清,一些快速的额外评论:
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:
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:
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).我不确定这里的问题是什么。
单例模式用于实例的状态可以在多次调用中保留或更改的情况 - 这可能是连接池或类提供访问的其他共享对象。
静态实用程序类用于每个单独的方法都是无状态的,并且与该类提供的其他方法无关。
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.
当需要实例化单个对象并且所有请求的对象访问都通过该特定实例时,使用单例。如果需要,该对象可以保持状态。
当您有一个只是无状态实用函数的类时,使用静态实用程序。它不维护状态。对象的实例永远不会被实例化。
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.