二十三种设计模式之适配器模式

概念介绍

适配器模式(Adapter Pattern)将某个类的接口转换成客户端期望的另一个接口表 示,主的目的是兼容性,让原本因接口不匹配不能一起工作的两个类可以协同 工作。其别名为包装器(Wrapper)

适配器模式属于结构型模式。

主要分为三类:

  • 类适配器模式(class adapter pattern)

  • 对象适配器模式(object adapter pattern)

  • 缺省适配器模式(default adapter pattern),也叫默认适配器模式、接口适配器模式

结构

  • 目标角色(target):这是客户所期待的接口。目标可以是具体的或抽象的类,也可以是接口

  • 适配者角色(adaptee):已有接口,但是和客户器期待的接口不兼容。

  • 适配器角色(adapter):将已有接口转换成目标接口。

优劣势

优势
  • 客户端通过适配器可以透明地调用目标接口。

  • 复用了现存的类,程序员不需要修改原有代码而重用现有的适配者类。

  • 将目标类和适配者类解耦,解决了目标类和适配者类接口不一致的问题。

劣势

对类适配器来说,更换适配器的实现过程比较复杂。

栗子

在日常生活中,我们的家庭用电是交流电,像我们笔记本用的却是直流电,那么我们就需要一个电源适配器将交流电转为直流电。

类适配器模式

类图

代码示例
/**
 * @author yulecha
 * @version 1.0.0
 * @ClassName Target.java
 * @Description 我们需要直流电
 * @createTime 2019年10月26日 14:47:00
 */
public interface Target {

    void directCurrent();
}
/**
 * @author yulecha
 * @version 1.0.0
 * @ClassName Adaptee.java
 * @Description 适配者
 * @createTime 2019年10月26日 14:49:00
 */
public class Adaptee {

    public String type = "交流电";

    public void alternatingCurrent() {
        System.out.println("电流为:" + type);
    }
}
/**
 * @author yulecha
 * @version 1.0.0
 * @ClassName Adapter.java
 * @Description 适配器
 * @createTime 2019年10月26日 14:52:00
 */
public class Adapter extends Adaptee implements Target {

    @Override
    public void directCurrent() {
        type = "直流电";
        alternatingCurrent();
    }
}
/**
 * @author yulecha
 * @version 1.0.0
 * @ClassName Client.java
 * @Description 客户端
 * @createTime 2019年10月26日 14:55:00
 */
public class Client {

    public static void main(String[] args) {
        Adapter adapter = new Adapter();
        adapter.directCurrent();
    }
}


电流为:直流电

对象适配器模式

类图

代码示例
/**
 * @author yulecha
 * @version 1.0.0
 * @ClassName Target.java
 * @Description 我们需要直流电
 * @createTime 2019年10月26日 14:47:00
 */
public interface Target {

    void directCurrent();
}
/**
 * @author yulecha
 * @version 1.0.0
 * @ClassName Adaptee.java
 * @Description 适配者
 * @createTime 2019年10月26日 14:49:00
 */
public class Adaptee {

    public String type = "交流电";

    public void alternatingCurrent() {
        System.out.println("电流为:" + type);
    }
}
/**
 * @author yulecha
 * @version 1.0.0
 * @ClassName Adapter.java
 * @Description 适配器
 * @createTime 2019年10月26日 14:52:00
 */
public class Adapter implements Target {

    private Adaptee adaptee;

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

    @Override
    public void directCurrent() {
        adaptee.type = "直流电";
        adaptee.alternatingCurrent();
    }
}
/**
 * @author yulecha
 * @version 1.0.0
 * @ClassName Client.java
 * @Description 客户端
 * @createTime 2019年10月26日 14:55:00
 */
public class Client {

    public static void main(String[] args) {
        Adaptee adaptee = new Adaptee();
        Adapter adapter = new Adapter(adaptee);
        adapter.directCurrent();
    }
}


电流为:直流电
接口适配器模式

这里,直流电也有很多种类,比如不同的电器所需要的直流电电压不同。抽象类适配器默认实现适配者的方法,客户端调用就不用关心多余的方法,只需要关注自己想要的那个直流电。

类图

/**
 * @author yulecha
 * @version 1.0.0
 * @ClassName Adaptee.java
 * @Description 适配者
 * @createTime 2019年10月26日 16:00:00
 */
public interface Adaptee {

    void alternatingCurrent1();

    void alternatingCurrent2();
}
/**
 * @author yulecha
 * @version 1.0.0
 * @ClassName AbsAdapter.java
 * @Description 抽象的适配器(默认实现方法)
 * @createTime 2019年10月26日 15:59:00
 */
public abstract class AbsAdapter implements Adaptee {

    @Override
    public void alternatingCurrent1() {
    }

    @Override
    public void alternatingCurrent2() {
    }
}
/**
 * @author yulecha
 * @version 1.0.0
 * @ClassName Client.java
 * @Description 客户端
 * @createTime 2019年10月26日 16:03:00
 */
public class Client {

    public static void main(String[] args) {
        AbsAdapter absAdapter = new AbsAdapter() {
            @Override
            public void alternatingCurrent1() {
                System.out.println("直流电一");
            }
        };
        absAdapter.alternatingCurrent1();
    }
}


直流电一

总结

  • 类适配器,以类给到,在Adapter里,就是将src当做类,继承。

  • 对象适配器,以对象给到,在Adapter里,将src作为一个对象,持有。

  • 接口适配器,以接口给到,在Adapter里,将src作为一个接口,实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值