我在书上看到的一段东西:
假设我们要打桩,有两种类:方形桩 圆形桩.
public class SquarePeg{
  public void insert(String str){
    System.out.println("SquarePeg insert():"+str);
  }}
public class RoundPeg{
  public void insertIntohole(String msg){
    System.out.println("RoundPeg insertIntoHole():"+msg);
}
}现在有一个应用,需要既打方形桩,又打圆形桩.那么我们需要将这两个没有关系的类综合应用.假设RoundPeg我们没有源代码,或源代码我们不想修改,那么我们使用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 pegAdapter = new PegAdapter( new RoundPeg() );
pegAdapter.insert();  //打圆形桩
问题如下:
那怎样用pegAdapter对象打打方形桩呢?

解决方案 »

  1.   

    定义长方形的桩,如果方形桩代码可以修改,则让方行桩继承于长方形桩,如果不能修改方形桩代码,则把长方形桩单独作为一个桩来考虑,楼主最开始的设计就有点问题.桩至少上面要有个父类或者接口的.interface Peg{
    public void insert(String msg);
    }
    class RectanglePeg implements Peg{...}
    class SquarePeg extends RectanglePeg {...}
    class RoundPeg implements Peg{...}说一下你的问题实现:
    public class PegAdapter {  private Object peg;  public PegAdapter(Object peg)(this.peg=peg;)  public void insert(String str){       if(Peg instantsof SquarePeg){
                 ((SquarePeg)peg).insert(str);
          }else if(Peg instantsof SquarePeg){
                 ((RoundPeg)peg).insertIntoHole(str);
          }.......    }}
      

  2.   

    更改:
    if(peg instantsof SquarePeg){
                 ((SquarePeg)peg).insert(str);
          }else if(peg instantsof SquarePeg){
                 ((RoundPeg)peg).insertIntoHole(str);
          }.......
      

  3.   

    楼上的好像没有把Adapter的思想表现出来吧!!谁是Adaptee呢?还请高手帮忙,刚接触设计模式
      

  4.   

    public class PegAdapter extends SquarePeg{
      private RoundPeg roundPeg;
        public PegAdapter(RoundPeg peg){this.roundPeg=peg;}
      public PegAdapter(){}
        public void insert(String str){ roundPeg.insertIntoHole(str);}    
    }PegAdapter pegAdapter = new PegAdapter();
    pegAdapter.insertINtoHole(str);
    我是刚学,这样行不行啊?
      

  5.   

    你的上面没有接口,如果有接口的话,接口就是Adaptee,所以就用的Object当做Adaptee而每一个Target:
    RectanglePeg
    SquarePeg
    RoundPeg
    所对应的Insert就是SpecificRequest
    还有就是:模式要活用,学习模式学的是一种解决问题的思路.我们研究模式的目的在于解决问题而不是为了模式而模式.楼主在这方面的修行有待提高.
      

  6.   

    public interface IRoundPeg{
      public void insertIntoHole(String msg);}public interface ISquarePeg{
      public void insert(String str);}
    public class SquarePeg implements ISquarePeg{
      public void insert(String str){
        System.out.println("SquarePeg insert():"+str);
      }}public class RoundPeg implements IRoundPeg{
      public void insertIntohole(String msg){
        System.out.println("RoundPeg insertIntoHole():"+msg);
      }
    }
    public class PegAdapter implements IRoundPeg,ISquarePeg{  private RoundPeg roundPeg;
      private SquarePeg squarePeg;  // 构造方法
      public PegAdapter(RoundPeg peg){this.roundPeg=peg;}
      // 构造方法
      public PegAdapter(SquarePeg peg)(this.squarePeg=peg;)  public void insert(String str){ roundPeg.insertIntoHole(str);}}PegAdapter  pegAdapter  =  new  PegAdapter(  new  RoundPeg()  );  
    pegAdapter.insert();    //打圆形桩  
    PegAdapter  pegAdapter  =  new  PegAdapter(  new  SquarePeg()  );  
    pegAdapter.insert();    //打方形桩  
    这个可以不?
      

  7.   

    哥们对于Adapter 的理解和我的不太一样,Adapter的使用情况是:
    当有某接口或者父类已经存在且最好不要修改的情况下,融入一个新的继承体系外的类.如:
    interface 人类{public void 吃饭(肉类 肉丁);}
    class 老人 implements 人类{public void 吃饭(肉类 肉丁){慢慢吃;}}
    class 小孩 implements 人类{public void 吃饭(肉类 肉丁){大口大口吃;}}之前系统用法:
    人类 某人 = new 老人();
    某人.吃饭(new 肉类());
    但是现在系统考虑加入一个类:狗,如下:
    class 狗类{public void 咬(肉类 肉丁){咬着吃;}}在不考虑修改 人类的继承体系以及狗类的代码情况下,考虑引入一个Adapter:class 食客Adapter 狗类 implements 人类{
    private Object 吃东西的;
    public 食客Adapter (人类 某人){吃东西的= 某人;}
    public 食客Adapter (狗类 某狗){吃东西的= 某狗;}public void 吃(肉类 肉丁){
    if(吃东西的 instanceof 人类){
    ((人类)吃东西的).吃饭(肉丁);
    else if(吃东西的 instanceof 狗类){
    ((狗类)吃东西的).咬(肉丁);
    }
    }
    }
    }你看的板桥里人的<Java设计模式>吧?他在讲Adapter的时候我个人认为不太好.因为他修改了源码了,他引入了IRoundPeg和ISquarePeg接口,如果可以随便修改接口的情况下,为什么还要引入Adapter呢,直接定义一个IPeg,定义:insertMe(String str)
    修改源程序:
    public class SquarePeg implements IPeg{
    public void insert(String str){
    System.out.println("SquarePeg insert():"+str);
    }
    public void insertMe(String str){
    this.insert(str);
    }
    }public class RoundPeg implements IPeg{
    public void insertIntohole(String msg){
    System.out.println("RoundPeg insertIntoHole():"+msg);
    }
    public void insertMe(String str){
    this.insertIntohole(str);
    }
    }
    使用的时候:
    IPeg peg = new RoundPeg();
    peg.insertMe(new String());他的代码最后使用:
    PegAdapter pegAdapter= new PegAdapter(new RoundPeg());
    pegAdapter.insert(new String());
    哪个更好,楼主已经明白了吧?当然,作者那么举例子也许仅仅是为了讲明白一个思想.
    实际开发需求变化无常,能最简单最方便最优雅解决问题的才是最好的.
      

  8.   

    非常感谢 skylovers(琴声刺骨--致力成为中国最年轻的中小企业项目专家)