在现实生活中,常常出现两个对象因接口不兼容而不能在一块儿工做的实例,这时须要第三者进行适配。python
例如,讲中文的人同讲英文的人对话时须要一个翻译,用直流电的笔记本电脑接交流电源时须要一个电源适配器,用计算机访问照相机的 SD 内存卡时须要一个读卡器等。程序员
在软件设计中也可能出现:须要开发的具备某种业务功能的组件在现有的组件库中已经存在,但它们与当前系统的接口规范不兼容,若是从新开发这些组件成本又很高,这时用适配器模式能很好地解决这些问题。spa
适配器模式(Adapter)的定义以下:将一个类的接口转换成客户但愿的另一个接口,使得本来因为接口不兼容而不能一块儿工做的那些类能一块儿工做。翻译
适配器模式分为类结构型模式和对象结构型模式两种,前者类之间的耦合度比后者高,且要求程序员了解现有组件库中的相关组件的内部结构,因此应用相对较少些。设计
该模式的主要优势以下。3d
其缺点是:对类适配器来讲,更换适配器的实现过程比较复杂。code
类适配器模式 可采用多重继承方式实现,如 C++ 可定义一个适配器类来同时继承当前系统的业务接口和现有组件库中已经存在的组件接口;Java 不支持多继承,但能够定义一个适配器类来实现当前系统的业务接口,同时又继承现有组件库中已经存在的组件。对象
对象适配器模式 可釆用将现有组件库中已经实现的组件引入适配器类中,该类同时实现当前系统的业务接口。如今来介绍它们的基本结构。blog
适配器模式(Adapter)包含如下主要角色。继承
类适配器模式的结构图如图所示
对象适配器模式的结构图如图所示
类适配器模式的代码以下
class Target(object): def request(self): pass class Adaptee(object): def specific_request(self): print("适配器代码被调用") class Adapter(Adaptee, Target): def request(self): self.specific_request() if __name__ == '__main__': t = Adapter() t.request()
对象适配器模式的代码以下
class Target(object): def request(self): pass class Adaptee(object): def specific_request(self): print("适配器代码被调用") class Adapter(Target): __adaptee = None def __init__(self, adaptee): self.__adaptee = adaptee def request(self): self.__adaptee.specific_request() if __name__ == '__main__': adaptee = Adaptee() t = Adapter(adaptee) t.request()
例1】用适配器模式(Adapter)模拟新能源汽车的发动机。
分析:新能源汽车的发动机有电能发动机(Electric Motor)和光能发动机(Optical Motor)等,各类发动机的驱动方法不一样,例如,电能发动机的驱动方法 electricDrive() 是用电能驱动,而光能发动机的驱动方法 opticalDrive() 是用光能驱动,它们是适配器模式中被访问的适配者。
客户端但愿用统一的发动机驱动方法 drive() 访问这两种发动机,因此必须定义一个统一的目标接口 Motor,而后再定义电能适配器(Electric Adapter)和光能适配器(Optical Adapter)去适配这两种发动机。
class Motor(object): def driver(self): pass class ElectricMotor(object): def electric_driver(self): print('electric_driver') class OpticalMotor(object): def optical_driver(self): print('optical_driver') class ElectricAdapter(Motor): def __init__(self): self.__motor = ElectricMotor() def driver(self): self.__motor.electric_driver() class OpticalAdapter(Motor): def __init__(self): self.__motor = OpticalMotor() def driver(self): self.__motor.optical_driver() if __name__ == '__main__': em = ElectricAdapter() em.driver() om = OpticalAdapter() om.driver()
适配器模式(Adapter)一般适用于如下场景。
适配器模式(Adapter)可扩展为双向适配器模式,双向适配器类既能够把适配者接口转换成目标接口,也能够把目标接口转换成适配者接口,其结构图如图所示。
class TwoWayTarget(object): def request(self): pass class TwoWayAdaptee(object): def specific_request(self): pass class TargetRealize(TwoWayTarget): def request(self): print("目标代码被调用") class AdapteeRealize(TwoWayAdaptee): def specific_request(self): print("适配者代码被调用") class TwoWayAdapter(TwoWayTarget, TwoWayAdaptee): __adaptee = None __target = None def __init__(self, twoway): if type(twoway) == AdapteeRealize: self.__adaptee = twoway elif type(twoway) == TargetRealize: self.__target = twoway def request(self): self.__target.request() def specific_request(self): self.__adaptee.specific_request() if __name__ == '__main__': adaptee = AdapteeRealize() target = TargetRealize() twa_ad = TwoWayAdapter(adaptee) twa_ad.specific_request() twa_ta = TwoWayAdapter(target) twa_ta.request()