抽象工厂(Abstract Factory)

Posted RunningAndRunning

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了抽象工厂(Abstract Factory)相关的知识,希望对你有一定的参考价值。

抽象工厂模式是工厂模式的升级版,用于创建一组相关或者相互依赖的对象

// 抽象工厂模式
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)的主要内容,如果未能解决你的问题,请参考以下文章

抽象工厂模式(Abstract Factory)

抽象工厂模式 (Abstract Factory Pattern)

抽象工厂模式(Abstract Factory)

抽象工厂(Abstract Factory)

浅析设计模式——创建型模式之Abstract-Factory(抽象工厂模式)

抽象工厂模式(Abstract Factory)