【设计模式】适配器模式

本篇文章总结自 【狂神说Java适配器模式】bilibili 狂神说Java适配器模式

适配器模式核心

当一个类需要使用到另外一个类的方法时,而这两个类之间并没有任何关系,如果一味的使用 new 对象的方式则非常冗余,所以需要一个适配器来拉近两者的关系,例如 B 类持有适配器对象,将 A 对象传入适配器中,让适配器调用 A 的方法

适配器模式案例一【类适配器模式】

电脑没有插网线的口,需要一个转接头将网线口转为 usb 接口。有电脑、网线、转接头三个类,电脑调用转接头的方法,转接头调用网线的方法,代码结构:

在这里插入图片描述
网线类:

public class NetLine {
    public void toNet() {
        System.out.println("连接网线,可以上网");
    }
}

电脑类,其方法中需要传入一个适配器对象:

public class Computer {
    public void upNet(AdapterNet adapterNet) {
        adapterNet.connectNetLine();
    }
}

适配器接口类:

public interface AdapterNet {
    void connectNetLine();
}

适配器实现类,实现类继承了网线类,方便调用其方法:

public class AdapterNetImpl extends NetLine implements AdapterNet {
    @Override
    public void connectNetLine() {
        super.toNet();
    }
}

测试与结果:

public static void main(String[] args) {
	new Computer().upNet(new AdapterNetImpl());
}

在这里插入图片描述
使用类适配器时可以发现,适配器是继承网线类的,如果还要求该适配器去适配其他网线,只继承当前的网线类有局限性,所以引入 对象适配器模式

适配器模式案例二【对象适配器模式】

对象适配器模式让适配器不再继承任何类,但需要持有网线的实例。之前的代码结构不变

适配器实现类,在实例化适配器时要传入一个网线类:

public class AdapterNetImpl implements AdapterNet {

    private NetLine netLine;

    public AdapterNetImpl(NetLine nl) {
        this.netLine = nl;
    }

    @Override
    public void connectNetLine() {
        //super.toNet();
        this.netLine.toNet();
    }
}

测试与结果:

public static void main(String[] args) {
	new Computer().upNet(new AdapterNetImpl(new NetLine()));
}

在这里插入图片描述
对象适配器模式可以将不同的网线适配到一台电脑上。如果要求该适配器能够选择性地去适配网线、显示器、耳机口,那么之前的模式又不符合要求,引入 接口适配器模式

适配器模式案例三【接口适配器模式】

该模式在适配器接口中加入了连接显示器与耳机方法,新建了一个实现该接口的抽象类,真正的适配器继承该抽象类并选择重写哪些方法(选择适配那些端口),代码结构:
在这里插入图片描述
适配器接口:

public interface Adapter {
    void connectNetLine(NetLine netLine);

    void connectMonitor(Moniter moniter);

    void connectEarPhone(EarPhone earPhone);
}

适配器抽象类:

public abstract class AdapterAbstract implements Adapter {

    @Override
    public void connectNetLine(NetLine netLine) {
        netLine.toNet();
    }

    @Override
    public void connectMonitor(Moniter moniter) {
        moniter.toMoniter();
    }

    @Override
    public void connectEarPhone(EarPhone earPhone) {
        earPhone.toEarPhone();
    }

}

DIY的适配器:

public class AdapterDIY extends AdapterAbstract {

    @Override
    public void connectNetLine(NetLine netLine) {
        System.out.println("适配网线");
        super.connectNetLine(netLine);
    }

    @Override
    public void connectMonitor(Moniter moniter) {
        System.out.println("适配显示器");
        super.connectMonitor(moniter);
    }
}

电脑类:

public class Computer {
    public void upNet(Adapter adapter, NetLine netLine) {
        adapter.connectNetLine(netLine);
    }

    public void moniter(Adapter adapter, Moniter moniter) {
        adapter.connectMonitor(moniter);
    }
}

测试与结果:

public static void main(String[] args) {
        Computer computer = new Computer();
        AdapterDIY adapterDIY = new AdapterDIY();
        
        computer.upNet(adapterDIY, new NetLine());
        computer.moniter(adapterDIY, new Moniter());
}

在这里插入图片描述
接口的适配器模式:当不希望实现一个接口中所有的方法时,可以创建一个适配器抽象类,实现所有方法,写自定义适配器的时候,继承抽象类即可

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值