跳转至

适配器模式

时间:2017-08-28 20:47:08

参考:

  1. 设计模式之禅(第二版)-- 秦小波

适配器模式 (Adapter Pattern)#

定义#

Convert the interface of a class into another interface clients expect. Adapter lets class work together that couldn't otherwise because of incompatible(不相容的) interfaces.

将一个类的接口转变成客户端所期待的另一种接口,从而使原本因为接口不匹配而无法在一起工作的两个类能够在一起工作。

具体实现#

  1. 创建一个类实现期望的接口,继承另外一个需要转换的接口,接口的具体实现,通过调用继承类的方法实现。
  2. 创建一个类实现期望的接口,引用需要转换的类(一个或多个作为成员类),接口的具体实现,通过调用引用的类的方法实现。

使用范围#

  1. 出现在项目后期维时。