Givemepass's Android 惡補筆記

Simple Factory Pattern

| Comments

簡單工廠模式(Simple Factory Pattern)是一種把演算法包起來的模式,

public class AFactory {
    public A creator(int type){
            A a = null;
            switch(type){
            case 1:
                a = new A1();
              break;
          case 2:
                a = new A2();
              break;
        }
        return a;
    }
}

就像這樣把creator封裝起來,
好處是只要傳入對應的參數, 就可以得到對應的物件,
壞處就是這樣未來要新增刪除修改類型, 並不好維護。

Comments

comments powered by Disqus