簡單工廠模式屬於建立型模式,又叫作靜態工廠方法(Static Factory Method)。簡單工廠模式是由一個工廠對象決定建立哪種產品類實例。在簡單工廠模式中,能夠根據參數的不一樣返回不一樣類的實例。簡單工廠模式專門定義一個類來負責建立其餘類的實例,被建立的實例一般都具備共同的父類。簡單工廠模式是工廠模式家族中最簡單實用的模式,能夠理解爲不一樣工廠模式的一個特殊實現java
值得注意的是,簡單工廠模式並不屬於GOF設計模式之一。可是他說抽象工廠模式,工廠方法模式的基礎,而且有普遍得應用設計模式
從上圖中能夠看出,簡單工廠模式由三部分組成:具體工廠、抽象產品和具體產品:app
public class Apple { /** * 採集蘋果 */ public void get(){ System.out.println("採集蘋果"); } }
public class Banana { /** * 採集香蕉 */ public void get(){ System.out.println("採集香蕉"); } }
public class Mainclass{ public static void main(String[] args){ /** * 最基本的實例化方式 */ //實例化一個Apple Apple apple = new Apple(); //實例化一個Banana Banana banana = new Banana(); apple.get(); banana.get(); } }
public interface Fruit { public void get(); }
public class Apple implements Fruit{ @Override //重寫get方法 public void get(){ System.out.println("採集蘋果"); } }
public class Banana implements Fruit{ @Override //重寫get方法 public void get(){ System.out.println("採集香蕉"); } }
public class Mainclass{ public static void main(String[] args){ /** * 多態的實例化方式 */ Fruit apple = FruitFactory.getApple(); Fruit banana = FruitFactory.getBanana(); apple.get(); banana.get(); } }
public class FruitFactory { /** * 得到Apple類的實例 */ public static Fruit getApple(){ return new Apple(); } /** * 得到Banana類的實例 */ public static Fruit getBanana(){ return new Banana(); } }
public interface Fruit { public void get(); }
public class Apple implements Fruit{ @Override //重寫get方法 public void get(){ System.out.println("採集蘋果"); } }
public class Banana implements Fruit{ @Override //重寫get方法 public void get(){ System.out.println("採集香蕉"); } }
public class Mainclass{ public static void main(String[] args){ /** * 經過工廠類的靜態方法實例化 */ Fruit apple = FruitFactory.getApple(); Fruit banana = FruitFactory.getBanana(); apple.get(); banana.get(); } }
public class FruitFactory { public static Fruit getFruit(String type) throws IllegalAccessException, ClassNotFoundException, InstantiationException { /** * 對參數的判斷並返回實例 */ if (type.equalsIgnoreCase("apple")){ return Apple.class.newInstance(); } else if (type.equalsIgnoreCase("banana")){ return Banana.class.newInstance(); } else { System.out.println("找不到相應的實例化類"); return null; } } }
public interface Fruit { public void get(); }
public class Apple implements Fruit{ @Override public void get(){ System.out.println("採集蘋果"); } }
public class Banana implements Fruit{ @Override public void get(){ System.out.println("採集香蕉"); } }
public class Mainclass{ public static void main(String[] args) throws IllegalAccessException, ClassNotFoundException, InstantiationException { Fruit apple = FruitFactory.getFruit("apple"); Fruit banana = FruitFactory.getFruit("banana"); apple.get(); banana.get(); } }
public class FruitFactory { public static Fruit getFruit(String type) throws IllegalAccessException, ClassNotFoundException, InstantiationException { Class fruit = Class.forName(type); return (Fruit)fruit.newInstance(); } }
public interface Fruit { public void get(); }
public class Apple implements Fruit{ @Override public void get(){ System.out.println("採集蘋果"); } }
public class Banana implements Fruit{ @Override public void get(){ System.out.println("採集香蕉"); } }
public class Mainclass{ public static void main(String[] args) throws IllegalAccessException, ClassNotFoundException, InstantiationException { Fruit apple = FruitFactory.getFruit("Apple"); Fruit banana = FruitFactory.getFruit("Banana"); apple.get(); banana.get(); } }