GoF设计模式(六) - 适配器模式

前言

适配器模式(Adapter):将一个接口转换成客户希望的另一个接口,使接口不兼容的那些类可以一起工作,这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能,适配器模式分为类适配器模式和对象适配器模式。

角色

Target(目标抽象类):目标抽象类定义客户所需接口,可以是一个抽象类或接口,也可以是具体类。

Adapter(适配器类):适配器可以调用另一个接口,作为一个转换器,对Adaptee和Target进行适配,适配器类是适配器模式的核心。

Adaptee(适配者类):适配者即被适配的角色,它定义了一个已经存在的接口,这个接口需要适配,适配者类一般是一个具体类,包含了客户希望使用的业务方法,在某些情况下可能没有适配者类的源代码。

 

类适配器模式

1. 创建Target接口

public interface Target {
    void request();
}

2. 创建适配者类(需要适配的类)

public class Adaptee {

    public void adapteeRequest(){
        System.out.println("适配请求");
    }
}

3. 创建适配器类

public class Adapter extends Adaptee implements Target{

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

4. 测试

@SpringBootTest
class AdapterDemoApplicationTests {

	@Test
	void contextLoads() {
        Target adaptee = new Adapter();
        adaptee.request();
    }

}

 

对象适配器模式

与类的适配器模式不同的是,对象的适配器模式不是使用继承关系连接到Adaptee类,而是使用委派关系连接到Adaptee类

1. 适配器类

public class Adapter implements Target{

    private Adaptee adaptee;

    public Adapter(Adaptee adaptee){
        this.adaptee = adaptee;
    }

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

2. 测试

@SpringBootTest
class AdapterDemoApplicationTests {

	@Test
	void contextLoads() {
        Target adaptee = new Adapter(new Adaptee());
        adaptee.request();
    }

}

示例

我们国家的民用电都是 220V,日本是 110V,而我们的手机充电一般需要 5V,这时候要充电,就需要一个电压适配器,将 220V 或者 100V 的输入电压变换为 5V 输出。

1. 电流接口类 (目标类)

public interface ElectricCurrent {
    void outPut();
}

2. 实现类 110V和220V

public class Ac110 implements ElectricCurrent {

    @Override
    public void outPut() {
        System.out.println("输出110V AC");
    }
}
public class Ac220 implements ElectricCurrent {
    @Override
    public void outPut() {
        System.out.println("输出220V AC");
    }
}

3. 适配者类

public interface Dc5Adaptee {

    void outputDC5V();
}

4. 适配者实现类 china&japan

public class ChinaDc5Adaptee implements Dc5Adaptee {

    @Override
    public void outputDC5V() {
        System.out.println("China DC 5V");
    }
}
public class JapanDc5Adaptee implements Dc5Adaptee {
    @Override
    public void outputDC5V() {
        System.out.println("Japan DC 5V");
    }
}

5. 适配器类

public class Dc5Adapter implements ElectricCurrent {

    private Dc5Adaptee dc5Adaptee;

    public Dc5Adapter(Dc5Adaptee dc5Adaptee){
        this.dc5Adaptee = dc5Adaptee;
    }

    @Override
    public void outPut() {
        this.dc5Adaptee.outputDC5V();
    }
}

6. 工厂类 (扩展)

public class ElectricCurrentFactory {

    public static ElectricCurrent produce(String type){
        ElectricCurrent electricCurrent;
        switch (type){
            case "China_AC":
                electricCurrent = new Ac220();
                break;
            case "Japan_AC":
                electricCurrent = new Ac110();
                break;
            case "China_DC":
                electricCurrent = new Dc5Adapter(new ChinaDc5Adaptee());
                break;
            case "Japan_DC":
                electricCurrent = new Dc5Adapter(new JapanDc5Adaptee());
                break;
            default:
                throw new RuntimeException("not type electricCurrent");
        }

        return electricCurrent;
    }
}

7. 测试

@SpringBootTest
class AdapterDemoApplicationTests {


    @Test
    void contextLoads1() {
        ElectricCurrent electricCurrent = ElectricCurrentFactory.produce("China_DC");
        electricCurrent.outPut();
    }
}

 

优缺点

优点

  • 更好的复用性

系统需要使用现有的类,而此类的接口不符合系统的需要。那么通过适配器模式就可以让这些功能得到更好的复用。

  • 透明、简单

客户端可以调用同一接口,因而对客户端来说是透明的。这样做更简单 & 更直接

缺点

  • 过多的使用适配器,会让系统非常零乱,不易整体进行把握

源码 :https://download.csdn.net/download/javanbme/18589526

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

让程序飞

您的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值