什么是适配器模式
适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。
面向对象例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
class Adapter{ getSingaporeCityData() { return { shenggang: { id: '0001', coordinate: [1.3312, 103.32423] }, fenwei: { id: '0002', coordinate: [1.3221, 103.1212] }, angmokio: { id: '0003', coordinate: [1.654, 103.32465423] } } } getFullSingaporeCityData() { return [ { name: 'shenggang', id: '0001', coordinate: [1.3312, 103.32423], peopleCount: 10000 }, { name: 'fenwei', id: '0002', coordinate: [1.3312, 103.32423], peopleCount: 20000 }, { name: 'angmokio', id: '0003', coordinate: [1.3312, 103.32423], peopleCount: 30000 } ] }
dataAdapter(fn) { let ret = {}; let list = fn(); list.forEach(function(v, k) { let {name, ...props} = v; ret[name] = {...props}; }); return function() { return ret; } } }
|