Skip to content

Instantly share code, notes, and snippets.

@laispace
Last active August 29, 2015 14:15
Show Gist options
  • Save laispace/891acb78763749581b2d to your computer and use it in GitHub Desktop.
Save laispace/891acb78763749581b2d to your computer and use it in GitHub Desktop.

Revisions

  1. laispace revised this gist Feb 8, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions pattern-factory.js
    Original file line number Diff line number Diff line change
    @@ -57,6 +57,7 @@ console.log(truck);


    // ===== 2.pattern-abstract-factory.j =====

    var AbstractFactory = (function () {
    // store registed vehicles
    var types = {};
  2. laispace revised this gist Feb 8, 2015. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions pattern-factory.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // pattern-factory.js
    // ===== 1.pattern-factory.js=====

    // Car factory
    function Car (options) {
    @@ -56,9 +56,7 @@ var truck = vehicleFactory.create({
    console.log(truck);


    // ==============================================================

    // pattern-abstract-factory.js
    // ===== 2.pattern-abstract-factory.j =====
    var AbstractFactory = (function () {
    // store registed vehicles
    var types = {};
  3. laispace revised this gist Feb 8, 2015. 1 changed file with 70 additions and 15 deletions.
    85 changes: 70 additions & 15 deletions pattern-factory.js
    Original file line number Diff line number Diff line change
    @@ -3,15 +3,15 @@
    // Car factory
    function Car (options) {
    this.type = options.type || 'car';
    this.color = options.color || 'white';
    this.wheel = options.wheel || 'small';
    this.owner = options.owner || null;
    this.price = options.price || null;
    }

    // Trunk factory
    function Trunk (options) {
    this.type = options.type || 'trunk';
    this.color = options.color || 'blue';
    this.wheel = options.wheel || 'large';
    function Truck (options) {
    this.type = options.type || 'truck';
    this.owner = options.owner || null;
    this.price = options.price || null;
    }

    // Vehicle factory
    @@ -26,9 +26,9 @@ Vehicle.prototype.create = function (options) {
    if (type === 'car') {
    // create a car with Car factory
    this.supClass = Car;
    } else if (type === 'trunk') {
    // create a trunk with Trunk factory
    this.supClass = Trunk;
    } else if (type === 'truck') {
    // create a truck with Trunk factory
    this.supClass = Truck;
    }

    // return a vehicle instance
    @@ -41,12 +41,67 @@ var vehicleFactory = new Vehicle();

    // now we create a car
    var car = vehicleFactory.create({
    type: 'car'
    type: 'car',
    owner: 'xiaolai-1',
    price: 11111
    });
    console.log(car); // => Car {type: "car", color: "white", wheel: "small"}
    console.log(car);

    // then we create a trunk
    var trunk = vehicleFactory.create({
    type: 'trunk'
    // then we create a truck
    var truck = vehicleFactory.create({
    type: 'truck',
    owner: 'xiaolai-2',
    price: 22222
    });
    console.log(trunk); // => Trunk {type: "trunk", color: "blue", wheel: "large"}
    console.log(truck);


    // ==============================================================

    // pattern-abstract-factory.js
    var AbstractFactory = (function () {
    // store registed vehicles
    var types = {};

    return {
    // register a new vehicle type
    register: function (type, Vehicle) {
    // if types[type] exist
    if (types[type]) {
    // do nothing
    } else {
    // else register this new type
    types[type] = Vehicle;
    }

    return this;
    },
    // get vehicle from store and set custimal options generating an instance
    get: function (type, custimalOptions) {
    var Vehicle = types[type];
    if (Vehicle) {
    return new Vehicle(custimalOptions);
    } else {
    // not exist
    return null;
    }
    }
    };
    })();

    // now we register two type of vehicles
    AbstractFactory.register('car', Car);
    AbstractFactory.register('truck', Truck);

    // then we get instance with custimal options
    var car = AbstractFactory.get('car', {
    'owner': 'xiaolai-3',
    'price': 33333
    });
    console.log(car);

    var truck = AbstractFactory.get('truck', {
    'owner': 'xiaolai-4',
    'price': 44444
    });
    console.log(truck);
  4. laispace created this gist Feb 8, 2015.
    52 changes: 52 additions & 0 deletions pattern-factory.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    // pattern-factory.js

    // Car factory
    function Car (options) {
    this.type = options.type || 'car';
    this.color = options.color || 'white';
    this.wheel = options.wheel || 'small';
    }

    // Trunk factory
    function Trunk (options) {
    this.type = options.type || 'trunk';
    this.color = options.color || 'blue';
    this.wheel = options.wheel || 'large';
    }

    // Vehicle factory
    function Vehicle () {}

    // set default prototype to Car
    Vehicle.prototype.supClass = Car;

    // create vehicle with options
    Vehicle.prototype.create = function (options) {
    var type = options.type;
    if (type === 'car') {
    // create a car with Car factory
    this.supClass = Car;
    } else if (type === 'trunk') {
    // create a trunk with Trunk factory
    this.supClass = Trunk;
    }

    // return a vehicle instance
    var vehicle = new this.supClass(options);
    return vehicle;
    };

    // first we new a factory instance
    var vehicleFactory = new Vehicle();

    // now we create a car
    var car = vehicleFactory.create({
    type: 'car'
    });
    console.log(car); // => Car {type: "car", color: "white", wheel: "small"}

    // then we create a trunk
    var trunk = vehicleFactory.create({
    type: 'trunk'
    });
    console.log(trunk); // => Trunk {type: "trunk", color: "blue", wheel: "large"}