【设计模式】之适配器(Adapter)
适配器模式的定义为:将一个类的接口转换成客户端期望的另一个接口。适配器让原本因为接口不兼容而无法一起工作的类可以一起工作。
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
适配器模式的意图(Intent)就是为适配目标接口而转换已有的接口,一个adapter可以有多个adaptee,淡然adapter也可增加adaptee没有的功能。
适配器可以有两种版本,一种是对象适配器,adapter与adaptee是HAS-A的关系;另一种是类适配器,adapter继承自adaptee,两种类型各有优缺点。
类适配器需要用到多继承,target的接口需要public继承,而adaptee的实现需要private继承,这样就可已区分adaptee和target了。
根据《Header First Design Patterns》中的介绍,class dapter必须要用到multiple inheritance,这个应该是不对的吧?
因为adapter实现(implement)了target的接口,继承(extend)的是adaptee类,因此没有多继承依然可以实现class adapter。
适配器根据语言的不同可以有不同的用处,比如two-way class adapter只能在支持多继承的语言中使用,two-way class adapter可以把适配多个adaptee,
其本质就是adapter的可以重写实现相同但签名不同的函数
《Header First Design Patterns》中adapter模式的示例代码如下:
其intent是把一个Turkey适配成一个duck。
首先分别定义Duck和Turkey的interface。
public class DuckTestDrive { public static void main(String[] args) { MallardDuck duck = new MallardDuck(); WildTurkey turkey = new WildTurkey(); Duck turkeyAdapter = new TurkeyAdapter(turkey); System.out.println("The Turkey says..."); turkey.gobble(); turkey.fly(); System.out.println("\nThe Duck says..."); testDuck(duck); System.out.println("\nThe TurkeyAdapter says..."); testDuck(turkeyAdapter); } static void testDuck(Duck duck) { duck.quack(); duck.fly(); }}