适配器模式在Android开发中的应用

来源:互联网 发布:淘宝双皇冠店铺多少钱 编辑:程序博客网 时间:2024/06/14 13:52

*本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布

一、前言

适配器:Adapter,大家看到这个单词是不是非常眼熟了?没错,这就是我们平时用到ListView和RecyclerView时必定会配合使用的Adapter。列表显示数据几乎是任何一个安卓项目都会用到的一个功能,谷歌使用适配器模式来封装等使之异常灵活且非常实用,我们想要了解谷歌为什么这样做,就要先理解java适配器模式是怎样运作的,这篇文章我们就学习一下java设计模式之一的适配器模式以及它在安卓中的应用。

二、适配器模式

  • 使用场景:适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。
  • 引用网上的一个例子:笔记本电脑电源一般用的都是5V电压,但是我们的家用电是220V,我们要让笔记本充上电,最好的办法应该是通过一个工具把220V的电压转换成5V,这个工具就是适配器

好好理解一下设计模式的使用场景比起去记住代码是怎样写的效果要好很多,说不定你哪天编码的时候灵光一现用上了,那就是你真正掌握了。

适配器模式的种类

1. 类适配器

UML图(强烈建议大家去了解诶一下UML图,它很简单粗暴的把类与类之间的关系描述的很清楚易懂)

这里写图片描述

我们可以看到,目标接口Target就相当于是上面的5V电压,Adaptee就是被适配的220V电压,Adapter就是适配器。看代码:

/** * 目标:5V */public interface Target5V {    void chong5V();}
/** * 被适配电压:220V */public class Adaptee220V {    public void chong220V() {        System.out.println("充220V电啦");    }}
/** * 适配器 */public class Adapter extends Adaptee220V implements Target5V {    @Override    public void chong5V() {        System.out.println("充5V电啦");    }}
/** * 测试类 */public class Test {    public static void main(String[] args) {        Adapter adapter = new Adapter();        adapter.chong5V();        adapter.chong220V();    }}

输出结果:

充5V电啦充220V电啦

结合上面给点UML图稍微分析一下:
Adapter类继承了Adaptee220V类然后实现了接口Target5并实现了目标方法chong5V(),类适配器的一个特点就是Adapter会去继承被适配类,这样适配器就直接拥有了被适配类中的方法,所以类适配器的缺点就是不够灵活,让我们看一下另外一种适配器。

2. 对象适配器

UML图:

这里写图片描述

这里的Target依然是5V电压,Adaptee依然是220V电压,比起类适配器,Adapter和Adaptee的关系从继承变成了组合,上代码:

/** * 目标:5V */public interface Target5V {    void chong5V();}
/** * 被适配电压:220V */public class Adaptee220V {    public void chong220V() {        System.out.println("充220V电啦");    }}
/** * 适配器 */public class Adapter implements Target5V {    private Adaptee220V adaptee220V;    public Adapter(Adaptee220V adaptee220V) {        this.adaptee220V = adaptee220V;    }    @Override    public void chong5V() {        System.out.println("充5V电啦");    }    public void chong220V() {        adaptee220V.chong220V();    }}
/** * 测试类 */public class Test {    public static void main(String[] args) {        Adaptee220V adaptee220V = new Adaptee220V();        Adapter adapter = new Adapter(adaptee220V);        adapter.chong5V();        adapter.chong220V();    }}

输出结果:

充5V电啦充220V电啦

这里我们把Adaptee220V通过Adapter的构造方法传入到Adapter中,当调用Adapter的chong220V()方法时,实际是调用传进来Adaptee220V对象的chong220V方法,这样就会变得很灵活。

三、项目中的运用

之所以写这篇关于适配器设计模式的文章,是因为作者在以前有一次实际开发中,在没有学习过适配器模式的前提下,为了解决项目中的一个开发痛点,自己费劲脑筋想出来的一种模式,到现在才知道原来这个叫适配器模式,在学习了这个设计模式之后更加深入了解了一下。接下来,就来演示一下作者在实际工作中使用到的变异形态的对象适配器模式。
- 当时开发场景:当时作者开发一个基于POS机的APP,APP要求有打印功能,像订单打印、日期打印等等。我们知道POS机是有多种型号的,每种型号的POS机会有他们单独的SDK,也就是说打印方法是不同的!

/** * 第一种POS机 */public class PosPrinter1{    public void printOrder() {        System.out.println("第一种打印机:打印订单");    }    public void printMoney() {        System.out.println("第一种打印机:打印金额");    }    public void printDate() {        System.out.println("第一种打印机:打印日期");    }}
/** * 第二种POS机 */public class PosPrinter2 {    public void printOrder() {        System.out.println("第二种打印机:打印订单");    }    public void printMoney() {        System.out.println("第二种打印机:打印金额");    }    public void printDate() {        System.out.println("第二种打印机:打印日期");    }}
/** * 第一个调用打印的地方 */public class Activity1 {    public void printOrder(String posType) {        if ("POS1".equals(posType)) {            PosPrinter1 printer1 = new PosPrinter1();            printer1.printOrder();        } else if ("POS2".equals(posType)) {            PosPrinter2 printer2 = new PosPrinter2();            printer2.printOrder();        }    }    public void printMoney(String posType) {        if ("POS1".equals(posType)) {            PosPrinter1 printer1 = new PosPrinter1();            printer1.printMoney();        } else if ("POS2".equals(posType)) {            PosPrinter2 printer2 = new PosPrinter2();            printer2.printMoney();        }    }}
/** * 第二个调用打印的地方 */public class Activity2 {    public void printOrder(String posType) {        if ("POS1".equals(posType)) {            PosPrinter1 printer1 = new PosPrinter1();            printer1.printOrder();        } else if ("POS2".equals(posType)) {            PosPrinter2 printer2 = new PosPrinter2();            printer2.printOrder();        }    }    public void printMoney(String posType) {        if ("POS1".equals(posType)) {            PosPrinter1 printer1 = new PosPrinter1();            printer1.printMoney();        } else if ("POS2".equals(posType)) {            PosPrinter2 printer2 = new PosPrinter2();            printer2.printMoney();        }    }}

