首页 > 编程语言 > 详细

Java之工厂模式

时间:2014-08-04 13:28:37      阅读:325      评论:0      收藏:0      [点我收藏+]

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();
    }
}

Java之工厂模式,布布扣,bubuko.com

Java之工厂模式

原文:http://www.cnblogs.com/vonk/p/3889562.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!