适配器模式

适配器模式

适配器模式的作用是解决两个软件实体间的不兼容问题。使用适配器模式之后,原本由于接口不兼容而不能工作的两个软件实体可以一起工作。适配器的别名是包装器(wrapper),这是一个相对简单的模式。在程序开发种有许多这样的场景:当试图调用没款或者对象的某个接口时,却发现这个接口的格式并不符和目前的需求。这个时候有两种解决办法,第一种时修改原来的接口实现,但如果原来的模块很复杂,或者拿到的模块是别人编写的经过压缩的代码,修改原接口就显得不太显示了。第二种办法是创建一个适配器,将原接口转换为客户希望的另一个接口,客户只需要和适配器打交道。

适配器有4种角色:

  • 目标抽象角色(Target)
  • 源角色(Adaptee)
  • 适配器角色(Adapter)
  • 客户端(client)

简单的适配器模式

比如之前我们项目中都是用的axios进行ajax请求,现在我们想改成fly请求,项目中请求ajax的地方必然很多,我们替换axios的时候一个一个替换的话会非常麻烦,那怎么办呢?这个时候我们可以增加一个适配器:

    let axios = {
        get(url){
            return fly.get(url)
        },
        post(url,data){
            return fly.post(url,data)
        }
    }
    axios.get('xxx');
    axios.post('xxx',{name:'11'})

我们现在有一个这样的需求:

在以前的电脑上,PS2接口是连接鼠标、键盘等其他外部设备的标准接口。但随着技术的发展,越来越多的电脑开始放弃了PS2接口,转而仅支持USB接口。所以那些过去生产出来的只拥有PS2接口的鼠标、键盘、游戏手柄等,需要一个USB转接口才能继续正常工作,这是PS2-USB适配器诞生的原因

// 计算机
    class Computers{
        constructor(type) {
            this._type = type;
        }
        connect() {
            if(this._type == 'usb'){
                console.log('连接成功')
            }else{
                console.log('连接失败,只能使用usb连接')
            }
        }
        getType() {
            return this._type;
        }
    }
    // ps2接口
    class Ps2 {
        _type = 'ps2'
    }
    // usb接口
    class Usb{
        _type = 'usb'
    }
    let ps2 = new Ps2();
    let computers = new Computers(ps2._type);
    console.log(computers.getType()) // ps2
    computers.connect(); // 连接失败,只能使用usb连接 
    let usb = new Usb();
    let computers1 = new Computers(usb._type);
    console.log(computers1.getType()); // usb
    computers1.connect(); // 连接成功

上面代码中,我们定义了两个源角色Ps2Usb,还有一个目标抽象角色Computers,但是我们的计算机只支持usb接口,是无法连接ps2的,所以我们现在需要一个转接头,也就是适配器

// 计算机
    class Computers{
        constructor(type) {
            this._type = type;
            this._port = 'usb'
        }
        connect() {
            if(this._type == this._port){
                console.log('连接成功')
            }else{
                console.log('连接失败,只能使用usb连接')
            }
        }
        getType() {
            return this._type;
        }
    }
    // 适配器
    class Adapter extends Computers{
        constructor(type) {
            super()
            this._type = type;
            this.transform()
        }
        getType() {
            return this._type;
        }
        transform(){
            if(this._type === this._port){
                return this._type;
            }else {
                return this._port;
            }
        }
    }
    // ps2接口
    class Ps2{
        constructor() {
            this.__proto__ = new Adapter('usb') 
        }
    }
    // usb接口
    class Usb{
        constructor() {
            this.__proto__ = new Adapter('usb') 
        }
    }
    let computers = new Computers(new Ps2().transform());
    console.log(computers.getType()) // usb
    computers.connect(); // 连接成功
    let computers1 = new Computers(new Usb().transform());
    console.log(computers1.getType()); // usb
    computers1.connect(); // 连接成功
posted @ 2019-06-12 20:01  我会放电啪啪  阅读(298)  评论(0编辑  收藏  举报