码迷,mamicode.com
首页 > 其他好文 > 详细

抽象工厂(Abstract Factory)

时间:2017-09-16 23:20:28      阅读:230      评论:0      收藏:0      [点我收藏+]

标签: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‘)

 虽然代码能看懂,但还是似懂非懂不知道什么时候用,后续继续学习更新~~~

抽象工厂(Abstract Factory)

标签:var   bst   相关   升级版   bsp   color   null   custom   prot   

原文地址:http://www.cnblogs.com/running1/p/7532994.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!