java设计模式进阶_singleton

这里写图片描述

//示例:
//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : IvoryTower.java
//  @ Date : 2016/8/19
//  @ Author : wuchao
//
//

/*
 * 单例类:
 * 饿汉初始化-保证线程安全
 */
public class IvoryTower {
    private static IvoryTower instance = new IvoryTower();

    //私有构造器
    private IvoryTower() {}

    public static IvoryTower getInstance() {
        return instance;
    }
}

/*
 * 枚举单例类.
 * Effective Java第二版 (Joshua Bloch) p. 18
 */
public enum EnumIvoryTower {

    INSTANCE;

    public String toString() {

        return getDeclaringClass().getCanonicalName() + "@" + hashCode();
    };

}

import java.io.Serializable;
/*
 * 这个初始化在需求持有者成语 是一个安全的创建,
 * 懒汉初始化单例对象
 * 参考 "The CERT Oracle Secure Coding Standard for Java"
 */
public class InitializingOnDemandHolderIdiom implements Serializable {

    private static final long serialVersionUID = -1798698522396538556L;

    private InitializingOnDemandHolderIdiom(){}

    private static class HelperHolder{
        public static final InitializingOnDemandHolderIdiom INSTANCE = new InitializingOnDemandHolderIdiom();
    }

    public static InitializingOnDemandHolderIdiom getInstance(){
        return HelperHolder.INSTANCE;
    }

    protected Object readResolve(){
        return getInstance();
    }

}

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : ThreadSafeLazyLoadedIvoryTower.java
//  @ Date : 2016/8/19
//  @ Author : wuchao
//
//


/*
 * 线程安全的单例类
 */
public class ThreadSafeLazyLoadedIvoryTower {
    private static ThreadSafeLazyLoadedIvoryTower instance = null;

    private ThreadSafeLazyLoadedIvoryTower() {  }

    public synchronized static ThreadSafeLazyLoadedIvoryTower getInstance() {
        /*
         * 获取实例只会在第一次调用时创建
         * 懒加载式 
         */
        if (instance == null) {
            instance = new ThreadSafeLazyLoadedIvoryTower();
        }

        return instance;
    }

}

/*
 * 双检查
 */
public class ThreadSafeDoubleCheckLocking {

    private static volatile ThreadSafeDoubleCheckLocking INSTANCE;

    private ThreadSafeDoubleCheckLocking(){
        //阻止使用反射工具调用
        if(INSTANCE != null)
            throw new IllegalStateException("Already Initialized.");
    }

    public static ThreadSafeDoubleCheckLocking getInstance()
    {
        /*
         * 本地变量增加性能%25 参考Joshua Bloch "Effective Java, Second Edition"
         */
        ThreadSafeDoubleCheckLocking result = INSTANCE;
        if(result == null)
        {
            synchronized(ThreadSafeDoubleCheckLocking.class)
            {
                result = INSTANCE;
                if(result == null){
                    INSTANCE = result = new ThreadSafeDoubleCheckLocking();
                }
            }
        }

        return result;
    }

}


public class App {

    public static void main(String[] args) {
        //饿汉式
        IvoryTower ivoryTower1 = IvoryTower.getInstance();
        IvoryTower ivoryTower2 = IvoryTower.getInstance();
        System.out.println("ivoryTower1=" + ivoryTower1);
        System.out.println("ivoryTower2=" + ivoryTower2);

        //懒汉式
        ThreadSafeLazyLoadedIvoryTower threadSafeIvoryTower1 = ThreadSafeLazyLoadedIvoryTower
                .getInstance();
        ThreadSafeLazyLoadedIvoryTower threadSafeIvoryTower2 = ThreadSafeLazyLoadedIvoryTower
                .getInstance();
        System.out.println("threadSafeIvoryTower1=" + threadSafeIvoryTower1);
        System.out.println("threadSafeIvoryTower2=" + threadSafeIvoryTower2);

        //枚举
        EnumIvoryTower enumIvoryTower1 = EnumIvoryTower.INSTANCE;
        EnumIvoryTower enumIvoryTower2 = EnumIvoryTower.INSTANCE;
        System.out.println("enumIvoryTower1=" + enumIvoryTower1);
        System.out.println("enumIvoryTower2=" + enumIvoryTower2);

        InitializingOnDemandHolderIdiom demandHolderIdiom = InitializingOnDemandHolderIdiom.getInstance();
        System.out.println(demandHolderIdiom);
        InitializingOnDemandHolderIdiom demandHolderIdiom2 = InitializingOnDemandHolderIdiom.getInstance();
        System.out.println(demandHolderIdiom2);

        ThreadSafeDoubleCheckLocking dcl1 = ThreadSafeDoubleCheckLocking.getInstance();
        System.out.println(dcl1);
        ThreadSafeDoubleCheckLocking dcl2 = ThreadSafeDoubleCheckLocking.getInstance();
        System.out.println(dcl2);

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值