Java单例模式,它是如何工作的?

huangapple go评论66阅读模式
英文:

Java singleton, how it works?

问题

我不完全理解Java中的Singleton类的概念。例如,我有这样的单例类:

private int i = 0;
private static Singleton INSTANCE = new Singleton();

public static Singleton getInstance() {
    return INSTANCE;
}

public int getI() {
    return i;
}

public void setI(int i) {
    this.i = i;
}

所以每次我调用getInstance(),它不是因为new Singleton()而创建一个新的Singleton对象吗?

那么为什么如果我这样初始化它:

Singleton s = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();

s.setI(5);

System.out.println(s.getI());

System.out.println(s2.getI());

我得到了两次'5'。难道第二次不应该写0给我吗?我确实有点不明白,但不知道是什么。

英文:

I don't fully understand the concept of Singleton class in Java. For example I have singleton class like this:

private int i = 0;
private static Singleton INSTANCE = new Singleton();

public static Singleton getInstance() {
    return INSTANCE;
}

public int getI() {
    return i;
}

public void setI(int i) {
    this.i = i;
}

So every time I call getInstance() isn't it creating a new Object of Singleton because of this: new Singleton() ?

Then why if I initializes it like this :

Singleton s = Singleton.getInstance();
    Singleton s2 = Singleton.getInstance();

    s.setI(5);

    System.out.println(s.getI());

    System.out.println(s2.getI());

I'm getting '5' two times.
Shouldn't it write 0 to me the second time? I'm truly missing something but no idea what.

答案1

得分: 1

INSTANCE = new Singleton();

只会被调用一次,因为它是静态的。此后:

getInstance()

只会返回那个静态实例

英文:
    INSTANCE = new Singleton();

would only get called once as it is static. Thereafter:

    getInstance()

only ever returns that static instance

答案2

得分: 0

Singleton是一个只涉及一个类的模式,该类负责创建一个对象,同时确保只创建一个对象。

这个类提供了一种访问它唯一的对象的方式,可以直接访问,无需实例化该类的对象。

要实现它,您应该:

  1. 私有构造函数,以限制其他类从该类实例化。
  2. 同一类的私有静态变量,即类的唯一实例。
  3. 公共静态方法,返回该类的实例,这是外部世界获取单例类实例的全局访问点。
public class SingleObject {

    private int i = 0;
    // 创建SingleObject的对象
    private static SingleObject instance = new SingleObject();

    // 将构造函数设为私有,以防止此类被实例化
    private SingleObject() {}

    // 获取唯一可用的对象
    public static SingleObject getInstance() {
        return instance;
    }

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }

}
英文:

Singleton is a just pattern involves a single class which is responsible to create an object while making sure that only single object gets created.

This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class.

and to Implement that u should :

  1. Private constructor to restrict instantiation of the class from other classes.

  2. Private static variable of the same class that is the only instance of the class.

  3. Public static method that returns the instance of the class, this is the global access point for outer world to get the instance of the singleton class.

    public class SingleObject {

    private int i = 0;
    //create an object of SingleObject
    private static SingleObject instance = new SingleObject();
    
    //make the constructor private so that this class cannot be
    //instantiated
    private SingleObject() {}
    
    //Get the only object available
    public static SingleObject getInstance() {
        return instance;
    }
    
    public int getI() {
        return i;
    }
    
    public void setI(int i) {
        this.i = i;
    }
    

    }

huangapple
  • 本文由 发表于 2020年8月8日 21:34:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63315991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定