首页 > 代码库 > java简单工厂模式

java简单工厂模式

 interface Fruit(){

  public void eat();

  }

class Apple implements Fruit(){

  public void eat(){

System.out.println("吃苹果");

}

class Orange implements Fruit(){

  public void eat(){

System.out.println("吃橘子");

}

class Factory(){

public static Fruit getInstance(String className){

if("apple".equals(calssName)){

return new Apple

}else{return new Orange}}

public class TestDemo{

public static void main(String args[]){

Fruit f=Factory.getInstance("apple");

f.eat();

}}
}

 

java简单工厂模式