设计模式原则19----适配器模式

个人博客:打开链接

适配器模式定义

Adapter Pattern:Convert the interface of a class into another interface clients expect.Adapter lets classes work together that couldn’t otherwise because of incompatible interface.
适配器模式:将一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。

适配器通用类图

这里写图片描述
适配器模式就是把一个接口或类转换成其他的接口或类,适配器模式就是一个包装模式。

  • Target目标角色:
    该角色定义把其他类转换为何种接口,也就是我们期望的接口
  • Adaptee源角色
    你想把谁转换为目标角色,这个谁就是源角色,它是已经存在的,运行良好的类或对象,经过适配角色的包装,它会成为一个新的角色
  • Adapter适配器角色
    适配器模式的核心角色,其他两个角色都存在,而适配器角色是需要新建立的,它的职责:把源角色转换为目标角色,怎么转换?通过继承或是关联的方式。

类适配器通用代码

目标角色:

public interface Target {
    //目标角色有自己的方法
    public void request();
}

目标角色的实现类:

public class ConcreteTarget implements Target {

    @Override
    public void request() {
        System.out.println("if you need any help, please call me!");
    }
}

源角色:

public class Adaptee {

    public void doSomeThing(){
        System.out.println("I'm kind of busy, leave me alone please!");
    }
}

适配器角色:

public class Adapter extends Adaptee implements Target {

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

}

场景类:

public class Client {

    public static void main(String[] args) {
        Target target = new ConcreteTarget();
        target.request();
        System.out.println("--------------------------");
        Target target2 = new Adapter();
        target2.request();
    }
}

对象适配器

对象适配器是通过对对象关联的方法进行的适配。
对象适配器uml图:
这里写图片描述

//arget 类:
public interface Target {
    public void request();
}

//ConcreteTarget 类:
public class ConcreteTarget implements Target {
    @Override
    public void request() {
        System.out.println("ConcreteTarget-----request()");
    }
}

//Adaptee01 类:
public class Adaptee01 {
    public void SpecificRequest1(){
        System.out.println("Adaptee01--------------SpecificRequest1()");
    }
}

//Adaptee02 类:
public class Adaptee02 {

    public void SpecificRequest2(){
        System.out.println("Adaptee02--------------SpecificRequest2()");
    }
}

//Adaptee03 类:
public class Adaptee03 {

    public void SpecificRequest3(){
        System.out.println("Adaptee03--------------SpecificRequest3()");
    }
}

//Adapter 类:
public class Adapter implements Target {

    private Adaptee01 adaptee01;
    private Adaptee02 adaptee02;
    private Adaptee03 adaptee03;

    public Adapter(Adaptee01 adaptee01, Adaptee02 adaptee02, Adaptee03 adaptee03) {
        super();
        this.adaptee01 = adaptee01;
        this.adaptee02 = adaptee02;
        this.adaptee03 = adaptee03;
    }

    @Override
    public void request() {
        // TODO Auto-generated method stub
        System.out.println("Adapter-----request()");
        adaptee01.SpecificRequest1();
        adaptee02.SpecificRequest2();
        adaptee03.SpecificRequest3();
    }
}

//Client 类:
public class Client {

    public static void main(String[] args) {
        Adaptee01 adaptee01 = new Adaptee01();
        Adaptee02 adaptee02 = new Adaptee02();
        Adaptee03 adaptee03 = new Adaptee03();
        Adapter adapter = new Adapter(adaptee01, adaptee02, adaptee03);
        adapter.request();
    }

}

优点

  • 适配器模式可以让两个没有任何关系的类在一起运行,只要一个适配器就能够搞定
  • 增加类的透明性
    我们访问的Target目标角色,但是具体的实现都委托给了源角色,而这些对高层次模块是透明的
  • 提高了类的复用度
  • 灵活性非常好
    如果不想用适配器,删除这个适配器就可以了,其他的代码都不要动,基本上就类似一个灵活的构件,想用就用,不想就删除。

注意事项

适配器模式使用的主要场景是应用扩展。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

勤奋的凯尔森同学

你的鼓励将是我创作的最大动力!

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

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

打赏作者

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

抵扣说明:

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

余额充值