美文网首页
适配器模式

适配器模式

作者: Lemon_Home | 来源:发表于2018-03-09 14:51 被阅读22次

1. 概念

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

2. 类适配器

2.1 定义

类的适配器模式把适配的类的API转换成目标类的API

image.png

Adapter继承自Adaptee

2.2 实例

public class Adaptee {
    public void sampleOperation1() {
        System.out.println("adaptee sampleOperation1");
    }
}
public interface Target {
    void sampleOperation1();
    void sampleOperation2();
}
public class Adapter extends Adaptee implements Target {
    @Override
    public void sampleOperation2() {
        System.out.println("adapter sampleOperation2");
    }
}
public class Test {
    public static void main(String[] args) {
        Adapter adapter = new Adapter();
        adapter.sampleOperation1();
        adapter.sampleOperation2();
    }
}

2.3 总结

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

3. 对象适配器

3.1 定义

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


image.png

3.2 实例

public class Adaptee {
    public void sampleOperation1() {
        System.out.println("adaptee sampleOperation1");
    }
}
public interface Target {
    void sampleOperation1();
    void sampleOperation2();
}
public class Adapter implements AdapterClass.Target {

    private Adaptee adaptee;

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

    @Override
    public void sampleOperation1() {
        adaptee.sampleOperation1();
    }

    @Override
    public void sampleOperation2() {
        System.out.println("Adapter sampleOperation2");
    }
}
public class Test {
    public static void main(String[] args) {
        Adapter adapter = new Adapter(new Adaptee());
        adapter.sampleOperation1();
        adapter.sampleOperation2();
    }
}

3.3 总结

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

4. 在android中应用

listview
listview需要将各种各样的数据显示成列表样式,所以就需要在显示之前通过适配器将数据适配成统一的样式。封装数据,统一ItemView。


image.png

ListView的布局是由一条一条的Item组成的,这每一个Item又是一个View。通过Adapter适配器这个桥梁将View添加到ListView中。
一个Adapter是AdapterView视图与数据之间的桥梁,Adapter提供对数据的访问,也负责为每一项数据产生一个对应的view。
每一个数据产生对应的view后,然后将view添加到ListView之中。
符合MVC设计,Adapter属于Controller层。

相关文章

  • Java设计模式(二)

    talk is cheap show me the code 适配器模式 类适配器模式 接口适配器模式 对象适配器...

  • 适配器模式

    目录 1、什么是适配器模式? 2、适配器模式结构? 3、如何实现适配器模式? 4、适配器模式的特点? 5、适配器模...

  • 设计模式之适配器模式

    适配器模式: 类适配器模式、对象适配器模式、接口适配器模式 1.类适配器模式:新的接口出现了,但是和老的接口不兼容...

  • 学习iOS设计模式第一章 适配器(Adapter)

    今天学习了iOS设计模式中的适配器模式,适配器有两种模式对象适配器模式-- 在这种适配器模式中,适配器容纳一个它包...

  • 第4章 结构型模式-适配器模式

    一、适配器模式简介 二、适配器模式的优点 三、适配器模式的实例

  • 设计模式(Design Patterns)适配器模式(Adapt

    适配器模式主要分为三类:类的适配器模式、对象的适配器模式、接口的适配器模式。 类的适配器模式 场景:将一个类转换成...

  • 适配器模式

    适配器模式主要分为三类:类的适配器模式、对象的适配器模式、接口的适配器模式。适配器模式将某个类的接口转换成客户端期...

  • 适配器模式

    先直观感受下什么叫适配器 适配器模式有类的适配器模式和对象的适配器模式两种不同的形式。 类适配器模式 对象适配器模...

  • 适配器模式

    适配器模式 一、适配器模式定义 适配器模式的定义是,Convert the interface of a clas...

  • 设计模式:结构型

    享元模式 (Pools,Message) 代理模式 适配器模式 :类适配器和对象适配器 装饰者模式 外观模式 桥接...

网友评论

      本文标题:适配器模式

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