抽象工廠(Abstract Factory)
阿新 • • 發佈:2017-09-17
工廠 cnblogs null pro pes reg 抽象 blog log
抽象工廠模式是工廠模式的升級版,用於創建一組相關或者相互依賴的對象
// 抽象工廠模式 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‘)
雖然代碼能看懂,但還是似懂非懂不知道什麽時候用,後續繼續學習更新~~~
抽象工廠(Abstract Factory)