关于javascript:Javascript-设计模式Factory-工厂模式

10次阅读

共计 1363 个字符,预计需要花费 4 分钟才能阅读完成。

Factory (工厂模式)

Factory 提供一个通用的接口来创建对象,咱们能够指定咱们所心愿创立的工厂对象的类型。

何时应用 Factory:

  • 当对象或组件设置波及高复杂性时
  • 当须要依据所在不同环境轻松生成对象的不同实例时
  • 当解决很多共享雷同属性的小型对象或组件时
  • 在编写只须要满足一个 API 契约的其余对象的实例对象时。对于解耦是很有用的。

示例代码:

// eg:
function Car(opt) {
    // 默认值
    this.doors = opt.doors || 4;
    this.state = opt.state || 'brand new';
    this.color = opt.color || 'silver';
}

function Truck(opt) {
    // 默认值
    this.state = opt.state || 'used';
    this.wheelSize = opt.wheelSize || 'large';
    this.color = opt.color || 'blue';
}

function Factory() {}

// 定义该工厂 factory 的原型和试用工具,默认为 Car
Factory.prototype.class = Car;

Factory.prototype.createObj = function (opt) {if (opt.class === 'car') {this.class = Car;} else {this.class = Truck;}
    return new this.class(opt);
}

// TestCode
// 创立生成汽车的工厂实例
var carFactory = new Factory();
var car = carFactory.createObj({
    class: 'car',
    doors: '2',
    state: 'aodi',
    color: 'black'
});

// console.log(car instanceof Car);  // true
console.log(car);


// 形象工厂模式
var AbstractFactory = (function () {
    // 存储车辆类型
    var types = {};

    return {getVehicle: function (type, customizations) {var Vehicle = types[type];
            return (Vehicle) ? new Vehicle(customizations) : null;
        },

        registerVehicle: function (type, Vehicle) {
            var proto = Vehicle.prototype;

            // 只注册实现车辆契约的类  只有实现这两个办法能力被注册
            // if (proto.dirve && proto.breakDown) {//     types[type] = Vehicle;
            // }
            types[type] = Vehicle;
            return AbstractFactory;
        }
    }
})();

AbstractFactory.registerVehicle('car', Car);
AbstractFactory.registerVehicle('truck', Truck);

var car2 = AbstractFactory.getVehicle('car', {
    color: 'lime green',
    state: 'like new'
})

console.log(car2);
正文完
 0