设计模式之六:适配器模式(Adapter Pattern)

本文介绍了适配器模式的基本概念及其实现方式,通过具体的Java代码示例展示了如何使用适配器模式解决接口不兼容的问题,使不同接口的类能够协同工作。

《设计模式》GOF 说明:将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
适配器模式 是类对象结构模式。   

http://www.bkjia.com/Androidjc/863354.html

http://www.tuicool.com/articles/ymaUnm

http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1029/485.html    //BaseAdapter

http://www.chawenti.com/articles/11684.html   //ListAdapter

http://www.cnblogs.com/qianxudetianxia/archive/2012/02/27/2010965.html

//目标角色, 开放给客户的接口 clientInterface
public  interface Target {
     public  void  clientInterface();
}

 //源角色
public class AdapterSource {
      public AdapterSource() { }
      public void sourceRole() {
           System.out.println("sourceRole() ========");
     }
}

//适配器角色,将接口sourceRole()转化为给客户的接口clientInterface
public  class  Adapter   implements  Target {
     //被代理的设备
     private  AdapterSource   adapterSource;
     //装入被代理的设备
    public Adapter(AdapterSource  adapterSource) {
         super();
         this.adapterSource  =  adapterSource;
   } 

   @Override
   public void clientInterface() {
         adapterSource.sourceRole();
    }
}

/**
 * Apapter模式使得原本由于接口不兼容而不能再一起工作的那些类可以一起工作
 */
public class Client {
public static void main(String[] args) {
     AdapterSource as = new AdapterSource();
     Target target = new Adapter(as);
      target.clientInterface();
     }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值