您的位置:首页 > 其它

工厂设计模式

2016-07-09 20:10 204 查看
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){
Fruit f = null;
if("apple".equals(classname)){
f = new Apple();
}
if("orange".equals(classname)){
f = new Orange();
}
return f;
}

}

public class Test {

public static void main(String[] args) {

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

}

}

运行结果:

吃苹果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  工厂设计模式