Adapter模式,就是适配器模式,使两个原本没有关联的类结合一起使用。

平时我们会经常碰到这样的情况,有了两个现成的类,它们之间没有什么联系,但是我们现在既想用其中一个类的方法,
同时也想用另外一个类的方法。有一个解决方法是,修改它们各自的接口,但是这是我们最不愿意看到的。这个时候Adapter模式就会派上用场了。
Adapter模式有两种方式,一种是对象适配器,一种是类适配器。

1.对象适配器
假如有两个类,一个是DrawCircle,另一个是DrawRectangle。
public class DrawCircle {

public void draw(String msg){
  System.out.println("Draw Circle: " + msg);
 }
}

public class DrawRectangle {
 public void draw(String msg){
  System.out.println("Draw Rectangle: " + msg);
 }

}

现在我们有个应用想既可以画圆,又可以画方型,那么我们就需要把这两个类联合起来使用,但是又不想修改各自的接口,
这时就需要Adapter来实现这个应用了。
public class DrawAdapter extends DrawCircle{
 private DrawRectangle drawRectangle;
 public DrawAdapter(DrawRectangle drawRectangle){
  this.drawRectangle = drawRectangle;
 }
 
 public void draw(String msg){
  drawRectangle.draw(msg);
 }
}

这个示例中,DrawAdapter是适配器,DrawRectangle属于Adaptee,是被适配者,适配器将被适配者和适配目标(DrawCircle)进行适配。
具体调用:
DrawCircle drawCircle = new DrawCircle();
drawCircle.draw("DrawCircle"); //display "Draw Circle: DrawCircle"

drawCircle = new DrawAdapter(new DrawRectangle());
drawCircle.draw("DrawRectangle"); //display "Draw DrawRectangle: DrawRectangle"

2.类适配器
上例DrawAdapter继承了DrawCircle,也可以继承DrawRectangle,可是java不支持多重继承,所以其中有个类要实现接口。
还是以上边例子为例:
public interface IDrawCircle {
 void draw(String msg);
}

public class DrawCircle implements IDrawCircle {

public void draw(String msg){
  System.out.println("Draw Circle: " + msg);
 }
}

public class DrawAdapter extends DrawRectangle implements IDrawCircle {
 
 private DrawCircle drawCircle;

public DrawAdapter(){
 } 
 public DrawAdapter(DrawCircle drawCircle){
  this.drawCircle = drawCircle;
 }
 
 public void insert(String msg){

  if (null == drawCircle)
    super.draw(msg);
  else
    drawCircle.draw(msg);

}

}

使用:
DrawRectangle drawRectangle = new DrawRectangle();
drawRectangle.draw("rectangle");
  
drawRectangle = new DrawAdapter(new DrawCircle());
drawRectangle.draw("circle");

drawRectangle = new DrawAdapter();
drawRectangle.draw("rectangle");


第二个例子运行结果:

Draw Rectangle: rectangle
Draw Circle: circle
Draw Rectangle: rectangle

