码迷,mamicode.com
首页 > 其他好文 > 详细

单例模式

时间:2020-06-05 21:20:06      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:hold   print   oca   思想   declared   msi   ssi   饿汉式   win   

单例模式

重要思想:构造器私有,保证内存中只有一个对象

饿汉式

public class Hungry {
    private Hungry(){
    }
    private final static Hungry HUNGRY = new Hungry();

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

懒汉式

public class LazyMan {
    private LazyMan(){}
    private volatile static LazyMan lazyMan;
    //双重检测锁模式
    public static LazyMan getInstance(){
        if (lazyMan==null){
            synchronized (LazyMan.class){
                if (lazyMan == null){
                    LazyMan lazyMan = new LazyMan();
                }
            }
        }
        return lazyMan;
    }
}




public class LazyMan {
    private LazyMan(){

        synchronized (LazyMan.class){
            if (lazyMan!=null){
                throw new RuntimeException("禁止反射");
            }
        }
        System.out.println(Thread.currentThread().getName()+"ok");
    }
    private volatile static LazyMan lazyMan;
    //双重检测锁模式
    public static LazyMan getInstance(){
        if (lazyMan==null){
            synchronized (LazyMan.class){
                if (lazyMan == null){
                    lazyMan = new LazyMan();
                }
            }
        }
        return lazyMan;
    }

    public static void main(String[] args) throws Exception {
        LazyMan instance = LazyMan.getInstance();
        Constructor<LazyMan> constructor = LazyMan.class.getDeclaredConstructor(null);
        constructor.setAccessible(true);
        LazyMan instance1 = constructor.newInstance();
        System.out.println(instance);
        System.out.println(instance1);
    }
}
public class LazyMan{
    public LazyMan(){
        
    }
    private volatile static LazyMan lazyMan;
    public static LazyMan getInstanace(){
        if(lazyMan==null){
            synchronized(Lazyman.class){
                if(lazyMan==null){
                    lazyMan = new LazyMan();
                }
            }
        }
        return lazyMan;
    }
}

静态内部类式

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

枚举

public enum EnumSingle {
    INSTANCE;
    public static EnumSingle getInstance(){
        return INSTANCE;
    }
}
class Test{
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        EnumSingle instance = EnumSingle.INSTANCE;
        Constructor<EnumSingle> constructor = EnumSingle.class.getDeclaredConstructor(String.class, int.class);
        constructor.setAccessible(true);
        EnumSingle instance2 = constructor.newInstance();
        System.out.println(instance);
        System.out.println(instance2);
    }
}


单例模式

标签:hold   print   oca   思想   declared   msi   ssi   饿汉式   win   

原文地址:https://www.cnblogs.com/chaostudy/p/13052185.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!