interface Drink{public void drink();} //定义一个接口 喝 class Milk implements Drink{ //定义一个牛奶类 实现接口喝 重写方法 public void drink(){System.out.println("喝牛奶");} } class Beer implements Drink{ //定义一个啤酒类 实现接口喝 重写方法 public void drink(){System.out.println("喝啤酒");} } //... class DrinkFactory{ static Drink getInstance(String str){ //多态 if(str.equals("milk")){ return new Milk(); }else if(str.equals("beer")){ return new Beer(); } return null; } } public class FactoryDesign { public static void main(String[] args) { Milk milk=(Milk)DrinkFactory.getInstance("milk"); milk.drink(); Beer beer=(Beer)DrinkFactory.getInstance("beer"); beer.drink(); } }