设计模式之美(8)-结构型-适配器模式

设计模式之美(8)-结构型-适配器模式适配器模式的原理与实现适配器模式 AdapterDesig 这个模式就是用来做适配的 它将不兼容的接口转换为可兼容的接口 让原本由于接口不兼容而不能一起工作的类可以一起工作 适配器模式有两种实现方式 类适配器和对象适配器 类适配器使用继承关系来实现 对象适配器使用组合关系来实现 类适配器 基于继承 publicinterf voidf1 voidf2 voidfc publicclassA

适配器模式的原理与实现

 // 类适配器: 基于继承 public interface ITarget { 
    void f1(); void f2(); void fc(); } public class Adaptee { 
    public void fa() { 
    //... } public void fb() { 
    //... } public void fc() { 
    //... } } public class Adaptor extends Adaptee implements ITarget { 
    public void f1() { 
    super.fa(); } public void f2() { 
    //...重新实现f2()... } // 这里fc()不需要实现,直接继承自Adaptee,这是跟对象适配器最大的不同点 } // 对象适配器:基于组合 public interface ITarget { 
    void f1(); void f2(); void fc(); } public class Adaptee { 
    public void fa() { 
    //... } public void fb() { 
    //... } public void fc() { 
    //... } } public class Adaptor implements ITarget { 
    private Adaptee adaptee; public Adaptor(Adaptee adaptee) { 
    this.adaptee = adaptee; } public void f1() { 
    adaptee.fa(); //委托给Adaptee } public void f2() { 
    //...重新实现f2()... } public void fc() { 
    adaptee.fc(); } } 

针对这两种实现方式,在实际开发中,到底该如何选择使用哪一种,判定标准主要有两个,一个是Adaptee接口的格式,另一个是Adaptee和ITarget的契合程度。

如果 Adaptee接口并不多,那两种实现方式都可以。

如果 Adaptee接口很多,而且Adaptee和ITarget接口定义大部分都相同,那推荐使用类适配器,因为Adaptor复用父类Adaptee的接口,比起对象适配器的实现方式,Adaptor的代码量要少一些。

如果Adaptee的接口很多,而且Adaptee和ITarget接口定义大部分都不相同,那我们推荐使用对象适配器,因为组合结构相对于继承更加灵活。

适配器模式应用场景总结

适配器模式可以看作一种“补偿模式”,用来补救设计上的缺陷。如果在设计初期,就能协调规避接口不兼容的问题,那这种模式就没有应用的机会了。

  1. 封装有缺陷的接口设计
    假设一类的外部系统在接口设计方面有缺陷(比如包含大量静态方法),引入之后会影响到我们自身代码的可测试性。

public class CD { 
    //这个类来自外部sdk,我们无权修改它的代码 //... public static void staticFunction1() { 
    //... } public void uglyNamingFunction2() { 
    //... } public void tooManyParamsFunction3(int paramA, int paramB, ...) { 
    //... } public void lowPerformanceFunction4() { 
    //... } } // 使用适配器模式进行重构 public class ITarget { 
    void function1(); void function2(); void fucntion3(ParamsWrapperDefinition paramsWrapper); void function4(); //... } // 注意:适配器类的命名不一定非得末尾带Adaptor public class CDAdaptor extends CD implements ITarget { 
    //... public void function1() { 
    super.staticFunction1(); } public void function2() { 
    super.uglyNamingFucntion2(); } public void function3(ParamsWrapperDefinition paramsWrapper) { 
    super.tooManyParamsFunction3(paramsWrapper.getParamA(), ...); } public void function4() { 
    //...reimplement it... } } 

2. 统一多个类的接口设计

某个功能的实现依赖多个外部系统。通过适配器模式,将他们的接口适配为统一的接口定义,然后我们就可以使用多态的特性来复用代码逻辑。

 public class ASensitiveWordsFilter { 
    // A敏感词过滤系统提供的接口 //text是原始文本,函数输出用*替换敏感词之后的文本 public String filterSexyWords(String text) { 
    // ... } public String filterPoliticalWords(String text) { 
    // ... } } public class BSensitiveWordsFilter { 
    // B敏感词过滤系统提供的接口 public String filter(String text) { 
    //... } } public class CSensitiveWordsFilter { 
    // C敏感词过滤系统提供的接口 public String filter(String text, String mask) { 
    //... } } // 未使用适配器模式之前的代码:代码的可测试性、扩展性不好 public class RiskManagement { 
    private ASensitiveWordsFilter aFilter = new ASensitiveWordsFilter(); private BSensitiveWordsFilter bFilter = new BSensitiveWordsFilter(); private CSensitiveWordsFilter cFilter = new CSensitiveWordsFilter(); public String filterSensitiveWords(String text) { 
    String maskedText = aFilter.filterSexyWords(text); maskedText = aFilter.filterPoliticalWords(maskedText); maskedText = bFilter.filter(maskedText); maskedText = cFilter.filter(maskedText, "*"); return maskedText; } } // 使用适配器模式进行改造 public interface ISensitiveWordsFilter { 
    // 统一接口定义 String filter(String text); } public class ASensitiveWordsFilterAdaptor implements ISensitiveWordsFilter { 
    private ASensitiveWordsFilter aFilter; public String filter(String text) { 
    String maskedText = aFilter.filterSexyWords(text); maskedText = aFilter.filterPoliticalWords(maskedText); return maskedText; } } //...省略BSensitiveWordsFilterAdaptor、CSensitiveWordsFilterAdaptor... // 扩展性更好,更加符合开闭原则,如果添加一个新的敏感词过滤系统, // 这个类完全不需要改动;而且基于接口而非实现编程,代码的可测试性更好。 public class RiskManagement { 
    private List<ISensitiveWordsFilter> filters = new ArrayList<>(); public void addSensitiveWordsFilter(ISensitiveWordsFilter filter) { 
    filters.add(filter); } public String filterSensitiveWords(String text) { 
    String maskedText = text; for (ISensitiveWordsFilter filter : filters) { 
    maskedText = filter.filter(maskedText); } return maskedText; } } 

3. 替换依赖的外部系统

当我们把项目中依赖的一个外部系统替换为另一个外部系统的时候,利用适配器模式,可以减少对代码的改动。

// 外部系统A public interface IA { 
    //... void fa(); } public class A implements IA { 
    //... public void fa() { 
    //... } } // 在我们的项目中,外部系统A的使用示例 public class Demo { 
    private IA a; public Demo(IA a) { 
    this.a = a; } //... } Demo d = new Demo(new A()); // 将外部系统A替换成外部系统B public class BAdaptor implemnts IA { 
    private B b; public BAdaptor(B b) { 
    this.b= b; } public void fa() { 
    //... b.fb(); } } // 借助BAdaptor,Demo的代码中,调用IA接口的地方都无需改动, // 只需要将BAdaptor如下注入到Demo即可。 Demo d = new Demo(new BAdaptor(new B())); 

代理、桥接、装饰器、适配器的区别

代理模式:代理模式在不改变原始类接口的条件下,为原始类定义一个代理类,主要目的是控制访问,而非加强功能,这是它跟装饰器模式最大的不同。

桥接模式:桥接模式的目的是将接口部分和实现部分分离,而让它们可以较为容易、独立地加以改变。

装饰器模式:装饰器模式在不改变原始类接口的情况下,对原始类功能进行增强,并且支持多个装饰器的嵌套使用。

适配器模式:适配器模式是一种时候的补救策略,适配器提供跟原始类不同的接口,而代理模式、装饰器模式提供的都是跟原来类相同的接口。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/200287.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月20日 上午11:09
下一篇 2026年3月20日 上午11:09


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号