1、核心意图:
将一个类的接口转换成客户希望的另外一个接口,从而使得原本由于接口不兼容而不能一起工作的类可以一起工作。
该模式的目标是通过一个代理(这里是Adapter),在原来的类(Adaptee)和客户(Client)之间进行协调,从而达到兼容的目的。其核心是解决一致性的问题。
2、身边实例:
在我们实际生活中也很容易看到这方面的例子,比如我们要和一个外国人打交道,例如韩国 人,如果我们没有学习过韩语,这个韩国人也没有学习过我们汉语,在这种情况下,我们之间是很难进行直接交流沟通。为了达到沟通的目的有两个方法:1)改造 这个韩国人,使其能够用汉语进行沟通;2)请一个翻译,在我们和这个韩国人之间进行语言的协调。显然第一种方式——改造这个韩国人的代价要高一些,我们不 仅要重新培训他汉语的学习,还有时间、态度等等因素。而第二个方式——请一个翻译,就很好实现,而且成本低,还比较灵活,当我们想换个日本人,再换个翻译 就可以了。
3、动机简述:
在该模式的动机中,描述了一个绘图编辑器的实现,该编辑器可以对基本图元Shape(直 线、多边形、正文等)进行绘制和排列来生成图片和图表,对于这些图元类的实现,直线/多边形还比较容易实现,但是正文的实现却很麻烦,为了减少开发成本和 保证质量,通过采用Adapter模式定义适配器类TextShape,来重用图形工具箱中已经存在的正文编辑器TextView。
4、Java实现分析:
在GOF设计模式中,Adapter可以分为类模式和对象模式两种,类模式通过多重继承实现,对象模式通过委托实现。
在Java中由于没有多重继承机制,所以要想实现类模式的Adapter,就要进行相应 的改变:通过继承Adaptee类实现Target接口方式实现。这种改变存在两个问题:1)Target必须是一个接口而不能是一个类,否则Adapter无法implements实现;2)Adapter是继承Adaptee的实现,而不是私有继承,这就表示Adapter是一个Adaptee的子类。
类Adapter模式和对象Adapter模式的Java代码可参考本文下方代码部分。
5、类模式/对象模式分析:
由于存在两种Adapter实现方式(即使在Java中),那么在实际中我们采用哪一种要好呢?通过分析发现这两种模式有两个主要特性区别,并且还是互补的:
A、表现在Adapter对Adaptee的特殊性要求:
类模式由于Adapter是Adaptee的子类,所以Adapter很方便重新定义Adaptee中的个别方法,以达到自己的特性需要。
对象模式由于Adapter不是Adaptee的子类,所以如果Adapter对Adaptee中的个别方法有特殊的需要,就要新建Adaptee的子类,而让Adapter使用这个子类。
B、表现在Adaptee的类层次扩展上:
类模式由于Adapter是Adaptee的子类,所以编译后就不能再更换所实现的父类Adaptee,因此如果有一个Adaptee的类层次结构,就要相应的有一个Adapter的类层次结构,且新扩展Adaptee时很不方便。
对象模式由于Adapter不是Adaptee的子类,而是通过使用的方式,所以在系统运行时仍然可以更换Adapter所使用的Adaptee,只要他们具有相同的类型。所以在新扩展Adaptee时很方便。
6、Java代码示例—对象模式实现:
类Point,表示画面坐标中的点
package qinysong.pattern.adapter;

public class Point {
  private int coordinateX;
  private int coordinateY;

  public Point(int coordinateX, int coordinateY){
    this.coordinateX = coordinateX;
    this.coordinateY = coordinateY;
  }
  public String toString(){
    return "Point[x=" + coordinateX + ",y=" + coordinateY + "]";
  }
  public int getCoordinateX() {
    return coordinateX;
  }
  public int getCoordinateY() {
    return coordinateY;
  }
}
类Shape,表示图元借口,对应Adapter模式中的Target
package qinysong.pattern.adapter;

