zoukankan      html  css  js  c++  java
  • 《Android源代码设计模式解析与实战》读书笔记(二十)

    第二十章、适配器模式

    适配器模式是结构型设计模式之中的一个,它在我们的开发中使用率极高,比方ListView、GridView以及RecyclerView都须要使用Adapter。

    1.定义

    适配器模式把一个类的接口变换成client所期待的还有一种接口,从而使原本因接口不匹配无法在一起工作的两个类能够在一起工作。

    2.使用场景

    (1)系统须要使用现有的类。但此类的接口不符合系统的须要。即接口不兼容。

    (2)想要建立一个能够反复使用的类。用于与一些彼此之间没有太大关联的一些类,包含一些可能在将来引进的类一起工作。

    (3)须要一个统一的输出接口,而输入端的类型不可预知。

    3.UML类图

    适配器模式分为两种,即类适配器模式与对象适配器模式。下面是类适配器的UML图。

    这里写图片描写叙述

    (1)Target:目标角色。也就是所期待得到的接口。注意:因为这里讨论的是类适配器模式。因此目标不能够是类。

    (2)Adaptee:如今须要适配的接口。

    (3)Adapter:适配器角色,也就是本模式的核心。适配器把源接口转换成目标接口。显然,这一角色不能够是接口,而必须是详细类。

    4.简单实现

    以笔记本电源适配器为例,电源适配器将220V的电压转换到5V。

    那么5V电压就是Target接口,220V电压就是Adaptee类,转换就是Adapter。

    (1)以类适配器模式实现

    //Target角色
    public interface FiveVolt {
    
        public int getVolt5();
    }
    //Adapter角色
    public class Volt220 {
        public int getVolt220(){
            return 220;
        }
    }
    
    public class VoltAdapter extends Volt220 implements FiveVolt{
    
        @Override
        public int getVolt5() {
            return 5;
        }
    
    }
    
    public class Test {
    
        public static void main(String[] args) {
            VoltAdapter adapter = new VoltAdapter();
            System.out.println("输出电压:" + adapter.getVolt5());
        }
    
    }
    

    结果:

    输出电压:5

    (2)对象适配器模式实现

    对象适配器模式与类适配器模式不同的是。对象适配器模式不是使用继承关系连接到Adapter类,而是使用代理关系连接到Adapter类。

    所以FiveVolt 、Volt220 不变。VoltAdapter 改动例如以下:

    public class VoltAdapter1 implements FiveVolt{
    
        Volt220 mVolt220;
    
        public VoltAdapter1(Volt220 adaptee) {
            this.mVolt220 = adaptee;
        }
    
        public int getVolt220(){
            return mVolt220.getVolt220();
        }
    
        @Override
        public int getVolt5() {
            return 5;
        }
    
    }

    Test改动为:

    public class Test {
    
        public static void main(String[] args) {
            VoltAdapter adapter = new VoltAdapter(new Volt220());
            System.out.println("输出电压:" + adapter.getVolt5());
        }
    
    }
    

    这样的实现方式直接将要适配的对象传递到Adapter中,使用组合的形式实现接口兼容的效果。这比类适配器方式更为灵活,同一时候被适配对象的方法不会暴露出来。因此对象适配器模式更加灵活、有用。

    5.Android源代码中的适配器模式

    1.ListView的Adapter

    这里ListView的Adapter就是使用的对象适配器模式,Target就是View,Adapter角色就是将Item View输出为View抽象的角色,Adaptee就是须要被处理的Item View。

    6.总结

    1.长处

    (1)更好的复用性:系统须要使用现有的类。而此类的接口不符合系统的须要。那么通过适配器模式就能够让这些功能得到更好的复用。

    (2)更好的扩展性:在实现适配器功能的时候,能够调用自己开发的功能,从而自然地扩展系统的功能。

    2.缺点

    过多的使用适配器,会让系统非常零乱,不易总体进行把握。比方,明明看到调用的是A接口,事实上内部被适配成了B接口的实现,一个系统假设太多出现这样的情况,无异于一场灾难。因此假设不是非常有必要,能够不使用适配器,而是直接对系统进行重构。

    7.參考

    《JAVA与模式》之适配器模式

  • 相关阅读:
    MySQL: Speed of INSERT Statements
    ImportError: No module named argparse
    How To Use Coordinates To Extract Sequences In Fasta File
    30分钟掌握Dart语言
    Could not find com.android.tools.build:aapt2:3.2.0-alpha14-4748712.
    完美解决 No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    iOS 可变字符串NSMutableString的使用
    NSMutableString和NSString区别,及相互转换方法
    iOS学习-字符串的删除替换
    Android 常用正则表达式
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/7353241.html
Copyright © 2011-2022 走看看