软件艺术思考者  
混沌,彷徨,立志,蓄势...
公告
日历
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

随笔分类(86)

随笔档案(85)

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 
适配器模式
适配器模式可以把两个不相关的类组装在一起使用。实际上是继承和组合的综合运用。
让我们来看一个例子:
public interface Itable{
public void say(String str);
}
public interface Ichair{
public void say(String str);
}
public class Table implements  Itable{
  public void say(String str){
    System.out.println(" i am a table :"+str);
  }
}
public class Chair implements Ichair{
  public void say(String str){
    System.out.println(" i am a chair :"+str);
  }
}

public class AdapterFitment  extends Table{
private Ichair c;
  public AdapterFitment(Ichair c){
  this.c=c;
}
public void insert(String str){c.say(str);}
}

然后呢,这个组合家具就即能当桌子用又能当椅子用了。
posted on 2006-07-25 18:21 智者无疆 阅读(421) 评论(2)  编辑  收藏 所属分类: software project
评论:
  • # re: java模式学习(3)  anonymous Posted @ 2006-12-05 17:14
    adpater的目的是将一种物件视为另一种物件,文中最后部分的解释偏离了模式本身。  回复  更多评论   

  • # re: java模式学习(3)  anonymous Posted @ 2006-12-05 17:20
    deal with chair as a table

    public class Adapter2Table implements ITable{

    private IChair chair;

    public Adapter2Table(IChair chair){
    this.chair = chair;
    }

    public void Say(String str){
    chair.Say(str);
    }
    }  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
 
Copyright © 智者无疆 Powered by: 博客园 模板提供:沪江博客


   观音菩萨赞