适配器模式

基础概念

定义:将一个类的接口(被适配者)转换成客户期望的另一个接口(目标类)
使原本接口不兼容的类可以一起工作。


图片.png

左边:接口3实现接口1的抽象接口,继承接口2。那么接口3就有接口1的外观和接口2的功能。
右边:接口3有接口1的外观,含有属性是接口2对象,这样接口3也能有接口3的功能。

适用场景

已经存在的类,它的方法和需求不匹配时(方法结果相同或相似)
注:不是软件设计阶段考虑的设计模式,是随着软件维护,由于不同产品、不同厂家造成功能类似而接口不相同情况下的解决方案。

优点

  • 能提高类的透明性和复用,现有的类复用但不需要改变
  • 目标类和适配器类解耦,提高程序扩展性
  • 符合开闭原则

缺点

  • 适配器编写过程需要全面考虑,可能会增加系统的复杂性
  • 增加系统代码可读的难度

类适配器

图片.png

目标类:

package com.geely.design.pattern.structural.adapter.classadapter;

/**
 * Created by geely
 */
public interface Target {
    void request();
}
package com.geely.design.pattern.structural.adapter.classadapter;

/**
 * Created by geely
 */
public class ConcreteTarget implements Target {
    @Override
    public void request() {
        System.out.println("concreteTarget目标方法");
    }

}

被适配者:

package com.geely.design.pattern.structural.adapter.classadapter;

/**
 * Created by geely
 */
public class Adaptee {
    public void adapteeRequest(){
        System.out.println("被适配者的方法");
    }

}

适配者:

package com.geely.design.pattern.structural.adapter.classadapter;

/**
 * Created by geely
 */
public class Adapter extends Adaptee implements Target{
    @Override
    public void request() {
        //...
        super.adapteeRequest();
        //...
    }
}

测试类:

package com.geely.design.pattern.structural.adapter.classadapter;

/**
 * Created by geely
 */
public class Test {
    public static void main(String[] args) {
        Target target = new ConcreteTarget();
        target.request();

        Target adapterTarget = new Adapter();
        adapterTarget.request();
    }
}

结果:


图片.png

对象适配器:

图片.png

目标类:

package com.geely.design.pattern.structural.adapter.objectadapter;

/**
 * Created by geely
 */
public interface Target {
    void request();
}
package com.geely.design.pattern.structural.adapter.objectadapter;


/**
 * Created by geely
 */
public class ConcreteTarget implements Target {
    @Override
    public void request() {
        System.out.println("concreteTarget目标方法");
    }

}

被适配者:

package com.geely.design.pattern.structural.adapter.objectadapter;

/**
 * Created by geely
 */
public class Adaptee {
    public void adapteeRequest(){
        System.out.println("被适配者的方法");
    }
}

适配类:

package com.geely.design.pattern.structural.adapter.objectadapter;

/**
 * Created by geely
 */
public class Adapter implements Target{
    private Adaptee adaptee = new Adaptee();

    @Override
    public void request() {
        //...
        adaptee.adapteeRequest();
        //...
    }
}

测试类:

package com.geely.design.pattern.structural.adapter.objectadapter;

/**
 * Created by geely
 */
public class Test {
    public static void main(String[] args) {
        Target target = new ConcreteTarget();
        target.request();

        Target adapterTarget = new Adapter();
        adapterTarget.request();
    }
}

结果:


图片.png

继承和组合优先选择组合。

一个小例子

插座提供了220V的交流电,可是手机充电需要5V直流电,所以就需要用到电压适配器。
目标类:

package com.geely.design.pattern.structural.adapter;

/**
 * Created by geely
 */
public interface DC5 {
    int outputDC5V();
}

被适配者:

package com.geely.design.pattern.structural.adapter;

/**
 * Created by geely
 */
public class AC220 {
    public int outputAC220V(){
        int output = 220;
        System.out.println("输出交流电"+output+"V");
        return output;
    }
}

适配类:

package com.geely.design.pattern.structural.adapter;

/**
 * Created by geely
 */
public class PowerAdapter implements DC5{
    private AC220 ac220 = new AC220();

    @Override
    public int outputDC5V() {
        int adapterInput = ac220.outputAC220V();
        //变压器...
        int adapterOutput = adapterInput/44;

        System.out.println("使用PowerAdapter输入AC:"+adapterInput+"V"+"输出DC:"+adapterOutput+"V");
        return adapterOutput;
    }
}

测试类:

package com.geely.design.pattern.structural.adapter;

/**
 * Created by geely
 */
public class Test {
    public static void main(String[] args) {
        DC5 dc5 = new PowerAdapter();
        dc5.outputDC5V();
    }
}

结果:


图片.png

源码:

// todo 学习SpringMVC源码时补充

你可能感兴趣的:(适配器模式)