Design Patterns in WinJS #4: Factory Method

I've decided to write more OOP articles in WinJS library regarding the use of Design Patterns we all love! You can learn my previous parts:
Let's start with what this design pattern means.
What is Factory Method Design Pattern? 
Factory Method helps you create new objects by knowing the type of the object.The user may not know the details of each object's type and its details but Factory helps the user to select a type and instantiate.

According to DoFactory,Factory Method is:


Creator is our FactoryMethod here which creates objects with types defined.

AbstractProduct is an Interface and Interfaces arent its out of the picture.

Products are the objects created by FactoryMethod.

Factory Methods are frequently used by many Javascript scripts.The power of the Factory Method comes from its extensibility. We shall see it an example soon but before getting ahead of coding,I'd like to give a real world example for Factory Methods.

Think of a company.Think of its employees.A company and its employees are a fine example to Factory Method.

Every Individual(object created from Company class) is an object created with its type.Whenever a job needs to be assigned or you simply wanna chat,you get to that person or mail to the person.The person here has a type and he does a specific job.

The Company here is our Factory Method.This pattern will create Employees with its types of job they do and assign jobs to complete.

Our example is sound and clear.We shall create our Company first:
  1. var Company = WinJS.Class.define(  
  2. function() {  
  3.     this.createEmployee = function (type) {  
  4.         var employee;  
  6.         if (type === "Developer") {  
  7.             employee = new Developer();  
  8.         } else if (type === "ProjectManager") {  
  9.             employee = new ProjectManager();  
  10.         } else if (type === "Designer") {  
  11.             employee = new Designer();  
  12.         } else if (type === "Analyst") {  
  13.             employee = new Analyst();  
  14.         }  
  16.         employee.type = type;  
  18.         employee.say = function () {  
  19.             Helper.Func.add(this.type + " earns " + this.hourly + " per hour");  
  20.         }  
  22.         return employee;  
  23.     }  
  24. });   
According to the above code,we define jobs to complete according to the types created once createEmployee function is called.

Lets add our Employees now:
  1. var Developer = WinJS.Class.define(  
  2.     function () {  
  3.     this.hourly = "$20";  
  4. });  
  7. var ProjectManager = WinJS.Class.define(  
  8.     function () {  
  9.     this.hourly = "$25";  
  10. });  
  12. var Designer = WinJS.Class.define(  
  13.     function () {  
  14.     this.hourly = "$20";  
  15. });  
  17. var Analyst = WinJS.Class.define(  
  18.     function () {  
  19.     this.hourly = "$15";  
  20. }); 
Here are the employees that completes specific jobs,you can extend it according to your needs.

These employee types have property name "hourly" which defines their hourly payment.
Now lets create our Helper class.Our helper class will never change in these series:
  1. WinJS.Namespace.define("Helper");  
  2. var log = "";  
  4. Helper.Func =  
  5. {  
  6.     add: function (msg) { log += msg + "\n"; },  
  7.     show: function () { console.log(log); log = ""; }  
  8. };   
Our classes and Helper functions are defined. Now,its time to call them.

First we create our Company object:
  1. var company = new Company();  
Then create our Employees,define their types as parameter and send their message to Helper Functions:
  1. company.createEmployee("Developer").say();  
  2. company.createEmployee("ProjectManager").say();  
  3. company.createEmployee("Designer").say();  
  4. company.createEmployee("Analyst").say();  
And last we show the message:

Lets build&run the project,and see the output: 

Each employee has a different type and the jobs they do can vary if you choose them to.You can set additional jobs for specific position. It's all a good show of extensibility of Factory Method. Use it well.

Why should you use it?

It's popular! It's used by many scripts and websites.Defines types and does specific jobs. Moreover it's extensible.You can extend the types and the jobs they need to complete according to your needs. 

Similar Articles
Araf Global is a software consultancy company founded in 2016 focusing on cutting edge technologies.