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 supported.so 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.
According to DoFactory,Factory Method is:
Creator is our FactoryMethod here which creates objects with types defined.
AbstractProduct is an Interface and Interfaces arent supported.so 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.
Why?
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.
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.
Implementation
Our example is sound and clear.We shall create our Company first:
- var Company = WinJS.Class.define(
- function() {
- this.createEmployee = function (type) {
- var employee;
- if (type === "Developer") {
- employee = new Developer();
- } else if (type === "ProjectManager") {
- employee = new ProjectManager();
- } else if (type === "Designer") {
- employee = new Designer();
- } else if (type === "Analyst") {
- employee = new Analyst();
- }
- employee.type = type;
- employee.say = function () {
- Helper.Func.add(this.type + " earns " + this.hourly + " per hour");
- }
- return employee;
- }
- });


Sonu ChaudharyPosted May 26, 2016, 10:21 AM
good one Erosy
Vignesh ManiPosted May 25, 2016, 8:22 AM
Nice one
Hari ShankerPosted May 25, 2016, 4:08 AM
Nice Article
Gowtham RajamanickamPosted May 25, 2016, 1:13 AM
Good one..
Munesh SharmaPosted May 25, 2016, 12:26 AM
good one
Thiruppathi RPosted May 24, 2016, 3:43 PM
Nice Info...
Debasis SahaPosted May 24, 2016, 1:46 PM
Good One..
Sonu ChaudharyPosted May 24, 2016, 12:45 PM
nice article
Kuppurasu NagarajPosted May 24, 2016, 12:03 PM
Nice Sharing..