首先,我们每次调用打印的时候都会先判断POS机型号,因为型号不对程序肯定会报错,假如某一天,APP适配的POS机类型要增加一款,也就是说我每个判断POS机型号的地方都要多家一个if语句,并且还要把新POS机型的打印方法加上去,如果我有5个地方用到了打印,就要加5次,这样是很痛苦的,别问我为什么知道。。。经过2次这样的真实情况发生后,我暴走了,痛定思痛要想个办法结局,于是有了以下代码:

/** * 打印接口,因为每种POS机都必须实现这3种打印方法,所以定义了这个接口,类似于上面例子的Target接口。 */public interface IPrint {    /**     * 打印订单     */    void printOrder();    /**     * 打印金额     */    void printMoney();    /**     * 打印日期     */    void printDate();}
/** * 第一种POS机 */public class PosPrinter1 implements IPrint {    @Override    public void printOrder() {        System.out.println("第一种打印机:打印订单");    }    @Override    public void printMoney() {        System.out.println("第一种打印机:打印金额");    }    @Override    public void printDate() {        System.out.println("第一种打印机:打印日期");    }}
/** * 第二种POS机 */public class PosPrinter2 implements IPrint {    @Override    public void printOrder() {        System.out.println("第二种打印机:打印订单");    }    @Override    public void printMoney() {        System.out.println("第二种打印机:打印金额");    }    @Override    public void printDate() {        System.out.println("第二种打印机:打印日期");    }}
/** * 新增的第三种POS机 */public class PosPrinter3 implements IPrint {    @Override    public void printOrder() {        System.out.println("第三种打印机:打印订单");    }    @Override    public void printMoney() {        System.out.println("第三种打印机:打印金额");    }    @Override    public void printDate() {        System.out.println("第三种打印机:打印日期");    }}

每种POS机都实现IPrint中的打印方法

/** * 打印管理类,相当于Adapter */public class PrintManager implements IPrint {    private IPrint printer;    public PrintManager(String posType) {        if ("POS1".equals(posType)) {            printer = new PosPrinter1();        } else if ("POS2".equals(posType)) {            printer = new PosPrinter2();        } else if ("POS3".equals(posType)) {            printer = new PosPrinter2();        }    }    @Override    public void printOrder() {        printer.printOrder();    }    @Override    public void printMoney() {        printer.printMoney();    }    @Override    public void printDate() {        printer.printDate();    }}

这里我把POS机型号判断的代码放到了PrintManager的构造方法中,每次new出实例时就会去判断。然后我让PrintManager也实现了IPrint接口,因为我认为项目中的打印管理类必须要拥有这三种打印方法。

/** * 第一个调用打印的地方 */public class Activity1 {    public void printOrder(String posType) {        PrintManager printManager = new PrintManager(posType);        printManager.printOrder();    }    public void printMoney(String posType) {       PrintManager printManager = new PrintManager(posType);       printManager.printMoney();    }}
/** * 第二个调用打印的地方 */public class Activity2 {    public void printOrder(String posType) {        PrintManager printManager = new PrintManager(posType);        printManager.printOrder();    }    public void printMoney(String posType) {       PrintManager printManager = new PrintManager(posType);       printManager.printMoney();    }}

发现没,改过之后的代码,在不同的调用的地方再也不用担心新增POS机型需要改代码了,只需要在PrintManager的构造方法中增加一个判断就好了。要是改成单例:

public class PrintManager implements IPrint {    private IPrint printer;    private static PrintManager instance;    public static PrintManager getInstance(String posType) {        if (instance == null) {            synchronized (PrintManager.class) {                if (null == instance) {                    instance = new PrintManager(posType);                }            }        }        return instance;    }    private PrintManager(String posType) {        if ("POS1".equals(posType)) {            printer = new PosPrinter1();        } else if ("POS2".equals(posType)) {            printer = new PosPrinter2();        } else if ("POS3".equals(posType)) {            printer = new PosPrinter2();        }    }    @Override    public void printOrder() {        printer.printOrder();    }    @Override    public void printMoney() {        printer.printMoney();    }    @Override    public void printDate() {        printer.printDate();    }}
/** * 第一个调用打印的地方 */public class Activity1 {    public void printOrder(String posType) {        PrintManager.getInstance(posType).printOrder();    }    public void printMoney(String posType) {        PrintManager.getInstance(posType).printMoney();    }}

改完之后我哭了。。。我他吗的是个天才啊。。。后面果然加了多款POS机,但是我再也不痛苦了!看一下我的UML图:

这里写图片描述

其实,光看UML图,作者在项目中的这种写法已经不算是适配器模式了,因为Adaptee已经直接实现了Target中的方法,也就是说根本就不存在不满足目标接口这个说法了。。不过作者这样写完全是自己想出来的,没有基于任何设计模式,就现在目前的效果来说还是不错的,通过这篇文章,我对于适配器模式有了一个清晰的理解,同时也反思了一下我这种写法,还是很有收获的。

3 0
原创粉丝点击