2019.10.19【2】
接口中的工厂模式
public class Factory {
public static void main(String arg[]){
Fruit f=Factory.getInStance("apple");
f.eat();
}
public static Fruit getInStance(String className){
if ("apple"==className){
return new Apple();
}else if("orange"==className){
return new Orange();
}
return null;
}
}
interface Fruit {
public void eat();
}
public class Apple implements Fruit {
public void eat() {
System.out.print("吃苹果");
}
}
public class Orange implements Fruit {
public void eat() {
System.out.print("吃橘子");
}
}