Coding Guru

Code never does the mistake, we make the mistakes in the code. Think, Imagine, Phrase and then Code.

The Factory Pattern

It defines an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. 

This design pattern focuses on object creation but differs from other patterns, in which it does not require a constructor function like other creation design patterns. This pattern supplies an interface to the developers to create the new objects by using factory instead of invoking new operator for an object. For example we need a car door and goto a car factory which produces the product we're interested in and ask it to give us what we need. The factory then creates a new car door (or object) and gives it to us. This example gives a nice picture for how this factory pattern actually works; we simply ask it for the type of component,it instantiates the component and returns us what we were looking for.

The main purpose of the Factory Method is extensibility.Factory Methods are used in the applications which manage, maintain and manipulate collection of objects which are different however at the same time have various characteristics (i.e. functions and properties) in common.

Diagram


Participants

The objects participating in this pattern are:

  • Creator -- In sample code: Factory
    • the 'factory' object that creates new products
    • implements 'factoryMethod' which returns newly created products
  • AbstractProduct -- not used in JavaScript
    • declares an interface for products
  • ConcreteProduct -- In sample code: Employees
    • the product being created
    • all products support the same interface (properties and methods)

Sample code

In this example the Factory creates four different types of employees. Each employee has a different hourly rate. The createEmployee method is the actual Factory Method. The client instructs the factory what type of employee to create by passing a type argument into the Factory Method.

The AbstractProduct in the diagram is 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).

Four different employee types are created; all are stored in the same array. Each employee is asked to say what they are and their hourly rate.

The log function is a helper which collects and displays results. 

function Factory() {
this.createEmployee = function (type) {
var employee;

if (type === "fulltime") {
employee = new FullTime();
} else if (type === "parttime") {
employee = new PartTime();
} else if (type === "temporary") {
employee = new Temporary();
} else if (type === "contractor") {
employee = new Contractor();
}

employee.type = type;

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

return employee;
}
}

var FullTime = function () {
this.hourly = "$12";
};

var PartTime = function () {
this.hourly = "$11";
};

var Temporary = function () {
this.hourly = "$10";
};

var Contractor = function () {
this.hourly = "$15";
};

// log helper
var log = (function () {
var log = "";

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

function run() {
var employees = [];
var factory = new Factory();

employees.push(factory.createEmployee("fulltime"));
employees.push(factory.createEmployee("parttime"));
employees.push(factory.createEmployee("temporary"));
employees.push(factory.createEmployee("contractor"));
for (var i = 0, len = employees.length; i < len; i++) {
employees[i].say();
}

log.show();
}


Advantages

  • Makes a lot complex object creation very easy through an interface which bootstraps this process for us
  • Very good for generating multiple objects based on the environment
  • Practically for components which require similar instantiation or functions
  • Excellent for decoupling the components by bootstrapping the instantiation of different object to work for particular instances

Disadvantages

  • Unit testing is a bit difficult as the direct result of object creation process is hidden by the factory methods
Loading