@Test public void test(){ Food food = FoodFactory.getFood("Fish"); food.introduce(); }
public static Food getFood(String type) { Food food = null; try { food = (Food) Class.forName("com.hks.factory." + type).newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return food; }
FoodFactory { public static Food getFood(String type) { Food food = null; try { food = (Food) Class.forName("com.hks.factory." + type).newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return food; } }
FoodFactory { public static Food getFood(String type) { Food food = null; try { food = (Food) Class.forName("com.hks.factory." + type).newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return food; }  }
FoodFactory { public static Food getFood(String type) { Food food = null; try { food = (Food) Class.forName("com.hks.factory." + type).newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return food; }  static Food getFood(String type); }
FoodFactory { public static Food getFood(String type) { Food food = null; try { food = (Food) Class.forName("com.hks.factory." + type).newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return food; }  static Food getFood(String type);  }