资讯专栏INFORMATION COLUMN

JS 设计模式 十(适配器模式)

mochixuan / 551人阅读

摘要:适配器模式是指将一个接口转换成客户端希望的另外一个接口,该模式使得原本不兼容的类可以一起工作。适配器有种角色目标抽象角色定义客户所期待的使用接口。接口源角色需要被适配的接口。接口适配器角色把源接口转换成符合要求的目标接口的设备。

适配器模式

是指将一个接口转换成客户端希望的另外一个接口,该模式使得原本不兼容的类可以一起工作。
举个例子:macbook pro有一个HDMI接口,一条HDMI接口的数据线,现在要外接显示器,而显示器只有VGI接口,我们需要一个HDMI-VGI转换器,这个转换器其实起到的作用就是适配器,让两个不兼容的接口可以一起工作。

适配器有4种角色:

1.目标抽象角色(Target):定义客户所期待的使用接口。(VGI接口)
2.源角色(Adaptee):需要被适配的接口。(HDMI接口)
3.适配器角色(Adapter):把源接口转换成符合要求的目标接口的设备。(HDMI-VGI转换器)
4.客户端(client):例子中指的VGI接口显示器。

实例 假设有两种充电接口MicroUSB和USBTypec
function ChargingCord(name) {
  var _name = name || "默认:无接口"
  this.work = function () {
    console.log("使用" + _name + "接口");
  }
  this.getName = function () {
    return _name;
  }
  this.check = function (target) {
    return _name == target.getName();
  }
}

function MicroUSB() {
  this.__proto__ = new ChargingCord("MicroUSB");
}

function USBTypec() {
  this.__proto__ = new ChargingCord("USBTypec");
}
有两种车分别有不同的充电接口
function Car(name, chargingCord) {
  var _name = name || "默认:车"
  var _chargingCord = chargingCord || new ChargingCord();
  this.getName = function () {
    return _name;
  };
  this.charge = function (target) {
    if (_chargingCord.check(target.getChargingCord())) {
      console.log(this.getName());
      _chargingCord.work();
      console.log("充电");
      target.charging();
    }
    else {
      console.log(this.getName()+"的"+_chargingCord.getName());
      console.log(target.getName()+"的"+target.getChargingCord().getName());
      console.log("接口不对无法充电");
    }
  }
}    
function Porsche911() {
  this.__proto__ = new Car("Porsche911", new USBTypec());
}    
function Porsche781() {
  this.__proto__ = new Car("Porsche781", new MicroUSB());
}
有两种手机有不同的接受充电的接口
function Phone(name, chargingCord) {
  var _name = name || "默认:手机"
  var _chargingCord = chargingCord || new ChargingCord();
  this.getChargingCord = function () {
    return _chargingCord;
  };
  this.getName = function () {
    return _name;
  };
  this.charging = function () {
    console.log(_name);
    _chargingCord.work();
    console.log("接收");
  }
}    
function IPhone() {
  this.__proto__ = new Phone("IPhone", new USBTypec());
}    
function MIPhone() {
  this.__proto__ = new Phone("MIPhone", new MicroUSB());
}
我们分别用辆车个两种手机充电
var porsche911 = new Porsche911();
var porsche781 = new Porsche781();    
var iPhone = new IPhone();
var miPhone = new MIPhone();    
console.log("-----------------------------------------");
porsche911.charge(iPhone);
console.log("-----------------------------------------");
porsche781.charge(miPhone);
console.log("-----------------------------------------");
porsche781.charge(iPhone);
console.log("-----------------------------------------");
结果
-----------------------------------------
Porsche911
使用USBTypec接口
充电
IPhone
使用USBTypec接口
接收
-----------------------------------------
Porsche781
使用MicroUSB接口
充电
MIPhone
使用MicroUSB接口
接收
-----------------------------------------
Porsche781的MicroUSB
IPhone的USBTypec
接口不对无法充电
-----------------------------------------
Porsche911的USBTypec
MIPhone的MicroUSB
接口不对无法充电
-----------------------------------------
所以我们要创建适配器函数
function PhoneUSBTypecToMicroUSB(Phone) {
  var _USBTypec = new ChargingCord("USBTypec");
  var _MicroUSB = new ChargingCord("MicroUSB");
  if (_USBTypec.check(Phone.getChargingCord())) {
    Phone.charging = function () {
      console.log(this.getName());
      _USBTypec.work();
      console.log("转接");
      _MicroUSB.work();
      console.log("接收");
    }
    Phone.getChargingCord = function () {
      return _MicroUSB;
    };
    return Phone;
  }
  else {
    console.log("接口不对无法转换");
  }
}

