工厂模式例子
interface Fruit{ public void eat(); } class Apple implements Fruit{ @Override public void eat() { System.out.println("吃苹果"); } } class Cherry implements Fruit{ @Override public void eat() { System.out.println("吃樱桃"); } } class Factory{ //工厂 public static Fruit getInstance(String className){ if ("apple".equals(className)){ return new Apple(); }else if ("cherry".equals(className)){ return new Cherry(); }else{ return null; } } } public class FactoryDemo { public static void main(String[] args){ Fruit f = Factory.getInstance(args[0]); f.eat(); } }