适配器模式

使原本因接口不匹配而导致不兼容的两个类能兼容工作

类适配

继承原类,实现新接口

1 2 3
class Adapter extends OldClass implements NewInterface { ... }

对象适配

依赖原类,实现新接口

1 2 3
class Adapter implements NewInterface { private OldClass oldInstance; }

接口适配

创建新的抽象类,实现旧接口

1 2 3
abstract class Adapter implements OldInterface { void newFunc(); }

浙ICP备11005866号-12