function PhoneMicroUSBToUSBTypec(Phone) {
  var _USBTypec = new ChargingCord("USBTypec");
  var _MicroUSB = new ChargingCord("MicroUSB");
  if (_MicroUSB.check(Phone.getChargingCord())) {
    Phone.charging = function () {
      console.log(this.getName());
      _MicroUSB.work();
      console.log("转接");
      _USBTypec.work();
      console.log("接收");
    }
    Phone.getChargingCord = function () {
      return _USBTypec;
    };
    return Phone;
  }
  else {
    console.log("接口不对无法转换");
  }
}

function PhoneDeleteInterface(Phone){
  delete Phone.charging;
  delete Phone.getChargingCord;
  return Phone;
}
再来测试接口转换和充电情况
PhoneMicroUSBToUSBTypec(iPhone);
console.log("-----------------------------------------");
PhoneUSBTypecToMicroUSB(miPhone);
console.log("-----------------------------------------");
porsche781.charge(PhoneUSBTypecToMicroUSB(iPhone));
console.log("-----------------------------------------");
porsche911.charge(PhoneMicroUSBToUSBTypec(miPhone));
console.log("-----------------------------------------");
porsche781.charge(PhoneDeleteInterface(iPhone));
console.log("-----------------------------------------");
porsche911.charge(PhoneDeleteInterface(miPhone));
适配后结果
接口不对无法转换
-----------------------------------------
接口不对无法转换
-----------------------------------------
Porsche781
使用MicroUSB接口
充电
IPhone
使用USBTypec接口
转接
使用MicroUSB接口
接收
-----------------------------------------
Porsche911
使用USBTypec接口
充电
MIPhone
使用MicroUSB接口
转接
使用USBTypec接口
接收
-----------------------------------------
Porsche781的MicroUSB
IPhone的USBTypec
接口不对无法充电
-----------------------------------------
Porsche911的USBTypec
MIPhone的MicroUSB
接口不对无法充电
适配器模式优点

1.可以让任何两个没有关联的类一起运行。
2.提高了类的复用。
3.增加了类的透明度。
4.灵活性好。

适用场景

1.系统需要使用现有的类,而此类的接口不符合系统的需要。
2.想要建立一个可以重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作,这些源类不一定有一致的接口。
3.通过接口转换,将一个类插入另一个类系中。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/79502.html

相关文章

  • PHP设计模式():配器模式

    摘要:原文地址设计模式十适配器模式在设计模式七设计模式分类中我们提到过结构设计模式,结构设计模式专注于设计对象和实例的构建组合过程。适配器模式在不修改现有代码的基础上,保留了架构。 原文地址:PHP设计模式(十):适配器模式 Introduction 在PHP设计模式(七):设计模式分类中我们提到过结构设计模式(Structural patterns),结构设计模式专注于设计对象(Objec...

    paney129 评论0 收藏0
  • php设计模式

    摘要:我们今天也来做一个万能遥控器设计模式适配器模式将一个类的接口转换成客户希望的另外一个接口。今天要介绍的仍然是创建型设计模式的一种建造者模式。设计模式的理论知识固然重要,但 计算机程序的思维逻辑 (54) - 剖析 Collections - 设计模式 上节我们提到,类 Collections 中大概有两类功能,第一类是对容器接口对象进行操作,第二类是返回一个容器接口对象,上节我们介绍了...

    Dionysus_go 评论0 收藏0
  • php设计模式

    摘要:我们今天也来做一个万能遥控器设计模式适配器模式将一个类的接口转换成客户希望的另外一个接口。今天要介绍的仍然是创建型设计模式的一种建造者模式。设计模式的理论知识固然重要,但 计算机程序的思维逻辑 (54) - 剖析 Collections - 设计模式 上节我们提到,类 Collections 中大概有两类功能,第一类是对容器接口对象进行操作,第二类是返回一个容器接口对象,上节我们介绍了...

    vspiders 评论0 收藏0
  • 前端面试题小集

    摘要:一一个页面上两个左右铺满整个浏览器,要保证左边的一直为,右边的跟随浏览器大小变化比如浏览器为,右边为,浏览器为,右边为,请写出大概的代码。如果需要使用,最好是通过动态给添加属性值,这样可以绕开以上两个问题。 一、一个页面上两个div左右铺满整个浏览器,要保证左边的div一直为100px,右边的div跟随浏览器大小变化(比如浏览器为500,右边div为400,浏览器为900,右边div为...

    bawn 评论0 收藏0
  • PHP基础

    摘要:分别为适配器模式,装饰器模式,代理模式,外观模式,桥接模式,组合模式,享元模式。设计模式五适配器模式适配器模式将某个对象的接生成器和协程的实现在这篇文章中,作者针对那些比较难以理解的概念,以一个更为通俗的方式去讲明白。。 PHP 源码注解 PHP 的详细源码注解 PHP 字符串操作整理 一些有关字符串的常用操作。 Redis 常见七种使用场景 (PHP 实战) 这篇文章主要介绍利用 R...

    HtmlCssJs 评论0 收藏0

发表评论

0条评论

mochixuan

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<