1、我们有一个类,如下:
public class Singleton {
}
2、当我们要使用这个类时,需要创建一个它的实例,如下:
public class Test {
public static void main(String[] args) {
Singleton s = new Singleton();
}
}
3、当我们在不同地方使用Singleton这个类时,就会创建多个它的实例,为了便于演示,我们假设在Test类中多次使用Singleton这个类,如下:
public class Test {
public static void main(String[] args) {
Singleton s1 = new Singleton();
Singleton s2 = new Singleton();
if(s1==s2){
System.out.println("s1与s2是同一个实例");
}else{
System.out.println("s1与s2不是同一个实例");
}
}
}
【运行结果】:s1与s2不是同一个实例
4、以上说明,在3中,我们new出了两个不同的Singleton实例。
5、修改Singleton类,避免在外部被随便new出实例,要在Singleton的默认构造方法添加private修饰,如下:
public class Singleton {
private Singleton(){
}
}
6、这样,外部就不能new出Singleton的实例了,所有还需要对外开放获得Singleton实例的方法,如下:
public class Singleton {
private Singleton(){
}
static Singleton instance = new Singleton();
}
public class Test {
public static void main(String[] args) {
Singleton s1 = Singleton.instance;
Singleton s2 = Singleton.instance;
if(s1==s2){
System.out.println("s1与s2是同一个实例");
}else{
System.out.println("s1与s2不是同一个实例");
}
}
}
【运行结果】:s1与s2是同一个实例
7、虽然是达到了new出同一个实例的要求,但是这样写不安全,外部可以去修改这个instance,所以要把instance这个常量私有化,修改后的代码如下:
public class Singleton {
//将构造方法私有化,禁止外部直接创建对象
private Singleton(){
}
//创建类的唯一实例,使用private static修饰
private static Singleton instance = new Singleton();
//提供一个外部获取实例的方法,使用public static修饰
public static Singleton getInstance(){
return instance;
}
}
public class Test {
public static void main(String[] args) {
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
if(s1==s2){
System.out.println("s1与s2是同一个实例");
}else{
System.out.println("s1与s2不是同一个实例");
}
}
}
【运行结果】:s1与s2是同一个实例
8、上面是饿汉式单例,因为只要Singleton生成,用static修饰的常量instance就会生成。下面是懒汉式单例:
public class Singleton {
//将构造方法私有化,禁止外部直接创建对象
private Singleton(){
}
//创建类的唯一实例,使用private static修饰
private static Singleton instance = null;
//提供一个外部获取实例的方法,使用public static修饰
public static Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
}
9、饿汉模式与懒汉模式的区别
【饿汉模式】:加载类时比较慢,运行时获取对象实例的速度快,线程安全
【懒汉模式】:加载类时比较快,运行时获取对象实例的速度慢,线程不安全