设计模式学习笔记-Adapter模式的更多相关文章

  1. Java-马士兵设计模式学习笔记-命令模式

    一.概述 命令模式 二.代码 1.Client.java public class Client { public void request(Server server){ server.addCom ...

  2. Java-马士兵设计模式学习笔记-桥接模式

    一.概述 1.桥接模式的应用情况:(1)两个维度扩展(2)排列组合 二.代码 1.Gift.java public class Gift { protected GiftImpl giftImpl; ...

  3. Java-马士兵设计模式学习笔记-工厂模式-抽象工厂模式

    一.概述 1.抽象工厂:当情况是需要产生一系列产品,若需更换产品,则要求一系列产品一起换,且要控制一系列产品的产生过程,此时可考虑抽象工厂模式.例:小明装修屋子,把电视.冰箱都替换掉,他这次需要把电视 ...

  4. Java-马士兵设计模式学习笔记-工厂模式-简单工厂

    一.概述 1.目标:要控制任意类型交通工具的生产模式 2.目标有两层意思(1)任意类型 (2)生产模式,所以对应的,要这两个层面上抽象(Movable,VehicleFactory),利用接口,实现多 ...

  5. Java-马士兵设计模式学习笔记-策略模式-模拟 Comparator接口

    续上一篇  <Java 模拟 Comparable接口> 一.Teacher类及Student类的比较大小方式是不固定的,比如老师除了比较职称外,还可比较工龄大小,年龄大小等.则定义Com ...

  6. Java-马士兵设计模式学习笔记-迭代器模式-模仿Collectin ArrayList LinckedList

    Java Iterator模式 Java Iterator模式, 模仿Collectin ArrayList LinckedList 一.有如下几个类 1.接口Collection.java 2.接口 ...

  7. 设计模式学习笔记——Adapter 适配器模式

    适配器设计模式的适应场景: 一般情况是上端固定,下端固定,下端功能不满足或跟上端不协调,使用适配器重新包一层(继承适配器接口,以满足上端需求,继承下层类,以调用方法),使下端代码能满足上端需求(欺骗, ...

  8. Java-马士兵设计模式学习笔记-代理模式-聚合与继承方式比较

    一.概述 1.目标:要在Tank的move()方法做时间代理及日志代理(可以设想以后还要增加很多代理处理),且代理间的顺序可活更换 2.思路: (1)聚合:代理类聚合了被代理类,且代理类及被代理类都实 ...

  9. Java-马士兵设计模式学习笔记-代理模式-动态代理 调用Proxy.newProxyInstance()

    一.概述 1.目标:不自己写代理类,利用Proxy.newProxyInstance()动态生成 2.用到的知识点: (1)//编译源码,生成class,注意编译环境要换成jdk才有compiler, ...

  10. Java-马士兵设计模式学习笔记-建造者模式

    一.概述 二.代码 1.Animal.java public interface Animal { public void bark(); } 2.Dog.java public class Dog ...

随机推荐

  1. C# 将数字时间转化为特定格式字符串

    在工作中,经常遇到,将距离某点的时间段转化为"HH:MM:SS"格式时间的情况. 经过总结,用C#实现了一个特别好的办法: DateTime  _dTNow = DateTime. ...

  2. three.js初涉略(一)

    <!-- 最近要研究一下webgl,发现了webgl中文网(http://www.hewebgl.com/article/articledir/1).边研究教程边做下记录 --> thre ...

  3. js阻止form表单重复提交

    防止表单重复提交的方法总体来说有两种,一种是在js中阻止重复提交:另一种是在后台利用token令牌实现,大致思路是生成一个随机码放到session和form表单的隐藏输入框中,提交表单时两者对比,表单 ...

  4. funsioncharts的图表操作heatmap

    网址:http://www.fusioncharts.com/dev/chart-guide/heat-map-chart/introduction.html 以下只是假数据,目前还没有实现动态数据获 ...

  5. NetSuite SuiteScript 2.0 export data to Excel file(xls)

    In NetSuite SuiteScript, We usually do/implement export data to CSV, that's straight forward: Collec ...

  6. 【前端】js代码模拟用户键盘鼠标输入

    js代码模拟用户键盘鼠标输入 原生js var event = new Event('mousewheel'); event.wheelDelta = 360 document.dispatchEve ...

  7. javascript回文和类名的检测方法

    回文**           //"123"变为"123321" //"abc321"变为"abcd321123cba" ...

  8. python 最大公约数

    求解两个整数(不能是负数)的最大公约数(要求两数不能同时为0)当两数都是0时,最大公约数为0方式一:穷举法 def GCU(m, n): if not m: return n elif not n: ...

  9. 微信小程序-视图模板

    定义模板 使用name属性,作为模板的名字.然后在<template/>内定义代码片段,如: <!-- index: int msg: string time: string --& ...

  10. 【干货】微信场景之H5页面制作免费工具大集合

    营销代有手段出,各领风骚数百天.要说现在哪些营销方式最能传播,屡屡刷爆朋友圈的H5页面肯定就是首当其冲的,提到H5页面,就立马想到"围住神经猫",上线微信朋友圈3天的时间便创造了用 ...