JiveJdon Community Forums
在线100人   首页   主题总表   培训咨询   精华   查搜   注册    登陆
首页 » 论坛 » 设计模式、框架和架构
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表
???en_US.forumThreadNext.name??? 下一主题
Go 总共有 5 回复 / 1
 发表新帖子   回复该主题贴
tanmingbo

悄悄话
发表文章: 3
注册时间: 2003年10月15日 10:23
请问板桥里人Adapter模式后半部分? 2003年10月15日 11:24 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
标签列表 adapter模式(7)     
在PegAdapter类中:
public void insert(String str) {
roundPeg.insertIntoHole(str);
}
请问,insert()方法是ISquarePeg接口中的,为什么却要调用roundPeg.insertIntoHole()方法呢?
是不是应该写成:squarePeg.insert(str);
然后,再实现IRoundPeg中的方法:
public void insertIntoHole(String str) {
squarePeg.insertIntoHole(str);
}

这是我的理解,恳请各位同行指正!
相关资料在本站设计模式中。
**:15477736 MSN:tan_mingbo@hotmail.com
banq

悄悄话
发表文章: 9290
注册时间: 2002年08月03日 17:08
Re: 请问板桥里人Adapter模式后半部分? 2003年10月15日 12:48 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题


public class PegAdapter extends SquarePeg{

  private RoundPeg roundPeg;

  public PegAdapter(RoundPeg peg)(this.roundPeg=peg;)

  public void insert(String str){ roundPeg.insertIntoHole(str);}

}



PegAdapter这段代码只是说明两种方式:组合和继承,extends SquarePeg属于继承,RoundPeg roundPeg属于组合,这两点是Adapter模式的主要点,至于insert方法如何实现不重要,可以根据需求任意设定,不属于Adapter模式范围之内的定义。


banq

悄悄话
发表文章: 9290
注册时间: 2002年08月03日 17:08
Re: 请问板桥里人Adapter模式后半部分? 2003年10月15日 12:48 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题


PegAdapter这段代码只是说明两种方式:组合和继承,extends SquarePeg属于继承,RoundPeg roundPeg属于组合,这两点是Adapter模式的主要点,至于insert方法如何实现不重要,可以根据需求任意设定,不属于Adapter模式范围之内的定义。




public class PegAdapter extends SquarePeg{

  private RoundPeg roundPeg;

  public PegAdapter(RoundPeg peg)(this.roundPeg=peg;)

  public void insert(String str){ roundPeg.insertIntoHole(str);}

}


tanmingbo

悄悄话
发表文章: 3
注册时间: 2003年10月15日 10:23
Re: 请问板桥里人Adapter模式后半部分? 2003年10月15日 13:18 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
在PegAdapter中,应该将两个方法都实现,否则,会是一个错误。
banq

悄悄话
发表文章: 9290
注册时间: 2002年08月03日 17:08
Re: 请问板桥里人Adapter模式后半部分? 2003年10月15日 18:59 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
adapter另外一段代码,如下,WhatIHave是已经有的类,是adaptee,是将要被适配的对象,WhatIWant是是需要,是需求,试图使用Adapter模式达到这个需求,WhatIUse是客户端,表示客户端将如何使用WhatIWant。

客户端WhatIUse是希望实现WhatIWant的f方法,但是我们目前资源只有WhatIHave,那么通过Adapter模式来转换它,ProxyAdapter就是将WhatIHave转变为WhatIWant,就类似,日本带来一个笔记本,需要110V,中国都是220V,怎么办,使用变压器将220V转换为110V。

WhatIWant的f方法内容实现是根据需要,这里是
whatIHave.g();
whatIHave.h();
当然,也可以写成:
whatIHave.g();


whatIHave.h();

如何写,取决于实际需要,这已经不在模式定义范围之内。


class WhatIHave {
public void g() {}
public void h() {}
}

interface WhatIWant {
void f();
}

class ProxyAdapter implements WhatIWant {
WhatIHave whatIHave;
public ProxyAdapter(WhatIHave wih) {
whatIHave = wih;
}
public void f() {
// Implement behavior using
// methods in WhatIHave:
whatIHave.g();
whatIHave.h();
}
}

class WhatIUse {
public void op(WhatIWant wiw) {
wiw.f();
}
}
liloboy

悄悄话
发表文章: 51
注册时间: 2003年10月14日 02:02
Re: 请问板桥里人Adapter模式后半部分? 2003年12月16日 19:23 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
这个例子是从Bob Tarr的例子里面来的,关于具体的解释,请看Design Pattern的教材
应该比二手的解释要好.
这个主题有 5 回复 / 1Go
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表    返回页首返回页首
???en_US.forumThreadNext.name??? 下一主题
热点TAG: AOP cache 缓存 DDD EJB 集群 设计模式 Hibernate IOC JiveJdon OO RBAC Seam Spring Struts
google yahoo 新浪ViVi 365Key网摘 天极网摘 CSDN网摘 添加到百度搜藏 POCO网摘 博采网摘
查询本论坛内 回复超过的热门帖子
     回复该主题贴
标题
 
粗体 斜体 下划线 插入图片 插入代码 插入url链接 插入附件
内容
 

手机阅读 add to google add to yahoo
解惑之道在J道 ,打造中国最具影响力的的企业软件社区
OpenSource JIVEJDON v3.0 Powered by JdonFramework Code © 2002-08 jdon.com
anti spam