论坛首页 Java企业应用论坛

Adapter模式(Class Adapter)

浏览 2872 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-05-05  
昨天讨论了Adapter模式的一种情况Object Adapter,他比较适用于某些时候的二次开发,但我们拿到某一个组件的class文件,没有源码,只有功能列表的情况时,某些时候整合需要Object adapter。Adapter还有另外一种情况 Class Adapter。
我们现在的程序使用接口 PutBags
public interface PutBags {
  public void putAllBags();
}

如果我们有一个类的class文件PutFootBall,而无法适用其源码。只能适用其中的putAllFootBall()方法。
public class PutFootBall {
  public void putAllFootBall(){
    System.out.println("Put Football....");
  }
}

而在我们现有的程序所适用的方式是
public class Put implements PutBags{
  public void putAllBags(){
    System.out.println("Realize method....");
  }
  public void putOther(){
    System.out.println("Other....");
  }
}

如果我们现在不想改变PutBags接口,并且不希望改变PutAllFootBall()此时

我们考虑,如何让Put类既有第三方类的功能,又不改变原有的接口继承?简

单的讲,要想某个类拥有其他的类的功能继承是最普遍的方法。所以我们使用

继承并实现的方式
public class Put extends PutFootBall implements PutBags{
  public void putAllBags(){
    putBall();  
}
}
   发表时间:2008-06-03  
putBall();     哪儿呢?
0 请登录后投票
   发表时间:2008-06-11  
我觉得不如改成这样:

public class Put implements PutBags{
   PutFootBall pb = new PutFootBall()
   public void putAllBags(){  
     putBall();    
   }  
   public void putAllFootBall(){
      pb.putAllFootBall();
   }
}

我总觉得adapter最佳用处是用来实现某些类接口不统一的情况得
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics