美文网首页
适配器模式

适配器模式

作者: 闹鬼的金矿 | 来源:发表于2022-04-24 20:44 被阅读0次

适配器是一种比较容易理解的设计模式,它主要是为了解决接口不兼容的问题。但是也造成了额外的代码量,增加了维护的工作量。所以一般来说,不建议使用它来处理接口无法兼容的问题,应该是首选重构。除非遇到一些比较特殊的情况。比如引用了一些非开源的库,或者是第三方的系统,要么根本看不到源码,要么第三方暂时没办法配合去修改接口。

说的直接点:现在需要调用A接口,但是只提供了B接口,通过适配器C,最后可以实现将B当做A来使用。但是使用上需要考虑额外维护成本。

适配器的结构如下图所示:

适配器.png

客户端能够使用的接口是Client Interface,Adapter实现了Client Interface的接口。实际上它的实现是通过Service来完成的。最终客户端通过Adapter实现了对Service的调用,不管Service的接口是如何定义的。

接下来参考一个适配器的代码例子。

首先定义了一个RoundPeg和RoundHole,通过RoundHole可以过滤指定半径大小的RoundPeg

class RoundPeg {
    
    let radius: Double
    
    init(r: Double) {
        self.radius = r
    }
    
    func radiusValue() -> Double {
        return self.radius
    }
}

class RoundHole {
    
    let radius: Double
    
    init(r: Double) {
        self.radius = r
    }
    
    func fits(roundPeg: RoundPeg) -> Bool {
        return self.radius >= roundPeg.radiusValue()
    }
}

现在新增了SquarePeg,它不是RoundPeg类型,无法直接通过RoundHole进行过滤

//长方体,长和宽一样
class SquarePeg {
    
    let width: Double
    
    init(width: Double) {
        self.width = width
    }
}

为了能让RoundHole也能对SquarePeg进行过滤,创建一个SquarePegAdapter,它可以通过一个SquarePeg生成。但是它对外声明的是RoundPeg的接口,所以通过RoundHole可以直接使用SquarePegAdapter。而SquarePegAdapter具体实现由SquarePeg完成,从而间接实现了RoundHole对SquarePeg的过滤。

//适配长方体和圆柱体
class SquarePegAdapter: RoundPeg {
    
    let peg: SquarePeg
    
    init(peg: SquarePeg) {
        self.peg = peg
        super.init(r: self.peg.width)
    }
    
    override func radiusValue() -> Double {
        return self.peg.width * sqrt(2) / 2
    }
}

最后的测试代码:

func testAdapter() {
    
    let roundHole = RoundHole(r: 100)
    
    let roundPeg = RoundPeg(r: 99)
    
    print("\(roundHole.fits(roundPeg: roundPeg))")
    
    let squarePeg1 = SquarePeg(width: 200)
    let squarePeg2 = SquarePeg(width: 120)
    
//    print("\(roundHole.fits(roundPeg: squarePeg))")
    
    let squarePegAdaptor1 = SquarePegAdapter(peg: squarePeg1)
    let squarePegAdaptor2 = SquarePegAdapter(peg: squarePeg2)
    print("\(roundHole.fits(roundPeg: squarePegAdaptor1))")
    print("\(roundHole.fits(roundPeg: squarePegAdaptor2))")
}

Reference: Design-Patterns

相关文章

  • Java设计模式(二)

    talk is cheap show me the code 适配器模式 类适配器模式 接口适配器模式 对象适配器...

  • 适配器模式

    目录 1、什么是适配器模式? 2、适配器模式结构? 3、如何实现适配器模式? 4、适配器模式的特点? 5、适配器模...

  • 设计模式之适配器模式

    适配器模式: 类适配器模式、对象适配器模式、接口适配器模式 1.类适配器模式:新的接口出现了,但是和老的接口不兼容...

  • 学习iOS设计模式第一章 适配器(Adapter)

    今天学习了iOS设计模式中的适配器模式,适配器有两种模式对象适配器模式-- 在这种适配器模式中,适配器容纳一个它包...

  • 第4章 结构型模式-适配器模式

    一、适配器模式简介 二、适配器模式的优点 三、适配器模式的实例

  • 设计模式(Design Patterns)适配器模式(Adapt

    适配器模式主要分为三类:类的适配器模式、对象的适配器模式、接口的适配器模式。 类的适配器模式 场景:将一个类转换成...

  • 适配器模式

    适配器模式主要分为三类:类的适配器模式、对象的适配器模式、接口的适配器模式。适配器模式将某个类的接口转换成客户端期...

  • 适配器模式

    先直观感受下什么叫适配器 适配器模式有类的适配器模式和对象的适配器模式两种不同的形式。 类适配器模式 对象适配器模...

  • 适配器模式

    适配器模式 一、适配器模式定义 适配器模式的定义是,Convert the interface of a clas...

  • 设计模式:结构型

    享元模式 (Pools,Message) 代理模式 适配器模式 :类适配器和对象适配器 装饰者模式 外观模式 桥接...

网友评论

      本文标题:适配器模式

      本文链接:https://www.haomeiwen.com/subject/gpcxyrtx.html