首页 > 其他 > 详细

09 单例模式

时间:2021-08-23 19:20:47      阅读:13      评论:0      收藏:0      [点我收藏+]

单例模式

饿汉式

//饿汉式单例
public class Hungry {

    //可能会浪费空间
    private byte[] data1 = new byte[1024];

    private Hungry(){

    }

    private static final Hungry HUNGRY = new Hungry();

    public static Hungry  getInstance(){
        return HUNGRY;
    }
}

最基础懒汉式单例模式

//懒汉式单例模式
public class LazyMan {

    private LazyMan(){
        System.out.println(Thread.currentThread().getName()+"ok");
    }

    private static LazyMan lazyMan;

    public static LazyMan getInstance(){
        if (lazyMan == null){
            lazyMan = new LazyMan();
        }
        return lazyMan;
    }

    public static void main(String[] args) {
        //多线程下并发会出问题
        for (int i = 0; i < 20; i++) {

            new Thread(()->{
                LazyMan.getInstance();
            }).start();
        }
    }
}

解决多线程并发下的问题 DCL懒汉式

//懒汉式单例模式  
public class LazyMan {

    private LazyMan(){
        System.out.println(Thread.currentThread().getName()+"ok");
    }

    private volatile static LazyMan lazyMan;

    //双重检测锁模式的懒汉式单例  DCL懒汉式
    public static LazyMan getInstance(){
        if (lazyMan == null){
            synchronized (LazyMan.class){
                if (lazyMan == null){
                    lazyMan = new LazyMan();//不是原子性操作  要加上volatile 避免指令重排
                    /*
                    * 1.分配内存空间
                    * 2.执行构造器方法,初始换对象
                    * 3.把这个对象指向这个空间
                    * */
                }
            }
        }
        return lazyMan;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {

            new Thread(()->{
                LazyMan.getInstance();
            }).start();
        }
    }
}

静态内部类

//静态内部类
public class Holder {
    private Holder(){

    }

    public static Holder getInstance(){
        return InnerClass.HOLDER;
    }

    public static class InnerClass{
        private static final Holder HOLDER = new Holder();
    }
}

发现单例模式可以被反射破坏

//懒汉式单例模式
public class LazyMan {

    private static boolean flag = false;

    private LazyMan(){

        synchronized (LazyMan.class){

            if (flag == false){
                flag = true;
            }else{
                throw new RuntimeException("不要试图用反射破坏异常");
            }
        }

        System.out.println(Thread.currentThread().getName()+"ok");
    }

    private volatile static LazyMan lazyMan;

    //双重检测锁模式的懒汉式单例  DCL懒汉式
    public static LazyMan getInstance(){
        if (lazyMan == null){
            synchronized (LazyMan.class){
                if (lazyMan == null){
                    lazyMan = new LazyMan();//不是原子性操作  要加上volatile 避免指令重排
                    /*
                    * 1.分配内存空间
                    * 2.执行构造器方法,初始换对象
                    * 3.把这个对象指向这个空间
                    * */
                }
            }
        }
        return lazyMan;
    }

    public static void main(String[] args) throws Exception {
        //反射破环单例模式
        LazyMan instance = LazyMan.getInstance();
        Constructor<LazyMan> declaredConstructor = LazyMan.class.getDeclaredConstructor();
        declaredConstructor.setAccessible(true);
        LazyMan instance2 = declaredConstructor.newInstance();

        System.out.println(instance);
        System.out.println(instance2);

    }
}

枚举

//枚举 本身也是一个Class 类
public enum EnumSingle {

    INSTANCE;
    public EnumSingle getInstance(){
        return INSTANCE;
    }
}

class Test{

    public static void main(String[] args) throws Exception{
        EnumSingle instance1 = EnumSingle.INSTANCE;

        Constructor<EnumSingle> declaredConstructor = EnumSingle.class.getDeclaredConstructor(String.class, int.class);
        declaredConstructor.setAccessible(true);
        EnumSingle instance2 = declaredConstructor.newInstance();

        System.out.println(instance1);
        System.out.println(instance2);
    }
}

枚举类型不能使用反射会报错
技术分享图片

09 单例模式

原文:https://www.cnblogs.com/flypigggg/p/15177159.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!