/*
 * 工厂模式
 */
package com.classes;
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("吃桔子");
    }
}
public class factory {
    public static fruit getInstance(String className){
        fruit f=null;
        if("apple".equals(className)){
//"apple"在前className在后,这样可以避免空指向异常,因为传入的className有可能为null
            f=new apple();
        }
        if("orange".equals(className)){
            f=new orange();
        }
        return f;
    }
    public static void main(String args[]){
        fruit f=factory.getInstance("apple");
        f.eat();
    }
}