请问高手你们谁看过闫宏的java与模式

king_2007 2014-06-17 08:34:35
我想请教个问题<java与模式>中,有个章节是简单的工厂模式,书中代码中有编写一个水果接口,还有实现这个接口的三个类apple grape 和strawberry,还有fruitGardener类和badfruitException类,可是我照着他的代码编译运行后没有什么效果,是不是他的代码写错了?我是想问下是不是fruitGardener类中的public static fruit factory(String which)写错了,应该是无返回类型的吧?public static void factory(String which)?
...全文
203 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
逍遥jc 2014-06-20
  • 打赏
  • 举报
回复
应该没什么问题。那个p方法只是输出对应的语句而已。肯定不会是返回void类型的呀。不然还会是简单工厂模式么? 客户端是需要得到具体产品对象的,所以返回的肯定是fruit类型。
S117 2014-06-18
  • 打赏
  • 举报
回复
你工厂方法还不要返回类型?什么也不生产的工厂有用吗? 你贴下你代码是怎么写的!
xlight2023 2014-06-18
  • 打赏
  • 举报
回复
可以把代码贴出来,要达到什么效果
JPF1024 2014-06-18
  • 打赏
  • 举报
回复
http://www.blogjava.net/jzgl-ever/archive/2008/07/26/217704.html 看下这个
king_2007 2014-06-18
  • 打赏
  • 举报
回复
引用 1 楼 dcxy0 的回复:
http://www.blogjava.net/jzgl-ever/archive/2008/07/26/217704.html 看下这个
你的代码我看过了,不管怎么说,我觉得受益匪浅尤其是那最后的代码,对我有用,谢谢啊。那些代码是你写的,还是转帖??最后的代码应该这样是不是更专业些:try{ fruit f=new fruitgardener().factory("apple"); }catch(Exception e){}; },呵呵。卖弄了。
king_2007 2014-06-18
  • 打赏
  • 举报
回复
引用 1 楼 dcxy0 的回复:
http://www.blogjava.net/jzgl-ever/archive/2008/07/26/217704.html 看下这个
这位高手,你是不是看书看多了,女娲和苹果,草莓,葡萄有什么关系,你看串了,那个是《设计模式之禅》吧?
king_2007 2014-06-18
  • 打赏
  • 举报
回复
让你们先看了代码再说吧。 public interface fruit { public void grow(); public void harvest(); public void plant(); } public class apple implements fruit { private int treeage; public void grow() {p("apple is growing......"); } public void harvest() { p("apple has been harvested"); } public void plant() { p("apple has been planted"); } public static void p(String msg) { System.out.println(msg); } public int gettreeage() { return treeage; } public void settreeage(int treeage) { this.treeage=treeage; } } public class grape implements fruit { private boolean seedless; public void grow() { apple.p("grape is growing"); } public void harvest() { apple.p("grape has been harvested"); } public void plant() { apple.p("grape has been planted"); } public boolean getseedless() { return seedless; } public void setseedless(boolean seedless) { this.seedless=seedless; } } public class strawberry implements fruit { public void grow() { apple.p("strawberry is growing..."); } public void harvest() { apple.p("strawberry has been harvested"); } public void plant() { apple.p("strawberry has been planted"); } } public class fruitgardener { public static fruit factory (String which)throws badfruitException { if (which.equalsIgnoreCase("apple")) { return new apple(); }else if(which.equalslgnoreCase("grape")) { return new grape(); }else if(which.equalslgnoreCase("strawberry")) { return new strawberry(); }else { throw new badfruitException("bad fruit request"); } } } public clas badfruitException extends Exception { public badfruitException(String msg) { super(msg); } } public class test { public static void main(String args[]) { try{ fruitgardener.factory("apple"); fruitgardener.factory("grape"); fruitgardener.factory("strawberry"); }catch(Exception e){}; } } 运行这些代码后,并没有任何的提示或者运行后的实际意义,我不知道这本书的作者是不是写错了代码了,尤其是那个红色标记的静态的fruit方法?是不是应该写成void 的,然后修改if 语句体中的代码,才能显示出简单的工厂模式呢?高手你们说是么??

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