Java之工厂模式

interface Fruit {
    void eat();
}

class Apple implements Fruit {

    public void eat() {
        System.out.println("I am eating apple.");
    }

}

class Orange implements Fruit {

    public void eat() {
        System.out.println("I am  eating orange.");
    }

}

class Factory {
    public Fruit getInstance(String className) {
        Fruit f = null;
        if ("apple".equals(className)) {
            f = new Apple();
        } else {
            f = new Orange();
        }
        return f;
    }
}

public class InterfaceDemo04 {
    public static void main(String[] args) {
        Factory factory = new Factory();
        Fruit f = null;
        f = factory.getInstance("apple");
        f.eat();
    }
}

上一篇:解决AD9中“......has no driver”的问题


下一篇:'PostBuildEvent' failed with error code '1' 'Unspecified error'( PostBuildEvent”失败,错误代码为“1”。“未指定的错误” )