1.單例模式
顧名思義,使一個類最多僅有一個執行個體化的對象的模式。單例模式根據對象執行個體化的早晚,分為懶漢式和餓漢式兩種。
懶漢式,不調用它,就不執行個體化對象,懶。。。
/** * 懶漢式 * */class Singleton{private static Singleton mInstance;private Singleton(){}public static Singleton getInstance(){if(null==mInstance){mInstance=new Singleton();}return mInstance;}}
餓漢式,因為饑餓嘛,在定義對象的時候就迫不及待的執行個體化了。
/** * 餓漢式 * */class HungrySingleton{private static HungrySingleton mInstance=new HungrySingleton();private HungrySingleton(){}public static HungrySingleton getInstance(){return mInstance;}}
在多線程的情況下,懶漢式需要通過雙重鎖定來防止重複執行個體化。
第一個null==mInstance比較好理解,當未執行個體化時才加同步判斷,並執行個體化。
第二個null==mInstance,是為了防止多線程同時調用時,都執行到同步鎖外等待,結果一個線程已經執行個體化了,其他線程又進行執行個體化。
/** * 多線程情況下雙重鎖定的懶漢式 * */class SingletonMultiThread{private static SingletonMultiThread mInstance;private static byte[] mLock=new byte[0];private SingletonMultiThread(){}public static SingletonMultiThread getInstance(){if(null==mInstance){synchronized(mLock){if(null==mInstance){mInstance=new SingletonMultiThread();}}}return mInstance;}}
記得以前看過一篇Oracle的人寫的文章,分析了說雙重鎖定機制其實也不靠譜。。。
所以。。。還是直接用餓漢式吧,就沒有這些煩惱了。。。
2.橋接模式
橋接模式的名字貌似是因為UML圖看起來像一座橋。。。
其實橋接模式是指:當系統的實現有多種角度的分類時,盡量的將不同的分類分離出來,是它們獨立的變化,降低耦合。
橋接模式充分利用了組合彙總複用原則,即:儘可能的多使用組合彙總,而非繼承,只有在具有較強的"is a"的關係時,才考慮繼承。
一個較差的,多用繼承的設計。
abstract class CellPhoneBrand{public abstract void run();}class CellPhoneBrandH extends CellPhoneBrand{public void run(){System.out.println("HTC");}}class CellPhoneBrandS extends CellPhoneBrand{public void run(){System.out.println("SAMSUNG");}}class CellPhoneBrandHGame extends CellPhoneBrandH{public void run(){super.run();System.out.println("Play game");}}class CellPhoneBrandSGame extends CellPhoneBrandS{public void run(){super.run();System.out.println("Play game");}}class CellPhoneBrandHMusic extends CellPhoneBrandH{public void run(){super.run();System.out.println("Play music");}}class CellPhoneBrandSMusic extends CellPhoneBrandS{public void run(){super.run();System.out.println("Play music");}}public class BridgePattern {public static void main(String[] args) {// TODO Auto-generated method stubCellPhoneBrand c;c=new CellPhoneBrandHGame();c.run();c=new CellPhoneBrandSGame();c.run();c=new CellPhoneBrandHMusic();c.run();c=new CellPhoneBrandSMusic();c.run();}}
使用了橋接模式改進
abstract class CellPhoneSoftware{public abstract void run(); }class CellPhoneGame extends CellPhoneSoftware{public void run(){System.out.println("Play game");}}class CellPhoneMusic extends CellPhoneSoftware{public void run(){System.out.println("Play music");}}abstract class CellPhoneBrand{protected CellPhoneSoftware mCellPhoneSoftware;public void setSoftware(CellPhoneSoftware cellPhoneSoftware){mCellPhoneSoftware=cellPhoneSoftware;}public abstract void run();}class HTC extends CellPhoneBrand{public void run(){System.out.println("HTC");if(null!=mCellPhoneSoftware)mCellPhoneSoftware.run();}}class SAMSUNG extends CellPhoneBrand{public void run(){System.out.println("SAMSUNG");if(null!=mCellPhoneSoftware)mCellPhoneSoftware.run();}}public class BridgePatternBetter {public static void main(String[] args) {// TODO Auto-generated method stubCellPhoneBrand c;c=new HTC();c.setSoftware(new CellPhoneMusic());c.run();c=new SAMSUNG();c.setSoftware(new CellPhoneGame());c.run();}}