public interface Shape {
  public Point getBottomLeftPoint();
  public Point getTopRightPoint();
}
类TextView,工具箱中的文本组件类,已经存在的类,对应Adapter模式中的Adaptee
package qinysong.pattern.adapter;

public class TextView {

  public int getCoordinateX() {
    System.out.println("TextView.getCoordinateX()...");
    return 10;
  }
  public int getCoordinateY() {
    System.out.println("TextView.getCoordinateY()...");
    return 20;
  }
  public int getHeight() {
    System.out.println("TextView.getHeight()...");
    return 30;
  }
  public int getWidth() {
    System.out.println("TextView.getWidth()...");
    return 40;
  }
  public boolean isEmpty(){
    return false;
  }
}
类TextShape,对象模式实现的Adapter
package qinysong.pattern.adapter;

public class TextShape implements Shape {
  private TextView textView;

  public TextShape(TextView textView){
    this.textView = textView;
  }

  //通过TextView的实例进行协调实现
  public Point getBottomLeftPoint() {
    System.out.println("TextShape.getBottomLeftPoint()...");
    int coordinateX = textView.getCoordinateX();
    int coordinateY = textView.getCoordinateY();
    return new Point(coordinateX, coordinateY);
  }

  //通过TextView的实例进行协调实现
  public Point getTopRightPoint() {
    System.out.println("TextShape.getTopRightPoint()...");
    int coordinateX = textView.getCoordinateX();
    int coordinateY = textView.getCoordinateY();
    int height = textView.getHeight();
    int width = textView.getWidth();
    return new Point(coordinateX + width, coordinateY + height);
  }
}
类Client,Adapter模式的客户
package qinysong.pattern.adapter;

public class Client {
  public static void main(String[] args){
    System.out.println("Client.main begin ..........");
    System.out.println("Client.main 以下是通过实例委托方式实现的Adapter");
    Shape shape = new TextShape(new TextView());
    Point bottomLeft = shape.getBottomLeftPoint();
    Point topRight = shape.getTopRightPoint();
    System.out.println("Client.main shape's bottomLeft:" + bottomLeft);
    System.out.println("Client.main shape's topRight:" + topRight);

    System.out.println(" Client.main 以下是通过类继承方式实现的Adapter");
    Shape shape2 = new TextShape2();
    bottomLeft = shape2.getBottomLeftPoint();
    topRight = shape2.getTopRightPoint();
    System.out.println("Client.main shape2's bottomLeft:" + bottomLeft);
    System.out.println("Client.main shape2's topRight:" + topRight);
    System.out.println("Client.main end   ..........");
  }
}
7、Java代码示例—类模式实现:
和以上对象模式实现中的示例目的相同,类Point、Shape、TextView相同,略。以下是类TextShape2的示例代码,实现类模式的Adapter
package qinysong.pattern.adapter;

public class TextShape2 extends TextView implements Shape {

  //通过所继承的TextView,进行协调实现
  public Point getBottomLeftPoint() {
    System.out.println("TextShape2.getBottomLeftPoint()...");
    int coordinateX = getCoordinateX();
    int coordinateY = getCoordinateY();
    return new Point(coordinateX, coordinateY);
  }

  //通过所继承的TextView,进行协调实现
  public Point getTopRightPoint() {
    System.out.println("TextShape2.getTopRightPoint()...");
    int coordinateX = getCoordinateX();
    int coordinateY = getCoordinateY();
    int height = getHeight();
    int width = getWidth();
    return new Point(coordinateX + width, coordinateY + height);
  }

  //注意: 这一点体现了类模式的优势,可以很方便地重定义父类TextView中的方法
  public int getCoordinateX() {
    System.out.println("TextShape2.getCoordinateX()...");
    return 100;
  }
}

