13-Adapter

接下来的两个模式目的就是:Connecting different types

Adapter takes one type and produces an interface to some other type. When you’ve got this , and you need that , Adapter solves the problem. The only requirement is to produce a that , and there are a number of ways you can accomplish this adaptation.

//: adapter:SimpleAdapter.java

// "Object Adapter" from GoF diagram

package adapter;

import junit.framework.*;

 

class Target {

  public void request() {}

}

 

class Adaptee {

  public void specificRequest() {

    System.out.println("Adaptee: SpecificRequest");

  }

}

 

class Adapter extends Target {

  private Adaptee adaptee;

  public Adapter(Adaptee a) {

    adaptee = a;

  }

  public void request() {

    adaptee.specificRequest();

  }

}

 

public class SimpleAdapter extends TestCase  {

  Adaptee a = new Adaptee();

  Target t = new Adapter(a);

  public void test() {

    t.request();

  }

  public static void main(String args[]) {

    junit.textui.TestRunner.run(SimpleAdapter.class);

  }

} ///:~

......


正如作者论述的:

I’m taking liberties with the term “proxy” here, because in Design Patterns they assert that a proxy must have an identical interface with the object that it is a surrogate for. However, if you have the two words together: “proxy adapter,” it is perhaps more reasonable.


Adapter设计模式在格式上是和Proxy有很大相似之处.都是很好地应用了组合或者继承构造了新类。这个在实际应用当中就能体会到,总是看简单样例,感觉没有什么区别....继续前行!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值