package designmode;

interface Fruit {
public void eat();
}

class Apple implements Fruit {
public void eat() {
System.out.println("这个苹果真好吃!");
}
}

class Banana implements Fruit {
public void eat() {
System.out.println("开始吃香蕉!");
}
}

class Factory { // 工厂设计模式
public static Fruit getInstance(String str) {// 一般写static表示不需要写属性
if (str.equals("apple")) {
return new Apple();
} else if (str.equals("banana")) {
return new Banana();
} else {
return null;
}
}
}

public class FactoryDemo {
public static void main(String[] args) {
Fruit f = Factory.getInstance("apple");
Fruit f1 = Factory.getInstance("banana");
f.eat();// 这个苹果真好吃!
f1.eat();// 开始吃香蕉!
}
}