设计模式之单例模式(重点)

本文探讨设计模式中的单例模式,详细解释其用途,如控制实例数量、提升垃圾回收效率,以及作为多线程间通信的媒介。同时,介绍了实现单例模式的两种常见方法:懒汉模式和饿汉模式,强调了构造函数的私有化在单例模式中的关键作用。

设计模式之单例模式

单例模式的用途
单例模式保证了系统中某个类只有一个实例,而且自行实例化并向整个系统提供自己。在计算机系统中,线程池、日志对象、缓存、对话框、打印机等都被设计成单例。spring默认也是单例模式,单例模式一方面可以控制实例产生的数量,提高垃圾回收的效率,另一方面也可以作为通信的媒介,使不同线程实现通信。
单例模式的写法:
关键是用private关键字修饰构造函数,使程序中不能通过new的方法创建对象。然后用懒汉模式或者饿汉模式来实现单例模式。

package designMode.singleton;

public class SingleLazyUnsafe {

    public void print(){
        System.out.println(SingleLazyUnsafe.class.getName());
    }

    //构造函数定义为私有的,防止被调用
    private SingleLazyUnsafe(){};

    private static SingleLazyUnsafe instance;

    //若instance为空,进行初始化,运行时初始化,if判断没有加synchronized,线程不安全
    public static SingleLazyUnsafe getInstance(){
        if(instance == null){
            instance = new SingleLazyUnsafe();
        }
        return instance;
    }
}
package designMode.singleton;

public class SingleLazySafeOne {

    public void print(){
        System.out.println(SingleLazySafeOne.class.getName());
    }

    //构造函数定义为私有的,防止被调用
    private SingleLazySafeOne(){};

    private static SingleLazySafeOne instance;

    //若instance为空,进行初始化,运行时初始化,线程安全,但并发效率低
    public static synchronized SingleLazySafeOne getInstance(){
        if(instance == null){
            instance = new SingleLazySafeOne();
        }
        return instance;
    }
}
package designMode.singleton;

public class SingleLazySafeTwo {

    public void print(){
        System.out.println(SingleLazySafeTwo.class.getName());
    }

    //构造函数定义为私有的,防止被调用
    private SingleLazySafeTwo(){};

    private static volatile SingleLazySafeTwo instance;

    //若instance为空,进行初始化,运行时初始化,代码块加synchronized线程安全,效率高
    public static SingleLazySafeTwo getInstance(){
        if(instance == null){
            synchronized(SingleLazySafeTwo.class){
                if(instance == null){
                    //此处由于new 不是原子性动作,因此instance需要用volatile进行修饰
                    instance = new SingleLazySafeTwo();
                }
            }
        }
        return instance;
    }
}
package designMode.singleton;

public class SingleHungry {

    public void print(){
        System.out.println(SingleHungry.class.getName());
    }

    private SingleHungry(){};

    private static SingleHungry instance = new SingleHungry();

    public static SingleHungry getInstance(){
        return instance;
    }
}
package designMode.singleton;

public class SingleFinal {

    public void print(){
        System.out.println(SingleFinal.class.getName());
    }

    private SingleFinal(){}

    // 静态内部类只有在被调用时才初始化,且只会被初始化一次
    private static class InstanceHolder{

        private static final SingleFinal instance = new SingleFinal();
    }

    public static SingleFinal getInstance(){    
        return InstanceHolder.instance;
    }

}
package designMode.singleton;
//枚举方式实现单例模式
public enum SingleEnum {

    INSTANCE;

    public void print(){
        System.out.println(SingleEnum.class.getName());
    }

}
package designMode.singleton;

//测试用例
public class SingleObject {

    public static void main(String[] args){
        SingleLazyUnsafe singleLazyUnsafe = SingleLazyUnsafe.getInstance();
        singleLazyUnsafe.print();

        SingleLazySafeOne singleLazySafeOne = SingleLazySafeOne.getInstance();
        singleLazySafeOne.print();

        SingleLazySafeTwo singleLazySafeTwo = SingleLazySafeTwo.getInstance();
        singleLazySafeTwo.print();

        SingleHungry singleHungry = SingleHungry.getInstance();
        singleHungry.print();

        SingleFinal singleFinal = SingleFinal.getInstance();
        singleFinal.print();

        SingleEnum singleEnum = SingleEnum.INSTANCE;
        singleEnum.print();
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值