Java-单例模式的几种写法

一,饿汉式

优点:线程安全,实现简单。

缺点:类加载时就初始化了,没有实现懒加载。

public final class HungrySingleTon {

    private static final HungrySingleTon instance = new HungrySingleTon();

    private HungrySingleTon() {
        if (instance != null) {
            throw new IllegalStateException("实例已经初始化!");
        }
    }

    public HungrySingleTon getInstance() {
        return instance;
    }
}

二,饿汉式(线程不安全的形式)

优点:懒加载。

缺点:线程不安全。

public final class SingleTonSecond {

    private static SingleTonSecond singleTonSecond;

    private SingleTonSecond() {
        if (singleTonSecond!= null) {
            throw new IllegalStateException("实例已经初始化!");
        }
    }

    public static SingleTonSecond getInstance() {
        if (null == singleTonSecond) {
            singleTonSecond = new SingleTonSecond();
        }
        return singleTonSecond;
    }
}

三、懒汉式(线程安全)

  优点:懒加载、线程安全。

   缺点:每次获取单例都要锁定整个方法,性能较差

public final class LazySingleTonSafe {

    private static volatile LazySingleTonSafe instance;

    private LazySingleTonSafe() {
        if (instance != null) {
            throw new IllegalStateException("实例已经初始化!");
        }
    }

    public synchronized static LazySingleTonSafe getInstance() {
        if (null == instance) {
            instance = new LazySingleTonSafe();
        }
        return instance;
    }
}

四、双重检查懒汉式单例  

  优点:线程安全,只在第一次获取单例时锁定了单例对象。

public final class DoubleCheckedLockSingleTon {

    private static volatile DoubleCheckedLockSingleTon instance;

    private DoubleCheckedLockSingleTon() {
        if (instance != null) {
            throw new IllegalStateException("实例已经初始化!");
        }
    }

    public static DoubleCheckedLockSingleTon getInstance() {

        if (null == instance) {
            synchronized (DoubleCheckedLockSingleTon.class) {
                if (null == instance) {
                    instance = new DoubleCheckedLockSingleTon();
                }
            }
        }
        return instance;
    }
}

五、静态内部类方式-推荐!

    优点:线程安全,懒加载,效率高静态内部类的凡是在单例类在被JVM加载到内存时并不会立即实例化,而是在第一次调用的getInstance()方法时才会将SingleTonFifthInner类加载到内存,从而实例化SingleTonFifthInner的静态属性。JVM在类加载阶段是同步加锁的,因此JVM替我们保证了在同一时刻只会有一个内部类被加载。 

public final class StaticInnerClassSingleTon {

    private StaticInnerClassSingleTon() {

    }

    private StaticInnerClassSingleTon getInstance() {
        return StaticInnerClassSingleTonHolder.instance;
    }

    private static class StaticInnerClassSingleTonHolder {

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

}
六,枚举类型实现单例模式

 

 
public class SingleTonSixth {

    private SingleTonSixth(){

    }

    public static SingleTonSixth getInstance(){
        return SingleTonEnum.ISNTANCE.getInstancee();
    }

    private static enum SingleTonEnum{
        ISNTANCE;

        private  SingleTonSixth singleTonSixth;

        private  SingleTonEnum(){
            singleTonSixth=new SingleTonSixth();
        }

        private  SingleTonSixth getInstancee(){
            return singleTonSixth;
        }

    }
}

你可能感兴趣的:(java,java基础,设计模式)