美文网首页
浅谈设计模式——adapter

浅谈设计模式——adapter

作者: 被时光移动的城 | 来源:发表于2017-08-15 11:05 被阅读29次

如需转载请注明出处

一、什么是适配器模式

1、适配器模式定义
将一个接口转换成客户端希望的另一个接口,适配器模式使接口不兼容的那些类可以一起工作,其别名为包装器(Wrapper)

2、类适配器
1)类适配器定义
类的适配器模式把适配的类的API转换成目标类的API
2)UML结构图

类适配器UML结构图.png

3)code
Target接口

public interface Target {
    void sampleOperation1();
    void sampleOperation2();
}

Adaptee类

public class Adaptee {
    public void sampleOperation1(){
        System.out.println("sampleOperation1");
    };
}

Adapter类(继承Adaptee类,实现Taget接口)

public class Adapter extends Adaptee implements Target{

    @Override
    public void sampleOperation2() {
        System.out.println("sampleOperation2");
    }
}

实现

public class MyClass {
    public static void main(String[] args) {
        Adapter adapter = new Adapter();
        adapter.sampleOperation1();
        adapter.sampleOperation2();
    }
}

运行结果:

类适配器运行结果图.png

4)总结
类适配器使用对象继承的方式,是静态的定义方式;
对于适配器,适配器可以重定义Adaptee的部分行为;
对于类适配器,仅仅引入了一个对象,并不需要额外的引用来间接得到Adaptee;
对于类适配器,由于适配器直接继承了Adaptee,使得适配器不能和Adaptee的子类一起工作;

3、对象适配器
1)对象适配器定义
与类的适配器模式一样,对象的适配器模式把被适配的类的API转换成为目标类的API,与类的适配器模式不同的是,对象的适配器模式不是使用继承关系连接到Adaptee类,而是使用委派关系链接到Adaptee类。

2)UML结构图


对象适配器UML结构图.png

3)code
Target接口

public interface Target {
    void sampleOperation1();
    void sampleOperation2();
}

Adaptee类

public class Adaptee {
    public void sampleOperation1() {
        System.out.println("sampleOperation1");
    }
}

Adapter类

public class Adapter implements Target{

    private Adaptee mAdaptee;
    
    //构造方法
    public Adapter(Adaptee mAdaptee) {
        this.mAdaptee = mAdaptee;
    }

    @Override
    public void sampleOperation1() {
        //持有Adaptee的引用,调用sampleOperation1()
        mAdaptee.sampleOperation1();
    }

    @Override
    public void sampleOperation2() {
        System.out.println("sampleOperation2");
    }
}

实现

public class MyClass {
    public static void main(String[] args) {
        Adapter mAdapter = new Adapter(new Adaptee());
        mAdapter.sampleOperation1();
        mAdapter.sampleOperation2();
    }
}

运行结果

对象适配器运行结果图.png

4)总结
对象适配器使用的对象组合的方式,是动态组合的方式(即委托);
对于对象适配器,一个适配器可以把多种不同的源适配到同一个目标;
对于对象适配器,要重定义Adaptee的行为比较困难;
对于对象适配器,需要额外的引用来间接得到Adaptee;

二、Adapter模式在Android中的应用(以listview为例)

1、UI

listview的UI.png

1)Listview的布局是由一条一条的Item组成的,这每一个Item又是一个View。通过Adapter适配器这个桥梁将View添加到ListView中;
2)一个Adapter是AdapterView视图与数据之间的桥梁,Adapter提供对数据的访问,也负责为每一项数据产生一个对应的View;
3)每一项数据产生对应的View之后,然后将View添加到ListView之中;
4)MVC,adapter相当于control;

2、源码
查看ListView的父类AbsListView,成员变量中有关于Adapter的ListAdapter,ListView的主要功能是在它的父类中实现的。

image.png

主要看以下两个方法:
(1)onAttatchToWindow()


onAttatchToWindow().png

这个方法是当我们的view关联到指定的window的时候调用的,同时我们看方法的底部,它调用了mAdapter.getCount()方法,这个方法我们很熟悉了,在自定义Adapter时继承BaseAdapter的时候要复写这个方法,返回数据的数量,通过getcount方法给listview设定一个指定高度。
其次是一个观察者模式,当数据源发生变化时,会通过适配器通知观察者一起做相应的UI改变。

(2)onLayout(boolean change,int l,int t,int r,int b)

onLayout(boolean change,int l,int t,int r,int b).png

调用了layoutChilderen()方法,而这个方法是个空实现,所以肯定是在子类ListView中实现的。

layoutChilderen().png

代码太多就一一不粘贴了。
listview的数据缓存主要使用了观察者模式和适配器模式。
通过适配器各种各样的数据转化成了统一的接口,提供给item,同时又将不同的itemview转化成了统一的view添加到了listview当中,这就是listview中适配器的应用。

相关文章

  • 浅谈设计模式——adapter

    如需转载请注明出处 一、什么是适配器模式 1、适配器模式定义将一个接口转换成客户端希望的另一个接口,适配器模式使接...

  • 11.3设计模式-适配器-详解

    设计模式-适配器adapter模式 adapter模式详解 adapter模式在android中的实际运用1.li...

  • 浅谈设计模式之适配器模式

    适配器模式(Adapter Pattern) 概述: 在设计模式中,适配器模式(adapter pattern)有...

  • 浅谈GoF23设计模式-“Adapter”模式

    “Adapter”模式为结构型设计模式,C#当中主要使用对象适配器。“Adapter”模式定义:将一个类的接口转换...

  • 设计模式-adapter设计模式

    效果图 1.定义 将一种对象适配成另一种对象 2.示例 3.使用 listview的适配器 4.总结 1.适配器模...

  • Adapter模式(设计模式)

    对象适配器模式(使用委托的适配器) 个人理解 用一个比喻来描述比较好,比如:中国现在用电的标准电压是220V交流电...

  • 设计模式-Adapter

    一,Adapter模式详解 适配器模式定义将一个接口转换成客户希望的另一个接口,适配器模式使接口不兼容的那些类一起...

  • Adapter 设计模式

    adapter 模式详解 类的适配器模式 把适配的类的 API转换为目标类的API。 对象适配器对象的适配器模式 ...

  • 简说设计模式之适配器模式

    前言:对于设计模式基础概念可以去看[简说设计模式之设计模式概述] 一、什么是适配器模式 适配器模式(Adapter...

  • Typescript 适配器模式(Adapter)

    标签: 前端 设计模式 适配器模式 typescript Adapter 请仔细阅读下面代码,理解其中的设计理念。...

网友评论

      本文标题:浅谈设计模式——adapter

      本文链接:https://www.haomeiwen.com/subject/mbrmrxtx.html