1. 程式人生 > >設計模式 - 單件模式(singleton pattern) 具體解釋

設計模式 - 單件模式(singleton pattern) 具體解釋

單件模式(singleton pattern) 具體解釋


本文地址: http://blog.csdn.net/caroline_wendy/article/details/28595349


單件模式(singleton pattern) : 確保一個類僅僅有一個例項, 並提供一個全域性訪問點.

單位價格模式包含3個部分: 私有構造器, 靜態變數, 靜態方法.


具體方法:

1. 標準的單例模式:

/**
 * @time 2014.6.5
 */
package singleton;

/**
 * @author C.L.Wang
 *
 */
public class Singleton {
	private static Singleton uniqueInstance; //靜態變數
	
	private Singleton() {} //私有建構函式
	
	public static Singleton getInstance() { //靜態方法
		if (uniqueInstance == null)
			uniqueInstance = new Singleton();
		return uniqueInstance;
	}

}

2. 考慮多執行緒的三種方法:

同步(synchronized)方法, 加入"synchronized",  會導致效能下降, 每次呼叫演示樣例, 都須要同步, 可是使用簡單.

/**
 * @time 2014.6.5
 */
package singleton;

/**
 * @author C.L.Wang
 *
 */
public class Singleton {
	private static Singleton uniqueInstance; //靜態變數
	
	private Singleton() {} //私有建構函式
	
	public static synchronized Singleton getInstance() { //靜態方法
		if (uniqueInstance == null)
			uniqueInstance = new Singleton();
		return uniqueInstance;
	}

}


急切(eagerly)方法, 開始時建立例項, 會在不須要時, 佔用例項空間, 即佔用空間時間過長.

/**
 * @time 2014.6.5
 */
package singleton;

/**
 * @author C.L.Wang
 *
 */
public class Singleton {
	private static Singleton uniqueInstance = new Singleton(); //靜態變數
	
	private Singleton() {} //私有建構函式
	
	public static synchronized Singleton getInstance() { //靜態方法
		//if (uniqueInstance == null)
			//uniqueInstance = new Singleton();
		return uniqueInstance;
	}

}

雙重檢查加鎖(double-checked locking)方法, 使用"volatile"和"synchronized (Singleton.class)", 降低時間消耗, 適用於java1.4以上版本號.

/**
 * @time 2014.6.5
 */
package singleton;

/**
 * @author C.L.Wang
 *
 */
public class Singleton {
	private volatile static Singleton uniqueInstance; //靜態變數
	
	private Singleton() {} //私有建構函式
	
	public static synchronized Singleton getInstance() { //靜態方法
		if (uniqueInstance == null) {
			synchronized (Singleton.class) {
				if (uniqueInstance == null)
					uniqueInstance = new Singleton();
			}
		}
		return uniqueInstance;
	}

}

3. 使用單件模式的樣例:

程式碼:

/**
 * @time 2014.6.5
 */
package singleton;

/**
 * @author C.L.Wang
 *
 */
public class ChocolateBoiler { //巧克力鍋爐
	private boolean empty;
	private boolean boiled;
	
	public static ChocolateBoiler uniqueInstance; //靜態變數
	
	private ChocolateBoiler() { //私有建構函式
		empty = true;
		boiled = false;
	}
	
	public static ChocolateBoiler getInstance() { //靜態方法
		if (uniqueInstance == null) 
			uniqueInstance = new ChocolateBoiler();
		return uniqueInstance;
	}
	
	public void fill() { //填滿
		if (isEmpty()) {
			empty = false;
			boiled = false;
		}
	}
	
	public void drain() { //傾倒
		if (!isEmpty() && isBoiled())
			empty = true;
	}
	
	public void boil() { //煮
		if (!isEmpty() && !isBoiled()) {
			boiled = true;
		}
	}
	
	public boolean isEmpty() {
		return empty;
	}
	
	public boolean isBoiled() {
		return boiled;
	}

}

4. 列舉單件(enum singleton)模式, 也能夠保證執行緒安全.

程式碼:

/**
 * @time 2014.6.5
 */
package singleton;

/**
 * @author C.L.Wang
 *
 */
public class EnumSingleton {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		eSingleton d1 = eSingleton.INSTANCE;
		d1.setName("Spike");
		
		eSingleton d2 = eSingleton.INSTANCE;
		d2.setName("Caroline");
		
		System.out.println(d1);
		System.out.println(d2);
		
		System.out.println(d1 == d2);
	}

}

enum eSingleton {
	
	INSTANCE;
	
	private String name;
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public String toString() {
		return "[" + name + "]";
	}
}

輸出:

[Caroline]
[Caroline]
true