Tuesday, 19 May 2015

Design Patterns In JavaScript - Creational, Part 3

Factory Pattern


Factory Pattern we use to create objects, this object can be different types of objects but usually related in some way. for example they could share the same parent object or data type or they just have the same interface.

The Factory Method allows the client to delegate object creation while still retaining control over which type to instantiate.

The key objective of the Factory Method is extensibility. Factory Methods are frequently used in applications that manage, maintain, or manipulate collections of objects that are different but at the same time have many characteristics (i.e. methods and properties) in common.

Three 
objects should participating in this pattern are:
  • Creator: Factory:
    • The 'factory' object that creates new products implements 'factoryMethod' which returns newly created products.
  • Abstract Product :
    • Not used in JavaScript (The AbstractProduct not implemented because Javascript does not support abstract classes or interfaces. However, we still need to ensure that all employee types have the same interface (properties and methods)).
    • declares an interface for products
  • Concrete Product:
    • The product being created.
    • All products support the same interface (properties and methods)

We have an example like Employee factory, we have CEO, Project Manager, Team Leader, Software Enginner And Quality Software Engineer positions. Each one of them has his hourly rate.
  • Employee is the factory.
  • CEO, Project Manager, Team Leader, Software Beginner And Quality Software Engineer are the Concrete Products.

var log = (function () {
    var log = "";
 
    return {
        add: function (msg) { log += msg + "\n"; },
        show: function () { alert(log); log = ""; }
    }
})();

var EmployeeFactory = (function(log){

    var CEO = function () {
        this.hourly = "$100";
    };
 
    var ProjectManager = function () {
        this.hourly = "$80";
    };
 
    var TeamLeader = function () {
        this.hourly = "$65";
    };
 
    var SoftwareEnginner = function () {
        this.hourly = "$30";
    };

    var SoftwareQualityEnginner = function () {
        this.hourly = "$30";
    };

    var createEmployee = function(type){
        var employee;
 
        if (type === "CEO") {
            employee = new CEO();
        } else if (type === "ProjectManager") {
            employee = new ProjectManager();
        } else if (type === "TeamLeader") {
            employee = new TeamLeader();
        } else if (type === "SoftwareEnginner") {
            employee = new SoftwareEnginner();
        } else if (type === "SoftwareQualityEnginner") {
            employee = new SoftwareQualityEnginner();
        }
 
        employee.type = type;

        employee.say = function () {
            log.add(this.type + ": rate " + this.hourly + "/hour");
        }

        return employee;
    };


    return {
       createEmployee: createEmployee
    };
})(log);
 
 
var run = function() {
    var employees = [];

    employees.push(EmployeeFactory.createEmployee("CEO"));
    employees.push(EmployeeFactory.createEmployee("TeamLeader"));
    employees.push(EmployeeFactory.createEmployee("SoftwareEnginner"));
    employees.push(EmployeeFactory.createEmployee("SoftwareQualityEnginner"));
    
    for (var i = 0, len = employees.length; i < len; i++) {
        employees[i].say();
    }
 
    log.show();
}

run();
The core method of the above code is the createEmployee Method, check for type, if we need to apply specific business we will apply and return a new object to the user.

No comments:

Post a Comment