组合型 - 适配器模式
将一个类的接口转换成客户希望的另一个接口。适配器模式让那些接口不兼容的类可以一起工作
主要解决在软件系统中,常常要将一些"现存的对象"放到新的环境中,而新环境要求的接口是现对象不能满足的
// 只接受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])