学习:java设计模式—Adapter模式相关推荐

  1. 设计模式学习笔记——适配器(Adapter)模式

    设计模式学习笔记--适配器(Adapter)模式 @(设计模式)[设计模式, 适配器模式, adapter, 适配器] 设计模式学习笔记适配器Adapter模式 基本介绍 适配器案例 类适配器模式 类 ...

  2. Java 设计模式 Adapter 对象适配器 模式

    Java 设计模式 Adapter 对象适配器 模式 Adapter模式用于现有的程序无法直接使用,需要做适当的变换之后才能使用的情况. 涉及到的角色 Target对象:定义所需的方法. Client ...

  3. Java 设计模式——状态模式

    概述 很多人在说状态模式的时候总拿策略模式来进行对比,可能他们的类图会有一点类似,可我却不认为他们有多么相像.你可以阅读<Java设计模式--策略模式>这篇博客,并与本文对比,以找到蛛丝马 ...

  4. java设计模式代理模式_Java中的代理设计模式

    java设计模式代理模式 代理对象或代理对象为另一个对象提供占位符,以控制对该对象的访问. 代理充当原始对象的轻量级版本或简化版本. 它支持与原始对象相同的操作,但可以将那些请求委托给原始对象以实现它 ...

  5. java设计模式工厂模式_Java中的工厂设计模式

    java设计模式工厂模式 Welcome to the Factory Design Pattern in Java tutorial. Factory Pattern is one of the C ...

  6. java设计模式工厂模式_Java中的复合设计模式

    java设计模式工厂模式 Composite pattern is one of the Structural design pattern. Composite design pattern is ...

  7. java设计模式工厂模式_Java中的桥梁设计模式

    java设计模式工厂模式 Today we will look into Bridge Design Pattern in java. When we have interface hierarchi ...

  8. java设计模式工厂模式_Java中的外观设计模式

    java设计模式工厂模式 Facade Design Pattern is one of the Structural design patterns (such as Adapter pattern ...

  9. Java设计模式 -11- 外观模式(Facade模式)

    Java设计模式 -11- 外观模式(Facade模式) 前言 外观模式的定义与特点 优点: 缺点: 外观模式的结构与实现 1. 模式的结构 2. 模式的实现 外观模式的应用实例 外观模式的应用场景 ...

  10. 设计模式-Adapter模式

    目录 相关概念 对象适配器 类适配器 一个例子 总结 适配器模式(Adapter Pattern)是结构型模式.主要用来解决接口不兼容的问题,将一个类的接口变换成客户端所期待的另一种接口,从而使原本因 ...

最新文章

  1. 【NLP新闻-2013.06.03】New Book Where Humans Meet Machines
  2. Redis对象类型与编码
  3. laydate 时间控件去掉秒以及解决在移动端不能滑动的问题
  4. IncrediBuild 加速原理
  5. 在git 服务器挂载、创建新的项目、克隆新的项目
  6. 第003讲 无序列表 有序列表 框架
  7. db2数据库基础知识
  8. 华为NP课程笔记8-BGP2
  9. 引用百度新闻热门搜索html,百度新闻搜索技巧(之一)
  10. 一致 先验分布 后验分布_先验概率、似然函数与后验概率
  11. unity webgl优化
  12. 2+22+222+2222
  13. ACE1.0动态搜索框OpenHarmony组件开发大赛参与组件-Search_DialogJS
  14. 程序员面试的奇葩问题
  15. 向日葵远程操控的实现
  16. Telnet,DHCP,静态路由
  17. 数据结构——马踏棋盘题解(贪心算法)(C语言)
  18. Elastic Job 入门详解
  19. 采用业界最小的运算放大器设计麦克风电路
  20. 计算机第五章word试题及答案,职称计算机word练习试题及答案(4)

热门文章

  1. CMS:内容管理系统
  2. 设计网站中的精品,你可能需要它--第二期
  3. [转载]计算机视觉专业名词中英文对照
  4. NLP-2015:Subword NMT模型【使用子词来解决OOV问题】
  5. 基于树莓派的Data Matrix decode
  6. Unix/Linux/BSD命令大全|实用指南
  7. 好用的跨平台开源截图工具推荐--flameshot
  8. 单片机c语言论文,基于51单片机的C语言程序设计论文.doc
  9. 手机变身高清摄像头 DroidCamX完全使用教程
  10. 《吴军-信息论40讲》摘录