设计模式袖珍版 连续转载之 - Adapter(适配器)

<script language="javascript" type="text/javascript"> showbanner(6,6,1); </script><script type="text/javascript"> google_ad_client ="pub-2141342037947367";google_ad_width = 120;google_ad_height =240;google_ad_format = "120x240_as";google_ad_channel="8570654326";google_color_border = "CCCCCC";google_color_bg ="FFFFFF";google_color_link = "000000";google_color_url ="666666";google_color_text = "333333"; </script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-2141342037947367&dt=1117558259906&lmt=1117558259&prev_fmts=468x60_pas_abgnc&format=120x240_as&output=html&channel=8570654326&url=http%3A%2F%2Fwww.javaresearch.org%2Farticle%2Fshowarticle.jsp%3Fcolumn%3D31%26thread%3D12465&color_bg=FFFFFF&color_text=333333&color_link=000000&color_url=666666&color_border=CCCCCC&ref=http%3A%2F%2Fwww.javaresearch.org%2Farticle%2Fallarticles.jsp%3Fstart%3D690%26thRange%3D30&cc=476&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=16&u_tz=480&u_java=true" frameborder="0" width="120" scrolling="no" height="240" allowtransparency="65535">
原作:fanix

定义:
将两个不兼容的类纠合在一起使用,属于结构型模式,需要有Adaptee(被适配者)和Adaptor(适配器)两个身份.

为何使用?
我们经常碰到要将两个没有关系的类组合在一起使用,第一解决方案是:修改各自类的接口,但是如果我们没有源代码,或者,我们不愿意为了一个应用而修改各自的接口。 怎么办? 

使用Adapter,在这两种接口之间创建一个混合接口(混血儿).

如何使用?
实现Adapter方式,其实"think in Java"的"类再生"一节中已经提到,有两种方式:组合(composition)和继承(inheritance).


假设我们要打桩,有两种类:方形桩 圆形桩.
程序代码:

  1. public class SquarePeg{
  2.   public void insert(String str){
  3.     System.out.println("SquarePeg insert():"+str);
  4.   }
  5. }
  6. public class RoundPeg{
  7.   public void insertIntohole(String msg){
  8.     System.out.println("RoundPeg insertIntoHole():"+msg);
  9. }
  10. }


现在有一个应用,需要既打方形桩,又打圆形桩.那么我们需要将这两个没有关系的类综合应用.假设RoundPeg我们没有源代码,或源代码我们不想修改,那么我们使用Adapter来实现这个应用:
程序代码:

  1. public class PegAdapter extends SquarePeg{
  2.   private RoundPeg roundPeg;
  3.   public PegAdapter(RoundPeg peg)(this.roundPeg=peg;)
  4.   public void insert(String str){ roundPeg.insertIntoHole(str);}
  5. }


在上面代码中,RoundPeg属于Adaptee,是被适配者.PegAdapter是Adapter,将Adaptee(被适配者RoundPeg)和Target(目标SquarePeg)进行适配.实际上这是将组合方法(composition)和继承(inheritance)方法综合运用.

PegAdapter首先继承SquarePeg,然后使用new的组合生成对象方式,生成RoundPeg的对象roundPeg,再重载父类insert()方法。从这里,你也了解使用new生成对象和使用extends继承生成对象的不同,前者无需对原来的类修改,甚至无需要知道其内部结构和源代码.

如果你有些Java使用的经验,已经发现,这种模式经常使用。

进一步使用
上面的PegAdapter是继承了SquarePeg,如果我们需要两边继承,即继承SquarePeg 又继承RoundPeg,因为Java中不允许多继承,但是我们可以实现(implements)两个接口(interface)
程序代码:

  1. public interface IRoundPeg{
  2.   public void insertIntoHole(String msg);
  3. }
  4. public interface ISquarePeg{
  5.   public void insert(String str);
  6. }


下面是新的RoundPeg 和SquarePeg, 除了实现接口这一区别,和上面的没什么区别。
程序代码:

  1. public class SquarePeg implements ISquarePeg{
  2.   public void insert(String str){
  3.     System.out.println("SquarePeg insert():"+str);
  4.   }
  5. }
  6. public class RoundPeg implements IRoundPeg{
  7.   public void insertIntohole(String msg){
  8.     System.out.println("RoundPeg insertIntoHole():"+msg);
  9.   }
  10. }


下面是新的PegAdapter,叫做two-way adapter:
程序代码:

  1. public class PegAdapter implements IRoundPeg,ISquarePeg{
  2.   private RoundPeg roundPeg;
  3.   private SquarePeg squarePeg;
  4.   // 构造方法
  5.   public PegAdapter(RoundPeg peg){this.roundPeg=peg;}
  6.   // 构造方法
  7.   public PegAdapter(SquarePeg peg)(this.squarePeg=peg;)
  8.   public void insert(String str){ roundPeg.insertIntoHole(str);}
  9. }

还有一种叫Pluggable Adapters,可以动态的获取几个adapters中一个。使用Reflection技术,可以动态的发现类中的Public方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值