标签:var bst 相关 升级版 bsp color null custom prot
抽象工厂模式是工厂模式的升级版,用于创建一组相关或者相互依赖的对象
// 抽象工厂模式 function Car (name, color) { this.name = name; this.color = color; } Car.prototype.drive = function () { console.log(‘drive‘) } Car.prototype.breakDown = function () { console.log(‘breakDown‘) } function Trunk (name, color) { this.name = name; this.color = color; } let AbstractVehicleFactory = (function () { let types = []; return { getVehicle (type, customizations) { var Vehicle = types[type]; return (Vehicle)? new Vehicle(customizations):null; }, registerVehicle (type, Vehicle) { let proto = Vehicle.prototype; if (proto.drive && proto.breakDown) { types[type] = Vehicle; } return AbstractVehicleFactory; } } } )() AbstractVehicleFactory.registerVehicle(‘car‘, Car); let car = AbstractVehicleFactory.getVehicle(‘car‘, ‘dsdsds‘)
虽然代码能看懂,但还是似懂非懂不知道什么时候用,后续继续学习更新~~~
标签:var bst 相关 升级版 bsp color null custom prot
原文地址:http://www.cnblogs.com/running1/p/7532994.html