工厂设计模式
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();
}
}
运行结果:
吃苹果
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();
}
}
运行结果:
吃苹果
相关文章推荐
- PHP设计模式 工厂设计模式
- 接口工具类
- Java设计模式之工厂设计模式
- 随堂笔记160315工厂和代理模式
- JAVA常用设计模式
- 设计模式的概述与工厂设计模式
- Java设计模式—工厂设计模式
- 工厂设计模式
- 6、业务隔离(工厂设计模式)
- Java基础回顾 : 反射机制相关知识
- 《黑马程序员》 工厂设计模式深入分析
- [零基础学JAVA]Java SE应用部分-36.反射机制与工厂设计模式 推荐
- 工厂设计模式的探讨——iOS类簇的应用分析
- 工厂设计模式 - 数据存储的特有方式
- 最简单的Java工厂设计模式样例
- 设计模式之5 多方法的工厂模式
- 工厂设计模式
- 工厂设计模式
- 工厂设计模式
- Java接口 详解(二)