//示例:
//
//
// 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);
}
}
java设计模式进阶_singleton
最新推荐文章于 2024-04-03 15:20:17 发布