1.接口
 
package com.fzw.ms;

public interface Food {
  public void eat();
}
 
2.实现类一:
 
package com.fzw.ms;

public class Cake implements Food {

  public void eat() {
    System.out.println("------Cake.eat()----------");

  }

}
 
3.实现类二:
 
package com.fzw.ms;

public class Pissa implements Food {

  public void eat() {
    System.out.println("------Pissa.eat()----------");

  }

}
 
4.建立工厂类:
 
package com.fzw.ms;

public class FoodFactory {
  private FoodFactory() {
  };

  private static final FoodFactory ff = new FoodFactory();

  public static FoodFactory getInstance() {
    return ff;
  }

  public Food makefood(int i) {
    if (i == 1) {
      Food cake = new Cake();
      return cake;
    }
    if (i == 2) {
      Food pissa = new Pissa();
      return pissa;
    }
    return null;

  }

  public static void main(String[] args) {
    FoodFactory.getInstance().makefood(1).eat();
  }
}