单例和静态实用程序类 [英] Singleton and Static Utility classes

查看:92
本文介绍了单例和静态实用程序类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么因素影响适合的设计模式?



澄清:



我问这个问题的原因是因为我设计一个需要多个静态工厂类和单例管理器类的应用程序。有时候,我对于员工的设计感到困惑,而且我认为问这个社区为什么 可能会帮助我澄清一些事情。

解决方案

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



我很少使用单身,特别是试图避免全球单身人士。他们遭受与全球变量相关的所有常见问题的困扰。他们使测试变得困难,除非你的单身人士也是不可变的,他们会引入全球国家的问题。我发现它们的主要地方是在取决于对象身份的性能黑客中,例如:

  public static final END_OF_SEQUENCE_MARKER = new EndMarker(); 

然后当遍历一个序列时,你可以测试if(object == END_OF_SEQUENCE_MARKER)。因为它是一个静态的最终参考,所以JIT会把它变成一个非常快速的测试....



编辑



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




  • 静态工厂类通常不会感。工厂类的全部要点是您可以实例化它(或一个子类!),对工厂对象进行一些配置更改,然后根据所需的配置使用它来生成对象实例。如果你要使它静态,你可以创建一个静态MyObject.create(..)方法,而不是一个整个静态的MyObjectFactory类....

  • 同样的为什么要有单独的单身经理班?通常,管理单例的最好的类是单例类,因为您通常需要它来访问私有构造函数,假设您要保证只创建一个实例。只需要一个简单的静态MySingleton.getInstance()方法就可以执行所需的一切。


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 why and when may help clarify things for me a bit.

解决方案

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.

这篇关于单例和静态实用程序类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