Skip to content

Latest commit

 

History

History
33 lines (30 loc) · 771 Bytes

05.md

File metadata and controls

33 lines (30 loc) · 771 Bytes

js 面向对象初接触(5)

组合型 - 适配器模式

适配器模式定义

将一个类的接口转换成客户希望的另一个接口。适配器模式让那些接口不兼容的类可以一起工作

适配器模式何时使用

主要解决在软件系统中,常常要将一些"现存的对象"放到新的环境中,而新环境要求的接口是现对象不能满足的

js 代码模拟

// 只接受number类型
class Product {
    constructor() {
    }
    say(a, b) {
        console.log(Math.max(a, b))
    }
}
let p = new Product();
p.say(1, 2)
// 适配数组
class Target {
    constructor() {
        this.p = new Product();
    }
    say(arr) {
        this.p.say.apply(null, arr);
    }
}
let t = new Target();
t.say([2, 4])