zoukankan      html  css  js  c++  java
  • 适配器模式(三种)简单使用

    前言

    适配器模式是将一个类的接口转换成客户希望的另外一个接口,身边很多东西都是适用于适配器模式的,笔记本的电源(也叫电源适配器),是将220V的交流电转换为笔记本电脑所需要的12V(电流先忽略),笔记本电脑的各种接口,VGA转Hdml,USB-TypeA 转 USB-TypeC,亦或者你在香港买了个手机,充电器是你生活中没见过的三孔插座通过一个转换头转换为国内常用的插头,很多例子都能很形象的解释这个设计模式。适配器模式(有时候也称包装样式或者包装)将一个类的接口适配成用户所期待的。一个适配允许通常因为接口不兼容而不能在一起工作的类工作在一起,做法是将类自己的接口包裹在一个已存在的类中。
    UML角色

    Source:需要被适配的类、接口、对象,即Datas。
    Destination:需要得到的类,Source通过适配得到的类对象,也就是我们期待得到的借口。
    Adapter:适配器类,协调Source和Destination,使两者能够协同工作。
    适用场景

    1,系统需要使用现有的类,但现有的类却不兼容。
    2,需要建立一个可以重复使用的类,用于一些彼此关系不大的类,并易于扩展,以便于面对将来会出现的类。
    3,需要一个统一的输出接口,但是输入类型却不可预知。
    Demo

    简单的抽象一个场景:手机充电需要将220V的交流电转化为手机锂电池需要的5V直流电,我们的demo就是写一个电源适配器,将 AC220v ——> DC5V,其实适配器模式可以简单的分为三类:类适配器模式、对象的适配器模式、接口的适配器模式。我们就以这三种模式来实现上述步骤。
    类适配器模式

    就上面提到的功能,简单的使用类适配器模式,Source类如下:

    package com.demo.adapter.classadapter;

    /**
     * Created by italkbb on 2018/1/24.
     */

    public class AC220 {
        public int output220V(){
            int output = 220;
            return output;
        }
    }

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13

    我们的目标类Destination,只需要定义方法,由适配器来转化:

    package com.demo.adapter.classadapter;

    /**
     * Created by italkbb on 2018/1/24.
     */

    public interface DC5 {
        int output5V();
    }

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10

    Adapter类如下:

    package com.demo.adapter.classadapter;

    /**
     * Created by italkbb on 2018/1/24.
     */

    public class PowerAdapter extends AC220 implements DC5 {
        @Override
        public int output5V() {
            int output = output220V();
            return (output / 44);
        }
    }

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14

    对于使用,也很简单:

        /**
         * 类适配器使用demo
         */
        private void initClassAdapter() {
            DC5 dc5 = new com.demo.adapter.classadapter.PowerAdapter();
            dc5.output5V();
        }

        1
        2
        3
        4
        5
        6
        7
        8

    因为java单继承的缘故,Destination类必须是接口,以便于Adapter去继承Source并实现Destination,完成适配的功能,但这样就导致了Adapter里暴露了Source类的方法,使用起来的成本就增加了。
    对象适配器模式

    对于同样的逻辑,我们在以对象适配器模式实现。我们保留AC220和DC5两个基本类,我们让Adapter持有Destination类的实例,然后再实现DC5,以这种持有对象的方式来实现适配器功能:

    package com.demo.adapter.objadapter;

    import com.demo.adapter.classadapter.AC220;
    import com.demo.adapter.classadapter.DC5;

    /**
     * Created by italkbb on 2018/1/24.
     */

    public class PowerAdapter implements DC5{
        private AC220 mAC220;

        public PowerAdapter(AC220 ac220){
            this.mAC220 = ac220;
        }

        @Override
        public int output5V() {
            int output = 0;
            if (mAC220 != null) {
                output = mAC220.output220V() / 44;
            }
            return output;
        }
    }

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26

    使用代码:

        /**
         * 对象适配器模式demo
         */
        private void initObjAdapter() {
            com.demo.adapter.objadapter.PowerAdapter adapter = new com.demo.adapter.objadapter.PowerAdapter(new AC220());
            adapter.output5V();
        }

        1
        2
        3
        4
        5
        6
        7
        8

    对象适配器和类适配器其实算是同一种思想,只不过实现方式不同。再回想装饰者模式,装饰者是对Source的装饰,使用者毫无察觉到Source被装饰,也就是用法不变。而对于适配器模式用法还是有改变的。
    接口适配器模式

    对于接口适配器模式,我们就不用担着眼于220->5,我们的接口可以有更多的抽象方法,这一点在android开发中有很多影子,动画的适配器有很多接口,但我们只需要关心我们需要的回调方法(详见AnimatorListenerAdapter类),我们把接口比作万能适配器:

    package com.demo.adapter.interfaceadapter;

    /**
     * Created by italkbb on 2018/1/24.
     */

    public interface DCOutput {
        int output5V();
        int output9V();
        int output12V();
        int output24V();
    }

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13

    然后我们要用的是5V的电压,所以关心5V的适配:

    package com.demo.adapter.interfaceadapter;

    import com.demo.adapter.classadapter.AC220;

    /**
     * Created by italkbb on 2018/1/24.
     */

    public class Power5VAdapter extends PowerAdapter {

        public Power5VAdapter(AC220 ac220) {
            super(ac220);
        }

        @Override
        public int output5V() {
            int output = 0;
            if (mAC220 != null) {
                output = mAC220.output220V() / 44;
            }
            return output;
        }
    }

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24

    但是我们必须存在一个中间适配器,用于实现默认的接口方法,以至于减少我们适配器的代码量,让代码更加清晰:

    package com.demo.adapter.interfaceadapter;

    import com.demo.adapter.classadapter.AC220;

    /**
     * Created by italkbb on 2018/1/24.
     * 这里抽象类其实就写了空方法,等着子类去实现需要的方法。
     */
    public abstract class PowerAdapter implements DCOutput{
        protected AC220 mAC220;

        public PowerAdapter(AC220 ac220){
            this.mAC220 = ac220;
        }

        @Override
        public int output5V() {
            return mAC220.output220V();
        }

        @Override
        public int output9V() {
            return mAC220.output220V();
        }

        @Override
        public int output12V() {
            return mAC220.output220V();
        }

        @Override
        public int output24V() {
            return mAC220.output220V();
        }
    }

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36

    这样一来我们就只需要重写父类我们关心的方法了,当然我们有时候可以省略Power5VAdapter类,因为内部类可以实现我们的方法,就跟使用setOnClickOnLintener(new OnClickOnLintener(){…})一样,我们来看使用:

        /**
         * 接口适配器模式demo
         */
        private void initinterfaceAdapter() {
            // 已经实现了子类
            com.demo.adapter.interfaceadapter.Power5VAdapter power5VAdapter = new Power5VAdapter(new AC220());
            power5VAdapter.output5V();

            // 直接实现子类
            com.demo.adapter.interfaceadapter.PowerAdapter powerAdapter = new PowerAdapter(new AC220()) {
                @Override
                public int output5V() {
                    int output = 0;
                    if (mAC220 != null) {
                        output = mAC220.output220V() / 44;
                    }
                    return output;
                }
            };
            powerAdapter.output5V();
        }

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21

    这样也实现了这个适配功能,而且可以说易于扩展。
    总结

    可以说Source的存在形式决定了适配器的名字,类适配器就是继承Source类,对象适配器就是持有Source类,接口适配器就是实现Source接口。
    后记

    android开发中,ListView、RecyclerView等列表展示组件填充数据都会用到Adapter,这里面的Source相当于Datas,Destination相当于Views,然后Adapter去协调数据与显示。使用适配器模式会拥有更高的复用性以及更好的扩展性,但是过多的使用适配器模式,代码的可阅读性会有所下降,但是设计模式应该是对于相对应的情况,而不是盲目的使用。
    ---------------------
    作者:Must_Do_Kaihong
    来源:CSDN
    原文:https://blog.csdn.net/u012359453/article/details/79165080
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    移动端链接、点击事件、输入框去除背景高亮
    Quartz.Net与MVC结合定时任务
    Win10上使用SVN遇到的一些问题
    Win7上的ASP.NET MVC3项目在Win10上运行的一个坑
    《SQL必知必会》学习笔记(二)
    《SQL必知必会》学习笔记(一)
    数据库知识总结(表结构操作)
    搭建三层架构(ASP.NET MVC+EF)
    python线程中的全局变量与局部变量
    ADO.NET Entity Framework学习笔录(一)
  • 原文地址:https://www.cnblogs.com/tiancai/p/9908570.html
Copyright © 2011-2022 走看看