语言与模式-08适配器模式
意图
将一个类的接口转换成另外一个客户希望的接口。Adapter 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

适用性
- 你想使用一个已经存在的类,而它的接口不符合你的需求。
 - 你想创建一个可以复用的类,该类可以与其他不相关的类或不可预见的类(即那些接口可能不一定兼容的类)协同工作。
 - (仅适用于对象Adapter)你想使用一些已经存在的子类,但是不可能对每一个都进行子类化以匹配它们的接口。对象适配器可以适配它的父类接口。
 
Java实现
假设目前做一个新系统,有如下代码结构。
public interface Car{
     public void drive();
}
public class Benz implements Car{
     public void drive(){
          System.out.println("Benz run");
     }
}
public class Cruze implements Car{
     public void drive(){
          System.out.println("Cruze run");
     }
}
有一个老系统,里面有如下代码
public class Smart{
     public void run(){
         System.out.println("Smart run");
     }
}
类适配器:
public class SmartCar extends Smart implements Car{
      public void drive(){
           this.run();
      }
}
对象适配器:
public class SmartCar implements Car{
     private Smart smart = new Smart();
     public void drive(){
          smart.run();
     }
}
Clojure实现
first-class function轻松解决!
(defn drive []
  (println "drive"))
(defn run []
  (println "run"))
(defn do-drive [f]
  (f))
(do-drive drive)
(do-drive run)