请问板桥里人Adapter模式后半部分?

在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);
}

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



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模式范围之内的定义。


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);}

}


在PegAdapter中,应该将两个方法都实现,否则,会是一个错误。

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();
}
}

这个例子是从Bob Tarr的例子里面来的,关于具体的解释,请看Design Pattern的教材
应该比二手的解释要好.