[英]Differences between two singleton examples
Class A 1 A级1级
public class A {
private static final A instance = new A();
public static A getInstance() {
return new A();
}
}
Class A 2 A 2级
public class A {
private static final A instance = new A();
private A(){}
public static A getInstance() {
return instance;
}
}
I just start to learn singleton and I saw two java examples that using class A 1 example and class A 2 example. 我刚刚开始学习单例,并且看到了两个Java示例,分别使用A 1类示例和A 2类示例。 Is the class A 1
getInstance()
is singleton? A 1类
getInstance()
是单例吗? I also like to know what is the differences between these two class A getInstance()
method? 我还想知道这两个A类
getInstance()
方法之间的区别是什么? Thank you 谢谢
In A1, A
is not singleton .. getInstance()
is returning a new instance of A
everytime 在A1中,
A
不是单例 .. getInstance()
每次都返回A
的新实例
In A2, A
is not singleton again, cause the default constructor is still public
(implicitly) . 在A2中,
A
不再是单例 ,因为默认构造函数仍然是public
(隐式)。 One can easily create more instances from outside 一个人可以轻松地从外部创建更多实例
EDIT: 编辑:
Since you have edited the class A
in A2, now it becomes singleton. 由于您已经在A2中编辑了
A
类,因此现在成为单例。
Here A
is created eagerly and will be thread-safe by default. 此处
A
会急切创建,默认情况下将是线程安全的。 Check lazy vs eager intialization 检查懒惰与渴望初始化
I also like to know what is the differences between these two class A getInstance() method
我也想知道这两个A类getInstance()方法之间的区别
Class A 1: A类1:
If you look at the code : 如果您看一下代码:
public static A getInstance() {
return new A();
}
You are returning a new instance of A
on each call of getInstance()
method and hence it is not a Singleton. 您在每次调用
getInstance()
方法时都返回一个新的A
实例,因此它不是Singleton。 Also you have not made the default constructor private
in this case and any code outside your class can easily create instances of the class breaking the Singleton paradigm. 同样,在这种情况下,您没有将默认构造函数
private
,并且您的类之外的任何代码都可以轻松创建打破Singleton范例的类的实例。
Class A 2: A类2:
Looking at this code : 看这段代码:
public class A {
private static final A instance = new A();
private A(){}
public static A getInstance() {
return instance;
}
}
You are returning the same instance for each call of getInstance()
.Now your class behaves like Singleton , You are actually doing an eager instantiation of the Singleton instance here and this Singleton instance should be thread-safe. 您将为每次
getInstance()
调用返回相同的实例。现在您的类的行为类似于Singleton,实际上您正在此处急切地实例化Singleton实例,并且此Singleton实例应该是线程安全的。 Also make the class final
so that no one can sub class it and break the Singleton. 同时将课程定为
final
课程, final
使任何人都无法对其进行子分类并破坏Singleton。
In first case 在第一种情况下
each time a new instance of
A
is creating .每次创建
A
的新实例时。
* in second case * * 第二种情况*
As per single ton pattern ,it should be 按照单吨模式,应为
public class A {
private static A instance = null;
private A() {
}
public static A getInstance() {
if(instance == null) {
instance = new A();
}
return instance;
}
}
The class
A
maintains a static reference to the lone singleton instance and returns that reference from the static getInstance() method.类
A
维护对单例实例的静态引用,并从静态getInstance()方法返回该引用。
Is the class A 1 getInstance() is singleton? A 1类getInstance()是单例吗?
No because everytime you are calling this method, a new instance of A
is created. 否,因为每次调用此方法时,都会创建
A
的新实例。
I also like to know what is the differences between these two class A getInstance() method? 我还想知道这两个A类getInstance()方法之间的区别是什么?
The first getInstance()
will always create a new instance of class A, the second getInstance()
will always return the same instance created of class A. 第一个
getInstance()
将始终创建类A的新实例,第二个getInstance()
将始终返回创建类A的相同实例。
There are two ways of creating a singleton class
1, Dynamic Singleton
2, Static Singleton
Static Singleton :
public class A {
//Create a object of class A and make it final so that nobody can modify it
private static final A instance = new A();
//make the constructor private so that new objects can not be created
private A(){}
//provide a method to access the object
public static A getInstance() {
return instance;
}
}
Dynamic Singleton :
a, Single Check
public class A {
//Create a object reference of class A
private static A instance = null;
//make the constructor private so that new objects can not be created
private A() {}
public static A getInstance() {
//check if instance is null or not
if(instance == null) {
if(instance == null) {
//if null then create an instance of class A and assign it to the final reference
instance = new A();
}
}
return instance;
}
}
b, double check
public class A {
//Create a object reference of class A
private static A instance = null;
//make the constructor private so that new objects can not be created
private A() {}
public static A getInstance() {
//check if instance is null or not
if(instance == null) {
synchronized(A.class){
//Double Check
if(instance == null) {
//if null then create an instance of class A and assign it to the final reference
instance = new A();
}
}
}
return instance;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.