设计模式之适配器模式--- Pattern Adapter

来源:互联网 发布:linux vi编程 编辑:程序博客网 时间:2024/04/30 18:18

模式的定义

适配器模式(Adapter Pattern)定义如下:

Convert the interface of a class into another interface clients expect.Adapter lets classes work together that couldn’t otherwise because of incompatible interface.

将一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。

类型

结构类

模式的使用场景

如果你有动机修改一个已经投产中的接口时,适配器模式就可能是最适合你的模式

优点

  • 适配器模式可以让两个没有任何关系的类在一起运行,只要一个适配器就能够搞定

  • 增加类的透明性

    我们访问的Target目标角色,但是具体的实现都委托给了源角色,而这些对高层次模块是

透明的

  • 提高了类的复用度
  • 灵活性非常好

    如果不想用适配器,删除这个适配器就可以了,其他的代码都不要动,基本上就类似一个灵活的构件,想用就用,不想就删除。

类适配器

类适配器是通过继承进行的适配

类适配器UML类图

这里写图片描述

类适配器角色介绍

  • Target目标角色:

    该角色定义把其他类转换为何种接口,也就是我们期望的接口

  • Adaptee源角色

    你想把谁转换为目标角色,这个谁就是源角色,它是已经存在的,运行良好的类或对象,经过适配角色的包装,它会成为一个新的角色

  • Adapter适配器角色

    适配器模式的核心角色,其他两个角色都存在,而适配器角色是需要新建立的,它的职责:把源角色转换为目标角色,怎么转换?通过继承或是关联的方式。

类适配器模式的通用源码

Target 类:

public interface Target {    public void request();}

ConcreteTarget 类:

public class ConcreteTarget implements Target {    @Override    public void request() {        // TODO Auto-generated method stub        System.out.println("ConcreteTarget-----request()");    }}

Adaptee :

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

Adapter :

public class Adapter extends Adaptee implements Target {    @Override    public void request() {        // TODO Auto-generated method stub        System.out.println("Adapter-----request()");        super.doSomeThing();    }}

Client :

public class Client {    public static void main(String[] args) {        // TODO Auto-generated method stub        Target target = new ConcreteTarget();        target.request();        System.out.println("--------------------------");        Target target2 = new Adapter();        target2.request();    }}

类适配器输出结果

ConcreteTarget-----request()--------------------------Adapter-----request()Adaptee-----doSomeThing()

对象适配器

对象适配器是通过对对象关联的方法进行的适配

对象适配器uml图

这里写图片描述

对象适配器通用源码

Target 类:

public interface Target {    public void request();}

ConcreteTarget 类:

public class ConcreteTarget implements Target {    @Override    public void request() {        // TODO Auto-generated method stub        System.out.println("ConcreteTarget-----request()");    }}

Adaptee01 类:

public class Adaptee01 {    public void doAct01(){        System.out.println("Adaptee01--------------doAct01()");    }}

Adaptee02 类:

public class Adaptee02 {    public void doAct02(){        System.out.println("Adaptee02--------------doAct02()");    }}

Adaptee03 类:

public class Adaptee03 {    public void doAct03(){        System.out.println("Adaptee03--------------doAct03()");    }}

Adapter 类:

public class Adapter implements Target {    private Adaptee01 adaptee01;    private Adaptee02 adaptee02;    private Adaptee03 adaptee03;    public Adapter(Adaptee01 adaptee01, Adaptee02 adaptee02, Adaptee03 adaptee03) {        super();        this.adaptee01 = adaptee01;        this.adaptee02 = adaptee02;        this.adaptee03 = adaptee03;    }    @Override    public void request() {        // TODO Auto-generated method stub        System.out.println("Adapter-----request()");        adaptee01.doAct01();        adaptee02.doAct02();        adaptee03.doAct03();    }}

Client 类:

public class Client {    public static void main(String[] args) {        // TODO Auto-generated method stub        Adaptee01 adaptee01 = new Adaptee01();        Adaptee02 adaptee02 = new Adaptee02();        Adaptee03 adaptee03 = new Adaptee03();        Adapter adapter = new Adapter(adaptee01, adaptee02, adaptee03);        adapter.request();    }}

对象适配器输出结果

Adapter-----request()Adaptee01--------------doAct01()Adaptee02--------------doAct02()Adaptee03--------------doAct03()

Android源码中的模式实现

杂谈

适配器模式本质就是把现有的类,对象,方法来包装成我们所需要的类和对象。

适配器模式是结构类,而从结构形式来包装现有的类一般有二种方式:

  • 一是继承的方式(is-a),这也就是类适配。
  • 一是关联的方式(have-a),这也就是对象适配。

参考资料

(1).设计模式之禅—第19章 适配器模式
(2)装饰模式
https://github.com/simple-android-framework/android_design_patterns_analysis/tree/master/adapter

0 0