java设计模式-工厂设计模式
public class Test{
public static void main(String args[]){
Fruit f = Factory.getInstance(“apple”);
f.eat();
}
}
class Factory{
public static Fruit getInstance(String name){
if(name.equals(“apple”)){
return new Apple();
}
if(name.equals(“orange”)){
return new Orange();
}
return null;
}
}
interface Fruit{
public void eat();
}
class Apple implements Fruit{
public void eat(){
System.out.println(“eat apple”);
}
}
class Orange implements Fruit{
public void eat(){
System.out.println(“eat orange”);
}
}