java工程模式意思_[Java教程]简单工程模式

[Java教程]简单工程模式

0 2016-03-20 21:00:13

简单工厂模式

定义:专门定义一个类来负责创建其它类的实例,被创建的实例通常都具有共同的父类或接口;

意图:提供一个类,由它负责根据一定的条件创建某一具体类的实例;1 public class FactoryDemo{ 2 public static void main(String []args){ 3 IFruit fruit = Factory.getFruit("橘子"); 4 if(fruit!=null){ 5 System.out.println(fruit.get()); 6 } 7 else{ 8 System.out.println("你要的东西不存在"); 9 }10 }11 }12 13 interface IFruit{14 public String get();15 }16 17 class Factory{18 public static IFruit getFruit(String name){19 if(name.equals("苹果"))20 {21 return new Apple();22 }23 else if(name.equals("橘子"))24 {25 return new Orange();26 }27 else28 {29 return null;30 }31 }32 }33 34 class Apple implements IFruit{35 public String get(){36 return "采摘苹果";37 }38 }39 40 class Orange implements IFruit{41 public String get(){42 return "采摘橘子";43 }44 }

本文网址:http://www.shaoqun.com/a/203040.html

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:admin@shaoqun.com。

0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值