为什么有java单例类?你什么时候需要使用一个 [英] why are there java singleton classes? When would you need to use one

查看:180
本文介绍了为什么有java单例类?你什么时候需要使用一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一个单例类是只有一个实例化的类,但是我不明白为什么这样会是有用的。为什么不用静态变量和方法创建一个类,如果需要,使用同步来确保没有两个线程同时执行类中的方法。我只是不知道为什么任何人会遇到创造这种类的麻烦。我知道我在这里缺少一些东西。

I understand that a singleton class is one where there can be only one instantiation, but I don't understand why this would be useful. Why won't you just create a class with static variables and methods and use synchronize if needed to make sure that no two threads were executing a method in the class simultaneously. I just don't get why anyone would go through the trouble of creating this kind of class. I know I'm missing something here.

谢谢,

推荐答案

>虽然我同意其他的答案,OP是问为什么没有一个类有所有静态方法(可能是静态字段),而不是一个单例,你有一个实例。

While I agree with the other answers, the OP was asking why not have a class with all static methods (possibly with static fields) instead of a singleton where you have one instance.

为什么要使用单身人士?

您可以通过Googlesingleton查找各种原因。从 JavaWorld

You can Google "singleton" to find all sorts of reasons. From JavaWorld:


有时候,
只有一个类的一个实例是合适的:
窗口管理器,打印后台处理程序和
文件系统是原型例子。
通常,这些类型的
对象(称为单例)是
,由不同的对象
在整个软件系统中访问,因此
需要一个全局点
访问。当然,只要你
确定你永远不会需要超过
一个实例,这是一个很好的赌注,你会
改变主意。

Sometimes it's appropriate to have exactly one instance of a class: window managers, print spoolers, and filesystems are prototypical examples. Typically, those types of objects—known as singletons—are accessed by disparate objects throughout a software system, and therefore require a global point of access. Of course, just when you're certain you will never need more than one instance, it's a good bet you'll change your mind.

为什么使用一个Singleton而不是一个使用所有静态方法的类?

很少的原因


  1. 您可以使用继承

  2. 您可以使用界面

  3. 它使单个类本身的单元测试变得更容易

  4. 它可以对依赖于单例的代码进行单元测试

  1. You could use inheritance
  2. You can use interfaces
  3. It makes it easier to do unit testing of the singleton class itself
  4. It makes it possible to do unit testing of code that depends on the singleton

对于#3,如果您的Singleton是数据库连接池,则要确保您的应用程序只有一个实例,但对数据库连接池进行单元测试本身没有碰到数据库(可能通过使用包范围构造函数或静态创建方法):

For #3, if your Singleton was a database connection pool, you want to insure that your application has only one instance, but do unit testing of the database connection pool itself without hitting the database (possibly by using a package-scope constructor or static creational method):

public class DatabaseConnectionPool {
  private static class SingletonHolder {
    public static DatabaseConnectionPool instance = new DatabaseConnectionPool(
        new MySqlStatementSupplier());
  }

  private final Supplier<Statement> statementSupplier;

  private DatabaseConnectionPool(Supplier<Statement> statementSupplier) {
    this.statementSupplier = statementSupplier;
  }

  /* Visibile for testing */
  static DatabaseConnectionPool createInstanceForTest(Supplier<Statement> s) {
    return new DatabaseConnectionPool(s);
  }

  public static DatabaseConnectionPool getInstance() {
    return SingletonHolder.instance;
  }

  // more code here
}

(注意使用初始化开需求持有者模式)

然后,您可以使用package-scope来测试 DatabaseConnectionPool createInstanceForTest 方法。

You can then do testing of the DatabaseConnectionPool by using the package-scope createInstanceForTest method.

但是,请注意,静态 getInstance() code>方法可能导致静态粘贴,其中依赖于您的单例的代码无法进行单元测试。 由于这种原因,静态单身人士通常不被认为是一个很好的做法(见这个博文

Note, however, that having a static getInstance() method can cause "static cling", where code that depends on your singleton cannot be unit tested. Static singletons are often not considered a good practice because of this (see this blog post)

相反,你可以使用像Spring或Guice这样的依赖注入框架来确保你的类只有一个生产实例,同时仍然允许使用该类的代码是可测试的。由于Singleton中的方法不是静态的,所以您可以使用像JMock这样的嘲笑框架来模拟单身人士的测试。

Instead, you could use a dependency injection framework like Spring or Guice to insure that your class has only one instance in production, while still allowing code that uses the class to be testable. Since the methods in the Singleton aren't static, you could use a mocking framework like JMock to mock your singleton in tests.

这篇关于为什么有java单例类?你什么时候需要使用一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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