簡單工廠模式(Simple Factory Pattern),又稱爲靜態工廠方法(static Factory Method),它屬於建立型模式。
在簡單工廠模式中,能夠根據參數的不一樣返回不一樣類的實例。簡單工廠模式專門定義一個類來負責建立其餘類的實例,被建立的實例一般都具備共同的父類。
好比有一個電腦商,目前生產聯想電腦,隨着業務擴展,他如今須要生產惠普的電腦,咱們就須要一個單獨的類來專門生產電腦,這就用到了簡單工廠模式。
下面以代碼爲例:java
接口:git
package designpattern.factory.simplefactory.noodles; public interface INoodles { /** * 描述每種麪條啥樣的 */ public abstract void desc(); }
package designpattern.factory.simplefactory.noodles; public class LzNoodles implements INoodles { @Override public void desc() { System.out.println("蘭州拉麪,蘭州特點小吃"); } }
package designpattern.factory.simplefactory.noodles; public class PaoNoodles implements INoodles { @Override public void desc() { System.out.println("泡麪,程序員必備"); } }
package designpattern.factory.simplefactory.noodles; public class ReganNoodles implements INoodles { @Override public void desc() { System.out.println("紅油熱乾麪,武漢特點小吃"); } }
package designpattern.factory.simplefactory; import designpattern.factory.simplefactory.noodles.INoodles; import designpattern.factory.simplefactory.noodles.LzNoodles; import designpattern.factory.simplefactory.noodles.PaoNoodles; import designpattern.factory.simplefactory.noodles.ReganNoodles; /** * 簡單工廠模式 * 一家「簡單面館」(簡單工廠類) */ public class SimpleNoodlesFactory { public static final int TYPE_LZ = 1;//蘭州拉麪 public static final int TYPE_PM = 2;//泡麪 public static final int TYPE_GK = 3;//熱乾麪 /** * 若是生產對象的方法是static的,這種簡單工廠也叫作靜態工廠 * 若是生產對象的方法不是static的,這種簡單工廠也叫作實例工廠 * @param type * @return */ public static INoodles createNoodles(int type) { switch (type) { case 1: return new LzNoodles(); case 2: return new PaoNoodles(); case 3: return new ReganNoodles(); default: return new LzNoodles(); } } }
package designpattern.factory.simplefactory; import designpattern.factory.simplefactory.noodles.INoodles; import designpattern.factory.simplefactory.noodles.LzNoodles; public class Test { public static void main(String[] args) { /** * 一開始,窮,想吃麪必須得本身作 * 想吃拉麪得本身作,new LzNoodles() * 想吃泡麪得本身作,new PaoNoodles() * 想吃熱乾麪得本身作,new ReganNoodles() */ // 作拉麪吃 INoodles lzNoodles = new LzNoodles(); // 作泡麪吃 // INoodles paoNoodles = new PaoNoodles(); // 作熱乾麪吃 // INoodles reganNoodles = new ReganNoodles(); // 然而,new來new去,改來改去,好心煩...... /** * 突然,有一天走了狗屎運成了暴發戶 * 幸福生活今後來臨,吃麪今後變得簡單 * 給麪館說一聲想吃啥,麪館作好了給本身就行了 * 本身沒必要關心麪條怎麼作(怎麼new,如何new)讓麪館操心去吧(麪館幫咱們new)! * 好省心,有錢就是好,太爽了! */ INoodles iNoodles = SimpleNoodlesFactory.createNoodles(2); // 和具體的對象脫離關係 iNoodles.desc(); /**考慮使用工廠模式 * 1) 當客戶程序不須要知道要使用對象的建立過程。 * 2) 客戶程序使用的對象存在變更的可能,或者根本就不知道使用哪個具體的對象。 */ } }
工廠方法模式是比較複雜一點的工廠模式的體現,主要是經過建立工廠類來進行對對象的構建。程序員
package designpattern.factory.factorymethod; import designpattern.factory.simplefactory.noodles.INoodles; /** * 工廠方法模式 */ public interface INoodlesFactory { INoodles createNoodles(); }
package designpattern.factory.factorymethod; import designpattern.factory.simplefactory.noodles.INoodles; import designpattern.factory.simplefactory.noodles.LzNoodles; public class LzINoodlesFactory implements INoodlesFactory { @Override public INoodles createNoodles() { return new LzNoodles(); } }
package designpattern.factory.factorymethod; import designpattern.factory.simplefactory.noodles.INoodles; import designpattern.factory.simplefactory.noodles.PaoNoodles; public class PaoINoodlesFactory implements INoodlesFactory { @Override public INoodles createNoodles() { return new PaoNoodles(); } }
package designpattern.factory.factorymethod; import designpattern.factory.simplefactory.noodles.INoodles; import designpattern.factory.simplefactory.noodles.ReganNoodles; public class ReganINoodlesFactory implements INoodlesFactory { @Override public INoodles createNoodles() { return new ReganNoodles(); } }
package designpattern.factory.factorymethod; import designpattern.factory.simplefactory.noodles.INoodles; public class Test { public static void main(String[] args) { INoodlesFactory noodlesFactory = new LzINoodlesFactory(); INoodles noodles = noodlesFactory.createNoodles(); noodles.desc(); } }
代碼實例:
設計模式demo設計模式