工厂设计模式
来源:互联网 发布:形容不出门知天下事 编辑:程序博客网 时间:2024/06/05 08:13
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();
}
}
运行结果:
吃苹果
0 0
- 设计模式--工厂设计
- 设计模式------工厂模式
- 设计模式------工厂模式
- 设计模式 [ 工厂模式 ]
- 设计模式--工厂模式
- 设计模式-工厂模式
- 设计模式--工厂模式
- 设计模式 工厂模式
- 设计模式-工厂模式
- 设计模式 -- 工厂模式
- 设计模式-工厂模式
- 设计模式----工厂模式
- 设计模式---工厂模式
- 设计模式-工厂模式
- 设计模式--工厂模式
- 设计模式---工厂模式
- 设计模式-工厂模式
- 设计模式--工厂模式
- 华为机试---数字排序
- andriod 自动更新开发
- mongodb 在线添加新的分片复制集 后续
- json配置
- C起步感想
- 工厂设计模式
- 第140课: Spark面试经典系列之数据倾斜解决之并行度的深度使用
- 五分钟学会之ListView<simpleAdapter><BaseAdapter>
- <机器学习>自用笔记
- java枚举
- log4j配置
- 根据后序与中序遍历建树层序遍历输出
- android 内存进程管理分析
- Java服务器热部署的实现原理